本文利用的新浪图库的api接口实现是网站页面随机背景图,既刷新既换,每次切换网页自动随机展示一张背景图片。因为图片不在本地保存,所以大大节约了网站的服务器空间。既耍酷又能节约本地空间一举两得。(注:此方法只适用于PHP环境的网站)
首先建一个txt文本文件,用于存放接口里每张图片地址的id拼接参数,下面是收集的一些最近比较流行的二次元图片参数地址。由于本文内容有限只发出一部分,如下,保存RandImg.txt。
005WQYV9gy1fqgr53mydzj31hc0u0aci
0072Vf1pgy1foxk3shjrpj31hc0u0k3d
7f0c40d4gy1fqbg4jwszkj21hc0u0q7z
7f0c40d4gy1fqbfh91nf9j21hc0u0q62
7f0c40d4gy1fqbf0e94b8j21hc0xc1ky
0072Vf1pgy1foxk7sny76j31hc0u0du7
0072Vf1pgy1foxlnr1pdmj31hc0u0h1c
0072Vf1pgy1foxkf0qyqbj31hc0u0h2u
0072Vf1pgy1fodqnt870uj31kf14ex6p
0060lm7Tly1fpx1skrabaj31hc0u01kx
0072Vf1pgy1foxk7k489xj31kw0w0e3a
0072Vf1pgy1foxki8yafwj31kw0w04oy
0072Vf1pgy1foxkjjaqimj31hc0u07jh
0072Vf1pgy1fodqnwsypsj31hc10qe82
0072Vf1pgy1foxkbwxfq0j31hc0u0h43
0072Vf1pgy1foxli7783pj31hc0u0n9g
0072Vf1pgy1foxk7272z8j31kw0w0h0a
0072Vf1pgy1foxkic3ld0j31hc0u07rx
0072Vf1pgy1foxlnuvhbej31hc0u0wuw
0072Vf1pgy1foxkexoreoj31hc0u0qn0
0072Vf1pgy1foxlnixk7ej31hc0u0ke6
0072Vf1pgy1foxkf55zbij31kw0w0e55
0072Vf1pgy1foxlnzmfnjj31kw0w0az5
0072Vf1pgy1foxliaky92j31kw0w0b16
0072Vf1pgy1foxkcriixpj31hc0u0wu8
0072Vf1pgy1foxkj2ashuj31kw0w0hcp
0072Vf1pgy1foxkd6lx6jj31kw0w0wyb
0072Vf1pgy1foxkfqehi6j31hc0u0184
0072Vf1pgy1foxkcsx9rmj31hc0u0h9k
0072Vf1pgy1foxk70rdinj31kw0w0b19
0072Vf1pgy1foxk7lqroij31kw0w0nkz
0072Vf1pgy1foxlobn7kfj31hc0u01db
0072Vf1pgy1fodqo2pcz9j31kw0zkkjl
0072Vf1pgy1foxk45w3toj31hc0u01al
0072Vf1pgy1foxlof9infj31hc0u0gz4
0072Vf1pgy1foxk3shjrpj31hc0u0k3d
7f0c40d4gy1fqbg4jwszkj21hc0u0q7z
7f0c40d4gy1fqbfh91nf9j21hc0u0q62
7f0c40d4gy1fqbf0e94b8j21hc0xc1ky
0072Vf1pgy1foxk7sny76j31hc0u0du7
0072Vf1pgy1foxlnr1pdmj31hc0u0h1c
0072Vf1pgy1foxkf0qyqbj31hc0u0h2u
0072Vf1pgy1fodqnt870uj31kf14ex6p
0060lm7Tly1fpx1skrabaj31hc0u01kx
0072Vf1pgy1foxk7k489xj31kw0w0e3a
0072Vf1pgy1foxki8yafwj31kw0w04oy
0072Vf1pgy1foxkjjaqimj31hc0u07jh
0072Vf1pgy1fodqnwsypsj31hc10qe82
0072Vf1pgy1foxkbwxfq0j31hc0u0h43
0072Vf1pgy1foxli7783pj31hc0u0n9g
0072Vf1pgy1foxk7272z8j31kw0w0h0a
0072Vf1pgy1foxkic3ld0j31hc0u07rx
0072Vf1pgy1foxlnuvhbej31hc0u0wuw
0072Vf1pgy1foxkexoreoj31hc0u0qn0
0072Vf1pgy1foxlnixk7ej31hc0u0ke6
0072Vf1pgy1foxkf55zbij31kw0w0e55
0072Vf1pgy1foxlnzmfnjj31kw0w0az5
0072Vf1pgy1foxliaky92j31kw0w0b16
0072Vf1pgy1foxkcriixpj31hc0u0wu8
0072Vf1pgy1foxkj2ashuj31kw0w0hcp
0072Vf1pgy1foxkd6lx6jj31kw0w0wyb
0072Vf1pgy1foxkfqehi6j31hc0u0184
0072Vf1pgy1foxkcsx9rmj31hc0u0h9k
0072Vf1pgy1foxk70rdinj31kw0w0b19
0072Vf1pgy1foxk7lqroij31kw0w0nkz
0072Vf1pgy1foxlobn7kfj31hc0u01db
0072Vf1pgy1fodqo2pcz9j31kw0zkkjl
0072Vf1pgy1foxk45w3toj31hc0u01al
0072Vf1pgy1foxlof9infj31hc0u0gz4
下面是主要部分,用代码编辑器新建一个php文件,复制如下内容并命名为tu.php
$str = explode("\n", file_get_contents('RandImg.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';
//解析结果
$result=array("code"=>"200","imgurl"=>"$url");
//Type Choose参数代码
$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;
//IMG
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;
}
$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';
//解析结果
$result=array("code"=>"200","imgurl"=>"$url");
//Type Choose参数代码
$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;
//IMG
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;
}
以上的php代码会随机查找RandImg.txt内里的内容,然后和接口地址拼接成一个完整的图片url,实现了随机挑选出一张图片显示出来。
调用方法:把上面的RandImg.txt文件和tu.php文件放在同一目录下 , 在你网站的header文件里面引入样式,如:background: url(/tu.php);
最后附上本站api演示地址:http://img.dujup.com/tu.php
Social Plugin