Loading... <div class="tip share">请注意,本文编写于 433 天前,最后修改于 433 天前,其中某些信息可能已经过时。</div> ## 前言 逛github的时候偶然发现了这个api,简洁但是功能挺全的,而且还有一个好看的说明文档以及图片集,所以分享给大家。 [github地址](https://github.com/Crazy-White/Random-Picture)    ## 搭建及使用 下载[源码](https://codeload.github.com/Crazy-White/Random-Picture/zip/master),上传至网站任意目录,打开输入网址即可使用。 **说明:** * 自行添加或修改`url.txt`,加入图片链接,一行一个,注意不能有多余的空格和空行。 * 输入`yoururl/360.php`,可以随机获得360壁纸一张,大小200k左右 * 输入`yoururl/bing.php`,可以获得bing每日壁纸 * 输入`yoururl/show.php`,展示url.txt的所有图片 * 输入`yoururl/description.html`,展示说明文档 **其他参数:** * 访问`yoururl/`,默认跳转到一张随机图 * 访问`yoururl/?id=数字`,跳转到指定一张图片 * 访问`yoururl/?type=output`,服务器读取图片后输出(不建议使用) * 访问`yoururl/?type=json`,返回json格式 > ` {"code":"200","url":"https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg","width":"1920","height":"1080","mime":"image\/jpeg","size":"821735"}` * 访问`yoururl/?type=js`,输出js > `var pic_random='';var pic_end=35;var pic_rdn=34;` ## 修改 我发现输出json格式的时候特别慢,看了一下源码发现它用了个`getimagesize($x)`函数,每次访问的时候都要先对图片进行一次大小计算,自然就慢了。如果不想现场计算又想能够输出大小信息的话,可以将大小信息和url直接写成json格式再写入txt,让php直接读取json即可。 首先,`url.txt`中的内容应该写成以下格式: ```json {"name":"01-02.jpg","url":"https://pb.yanshu.work/image/5dc7e14c6ab7c","size":"647.636kb","width":"1280","height":"783"} ``` 同样一行一个,参数可以自行添加减少。 然后修改`index.php`为: ```php <?php $url = explode("\n", file_get_contents('url.txt'));//url数组 $i = array_rand($url);//随机id $arr = json_decode($url[$i], true); //获取txt文本中随机一行,并将其转换为json格式 $x = ($arr['url']);//随机图片url $id = $_REQUEST['id']; $type = $_REQUEST['type']; switch ($type) { case 'json': $result = array("code" => "200", "url" => "{$x}"); $result['id']=$i; $result['name']="{$arr['name']}"; $result['size'] = "{$arr['size']}"; $result['width'] = "{$arr['width']}"; $result['height'] = "{$arr['height']}"; header('Access-Control-Allow-Origin:*'); header('Content-type:text/json'); echo json_encode($result); break; case 'js': header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-cache"); header("Pragma: no-cache"); header("Content-type:application/x-javascript"); echo "var pic_random=" . "'" . $x . "'" . ";"; echo "var pic_end=" . count($url) . ";"; echo "var pic_rdn=" . $i . ";"; break; default: if ($id == "") { header("Location:$x"); } else { $x = json_decode($url[$id], true)['url']; header("Location:$x"); } } ``` 测试一下打开速度有了明显提升。 当然,最大的问题还是如何批量生成json格式的txt文档,这时候就建议你学一门编程了,个人认为python最容易上手。<hr class="content-copyright" style="margin-top:50px" /><blockquote class="content-copyright" style="font-style:normal"><p class="content-copyright">版权属于:雁陎</p><p class="content-copyright">本文链接:<a class="content-copyright" href="https://www.sitstars.com/archives/63/">https://www.sitstars.com/archives/63/</a></p><p class="content-copyright">转载时须注明出处及本声明</p></blockquote> Last modification:November 16th, 2019 at 10:18 pm © 允许规范转载