4年前
实现自定义图片源做到随机图片效果,实现效果见最后
第一步
建立一个名称为api的文件夹,文件夹内新建一个index.php文件,记事本打开输入下面代码并保存:
<?php
//存有链接的文件名
$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"));
}第二步
再新建一个img.txt,即保存有图片源链接的文件,如下:

最后一步
最后将整个api文件夹上传到网站目录,打开之后就会出现随机图啦!
随机图片API:https://icu.36ednz.eu.org/usr/uploads/api/index.php (刷新页面看看效果吧!)
调用代码:
<img src="https://icu.36ednz.eu.org/usr/uploads/api/index.php" width="100%" height="auto" alt="apipicture" />