随机图片api网站
1.樱花二次元图片API-Dmoe:https://www.dmoe.cc/
示例
2.夏沫博客:https://cdn.seovx.com/
示例
3.保罗 API:https://api.paugram.com/
示例
4.岁月小筑:https://img.xjh.me/
示例
5.东方Project随机图片API:https://img.paulzzh.com/
示例
6.缙哥哥博客:https://api.dujin.org/
示例
7.栗次元API:https://t.alcy.cc/
示例
8.LoliAPI:https://www.loliapi.com/
示例
9.likepoems随机图:https://api.likepoems.com/
示例
10.zatu的自建API:https://api.zatusgyo.top/
示例
11.樱道(已失效):https://api.r10086.com/
12.小歪(已失效):https://api.ixiaowai.cn/
13.墨天逸(已失效):https://api.mtyqx.cn/
自建API
如何自建?
<?php
//存放api随机图链接的文件名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"));
}
?>
转自likepoems由于需要一个一个写入链接,有些麻烦,所以进行了调整:
<?php
// 定义图片文件夹路径和缓存文件路径
$imagesDir = 'img/';
$cacheFile = 'img.txt';
// 检查并更新缓存文件
if (shouldUpdateCache($imagesDir, $cacheFile)) {
updateCacheFile($imagesDir, $cacheFile);
}
// 从缓存文件中获取图片链接
$pics = readCacheFile($cacheFile);
// 检查是否有图片链接
if (empty($pics)) {
die('没有图片链接');
}
// 从数组中随机获取链接
$pic = $pics[array_rand($pics)];
// 根据请求参数返回指定格式
$type = isset($_GET['type']) ? $_GET['type'] : 'default';
switch ($type) {
// JSON返回
case 'json':
header('Content-Type: application/json');
die(json_encode(['pic' => $pic]));
// 默认返回重定向
default:
header("Location: $pic");
exit;
}
//缓存文件的时间戳检查
function shouldUpdateCache($imagesDir, $cacheFile)
{
if (!file_exists($cacheFile)) {
return true;
}
$cacheTime = filemtime($cacheFile);
foreach (glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $image) {
if (filemtime($image) > $cacheTime) {
return true;
}
}
return false;
}
function updateCacheFile($imagesDir, $cacheFile)
{
// 获取文件夹中的所有图片文件名
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
// 检查是否有图片
if (empty($images)) {
throw new Exception('没有找到任何图片');
}
// 构建图片的完整URL
$baseUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/';
$imageUrls = array_map(function($image) use ($baseUrl) {
return $baseUrl . ltrim($image, '/');
}, $images);
// 写入缓存文件
file_put_contents($cacheFile, implode(PHP_EOL, $imageUrls));
}
function readCacheFile($cacheFile)
{
// 检查文件是否存在
if (!file_exists($cacheFile)) {
throw new Exception('缓存文件不存在');
}
// 读取文件内容
$lines = file($cacheFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// 检查是否有图片链接
if (empty($lines)) {
throw new Exception('缓存文件中没有图片链接');
}
return $lines;
}
?>
或者使用我最开始使用的:
<?php
// 图片存放目录
$image_directory = 'pathto/pe/';
// 缓存文件路径
$cache_directory = 'pathto/cache';
$cache_file = $cache_directory . '/image_cache.txt';
// 检查缓存目录是否存在,如果不存在则创建
if (!file_exists($cache_directory)) {
mkdir($cache_directory, 0775, true);
}
// 检查缓存文件是否存在并读取图片列表
if (file_exists($cache_file)) {
$files = unserialize(file_get_contents($cache_file));
} else {
// 获取目录下的所有图片文件
$files = glob($image_directory . '*.{jpg,jpeg,png,gif,webp}', GLOB_BRACE);
// 将图片列表序列化并写入缓存文件
file_put_contents($cache_file, serialize($files));
}
// 检查是否有图片文件
if (!empty($files)) {
// 随机选择一个图片文件
$random_image = $files[array_rand($files)];
// 获取图片文件名
$image_name = basename($random_image);
// 构造图片的完整URL
$image_url = '你的url路径' . $image_name;
// 发送302重定向响应
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
header("Location: $image_url", true, 302);
exit;
} else {
echo "No images found.";
}
?>
