前言

有部分博客文章封面说所以随机图,但是有一些入是肯定不知道怎么制作随机图,那么,这期就是一个教程

示例网站

api.mbd234.net

源码部分

你可以创建一个.php文件,例如index.php,然后输入以下内容

<?php
//存有图片链接的文件名img.txt
$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,如果你有图片的url按照下面示例的格式填写

https://free-img.mofashi.ltd/5/2025/03/28/67e5dbd8cadca.webp
https://free-img.mofashi.ltd/5/2025/03/28/67e5dbd8a384f.webp
https://free-img.mofashi.ltd/5/2025/03/28/67e5dbd91b4bd.webp
https://free-img.mofashi.ltd/5/2025/03/28/67e5dbda7a5fe.webp
https://free-img.mofashi.ltd/5/2025/03/28/67e5dbda470e4.webp

不需要,和' "这些

那么这样就制作了个随机图