步骤

1、 在网站目录下新建一个img.php,填入以下内容

<?php
// 获取分类,数量,返回类型
$sort = !empty($_GET['sort']) ? $_GET['sort'] : 'all';
$num = !empty($_GET['num']) ? $_GET['num'] : 1;
$num = $num > 100 ? 100 : $num;
$type = !($num > 1) ? $_GET['type'] : 'json';
$urls = [];

// 是否为空判断
if (!file_exists('' . $sort . '.txt')) {
    die('sort为空或文件不存在');
}

// 获取链接数组
$links = file_get_contents('' . $sort . '.txt');
$links = explode(PHP_EOL, $links);
$links = array_diff($links, ['']);
$links = array_values($links);

// 循环随机链接
$i = 0;
do {
    $url = $links[array_rand($links)];
    array_push($urls, $url);
    ++$i;
} while ($i < $num);

// 返回值
switch ($type) {
    case 'json':
        header('Content-type:text/json');
        die(json_encode(['pics' => $urls], JSON_PRETTY_PRINT));

    default:
        header('Location: ' . $url);
}

2、 新建一个(或多个)文本文档如all.txt,pc.txt,mp.txt来存放不同类型的图片
3、 将图片的直链放入txt文件内,每行一个,如

https://tvax4.sinaimg.cn/large/ec43126fgy1go7lbiz6dqj20hf0ol7cx.jpg
https://tvax4.sinaimg.cn/large/ec43126fgy1go7lbb4ugqj20uk17mqv5.jpg
https://tvax2.sinaimg.cn/large/ec43126fgy1go7lbbvgzaj20o40zm7cm.jpg
https://tvax1.sinaimg.cn/large/ec43126fgy1go7lbbjqj3j215o1ltqv5.jpg
https://tvax4.sinaimg.cn/large/ec43126fgy1go7lb9xm81j21k827ib2c.jpg
https://tvax4.sinaimg.cn/large/ec43126fgy1go7lbeosw9j20fw0p04im.jpg
https://tvax1.sinaimg.cn/large/ec43126fgy1go7lbao7umj20xc1ame82.jpg
https://tvax4.sinaimg.cn/large/ec43126fgy1go7lb2t2q3j20jr0rsn1x.jpg
https://tvax2.sinaimg.cn/large/ec43126fgy1go7lbhjc5jj24262vchdv.jpg
https://tvax2.sinaimg.cn/large/ec43126fgy1go7lbia5lyj20rs13yx28.jpg
https://tvax2.sinaimg.cn/large/ec43126fgy1go7lb89xpqj20m80vfnlo.jpg
https://tvax3.sinaimg.cn/large/ec43126fgy1go7lbecthjj20qk1b8kjl.jpg
https://tvax1.sinaimg.cn/large/ec43126fgy1go7lbgoqchj20rs13b4bp.jpg
https://tvax1.sinaimg.cn/large/ec43126fgy1go7lbh0ud7j20im0p0azn.jpg
https://tvax3.sinaimg.cn/large/ec43126fgy1go7lb8x9poj20jg0rindc.jpg
https://tvax1.sinaimg.cn/large/ec43126fgy1go7lbfup09j20rs17way0.jpg
https://tvax3.sinaimg.cn/large/ec43126fgy1go7lbf51o4j21vs1dpx6p.jpg

4、 访问http://你的域名或IP/img.php来查看效果

这里代码中提到三个参数,

  • sort 图片的类型,其值对应你txt文件的主文件名,如上文中的all,mp,pc,若不输入则为默认值,代码中默认值为all,可自行修改
  • size 图片的大小,新浪图床带有的参数,可选值见代码,默认为large
  • type 返回的格式,可选值json,默认直接302跳转

访问示例:

至此大功告成!

最后修改:2023 年 01 月 03 日