在网上copy的PHP代码自己改了改,通过GET请求当class = 1或者没带class参数时返回宽图,当class=2时返回长图。
图片链接由本地的txt文件来存放。
<?php
$imgClass = $_GET['class'];
if ($imgClass == null | $imgClass == 1) {
// class=1代表宽图
// 1.读取imgurl.txt中的内容,并以换行符分开
$str = explode("\n", file_get_contents('imgurl.txt'));
}else if($imgClass == 2){
$str = explode("\n", file_get_contents('img1url.txt'));
}
// 2.得到的$str是一个String的数组,然后获取随机数index
$rand_index = rand(0,count($str)-1);
// 根据生成的随机数选取index为$rand_index的图片链接
$url = $str[$rand_index];
// 替换掉转义
$url = str_re($url);
// 3.重定向到目标url,返回302码,然后浏览器就会跳转到图片url的地址
header("Location:".$url);
// 替换掉一些换行、制表符等转义
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;
}
?>