把下面代码保存于 img
.php
文件中
随机图片 api
PHP
<?php
//读取文本 $str
=explode
("\n
",file_get_contents
('wpimg
.txt
')); //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
://ws
'.$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
; } ?>
同时创建一个 TXT
文本文件
txt
文本里面的格式只取新浪外链图片的红色部分,一行一个
如:https
://ws4
.sinaimg
.cn
/large
/65b17112ly1fv5ll42bsqj21hc0xc4b9.jpg
内容如下
65b17112ly1fv5ll42bsqj21hc0xc4b9 65b17112ly1fv5ll4a2ktj21hc0xcqbi 65b17112ly1fv5ll4i3jnj21hc0xcwo3 65b17112ly1fv5ll4syu4j21hc0xcame 65b17112ly1fv5ll52zw7j21hc0xc7gz 65b17112ly1fv5ll5a4bwj21hc0xcak3 65b17112ly1fv5ll5imk2j21hc0xcn9o
这些图片全是电脑主题里面提取出来的,经过我反复无损压缩后上传到新浪图床的。
之前发过一篇文章图片无损压缩工具大家自己可找图片制作自己喜欢的图片接口
转载请注明:汇站网 » WordPress
添加自制随机背景图像 API