随机背景图API接口
1、api接口整合
夏沫:https://cdn.seovx.com/
搏天api:https://api.btstu.cn
IMGAPI:https://imgapi.cn/
ChenYFan:https://api.cyfan.top/
岁月小筑:https://img.xjh.me/
樱花二次元:https://www.dmoe.cc/
保罗 API:https://api.paugram.com/
如诗:https://api.likepoems.com/
小歪:https://api.ixiaowai.cn/
东方:https://img.paulzzh.com/
2、自制随机图
服务器自存图片
准备图片—>书写路径—>书写php文件
在img文件夹里新建img.txt,文件中直接写图片的URL地址,如:
https://static.likepoems.com/2020/10/06/385ea1c4c8b4fbac57f5a0aa61f033761.jpg
https://static.likepoems.com/2021/06/12/20210126104630786.png
新建index.php文件。文件内容如下:
<?php
//存放api随机图链接的文件名img.txt
$filename = "img.txt";
if(!file_exists($filename)){
die('文件不存在');
}
//从文本获取链接
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)){
$line=trim(fgets($fs));
if($line!=''){
array_push($pics, $line);
}
}
//从数组随机获取链接
$pic = $pics[array_rand($pics)];
//返回指定格式
$type=$_GET['type'];
switch($type){
//JSON返回
case 'json':
header('Content-type:text/json');
die(json_encode(['pic'=>$pic]));
default:
die(header("Location: $pic"));
}
?>
然后放在服务器里面的api文件夹里,绑定域名,记得开启php环境
访问路径:你的域名/api/img
通过sina图床搭建的随机图
index.php
<?php
//读取文本
$str = explode("\n", file_get_contents('img.txt'));
$k = rand(0,count($str));
$sina_img = str_re($str[$k]);
// 定义多个新浪图片请求接口
$size_arr = array('large', 'mw1024', 'mw690', 'bmiddle', 'small', 'thumb180', 'thumbnail', 'square');
$size = !empty($_GET['size']) ? $_GET['size'] : 'large' ;
$server = rand(1,4);
if(!in_array($size, $size_arr)){
$size = 'large';
}
$url = 'https://tva'.$server.'.sinaimg.cn/'.$size.'/'.$sina_img.'.jpg';
//解析url
$result=array("code"=>"200","imgurl"=>"$url");
//定义type返回类型
$type=$_GET['return'];
switch ($type)
{
//Json格式解析
case 'json':
$imageInfo = getimagesize($url);
$result['width']="$imageInfo[0]";
$result['height']="$imageInfo[1]";
header('Content-type:text/json');
echo json_encode($result);
break;
//返回图片链接
default:
header("Location:".$result['imgurl']);
break;
}
function str_re($str){
$str = str_replace(' ', "", $str);
$str = str_replace("\n", "", $str);
$str = str_replace("\t", "", $str);
$str = str_replace("\r", "", $str);
return $str;
}
?>
img.txt
007LvBV9gy1h4ogsz8i56j31hc0u0h99
007LvBV9gy1h4ogszkea5j31hc0u0qko
007LvBV9gy1h4ogszvqhuj31hc0u0k8t
007LvBV9gy1h4ogt05g8sj30xc0n3gue
007LvBV9gy1h4ogt0hx1qj31hc0xc7il
和上面的一样,只是请求的是微博的图片,比较稳定,速度快。
请求示例:
你的域名/api/img?return=json 返回json格式
你的域名/api/img 返回普通图片
SQL版本
数据存放在TXT里面影响效率,故有了SQL版本
ACGIMG