自用随机图片API
首先
目录结构如下
index/ (根目录)
├── img.php
└── img/
├── anime_pc/ (二次元横向)
├── anime_pe/ (二次元纵向)
├── photo/ (摄影)
├── nature/ (自然)
└── abstract/ (抽象)其次
用PHP写了一个简单的生成随机图片链接的接口:
<?php
// 1. 获取前端传递的分类参数 (默认为空)
// 例如: api.php?type=nature
$type = isset($_GET['type']) ? $_GET['type'] : '';
// 2. 获取返回格式 (默认直接跳转,可选 json)
// 例如: api.php?type=nature&format=json
$format = isset($_GET['format']) ? $_GET['format'] : 'redirect';
// 3. 定义图片根目录
$base_dir = "img/";
// 4. 定义允许的分类标签 (白名单,对应你的文件夹名)
// 二次元横向, 二次元纵向, 摄影, 自然, 抽象
$allowed_tags = [
'anime_pc',
'anime_pe',
'photo',
'nature',
'abstract'
];
// 5. 构建查找路径
if (!empty($type) && in_array($type, $allowed_tags)) {
// 如果指定了且在白名单内,只扫描特定文件夹
$path = $base_dir . $type . "/*.{gif,jpg,png,jpeg,webp}";
} else {
// 如果没有指定或指定错误,扫描 img 下所有子文件夹中的图片
// {*,*/*} 表示扫描当前目录及下一级子目录
$path = $base_dir . "{*,*/*}.{gif,jpg,png,jpeg,webp}";
}
// 6. 获取图片列表
$img_array = glob($path, GLOB_BRACE);
// 7. 错误处理:如果没有找到图片
if (empty($img_array)) {
die("Error: No images found in this category.");
}
// 8. 随机选取一张
$img_index = array_rand($img_array);
$selected_img = $img_array[$img_index];
// 9. 处理图片路径
// 如果图片在子文件夹中,glob 返回的路径已经包含文件夹名
// 我们需要确保返回的是完整的 URL(取决于你的服务器配置,这里给相对路径通常也能跳转)
// 为了更稳健,可以拼接当前域名,或者直接输出相对路径
$image_url = $selected_img;
// 10. 输出结果
if ($format === 'json') {
// JSON 模式:适合给前端 AJAX 调用
header('Content-Type: application/json');
echo json_encode([
'code' => 200,
'type' => $type ?: 'random',
'url' => $image_url
]);
} else {
// 默认模式:直接 302 跳转显示图片
// 增加 No-Cache 头,防止浏览器缓存随机结果
header("Cache-Control: no-cache, must-revalidate");
header("Location: " . $image_url);
}
?>最后
写一个HTML面板方便日后调用
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Starskyz API 文档</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-4 md:p-10 text-sm md:text-base">
<div class="max-w-3xl mx-auto space-y-8">
<section class="bg-white shadow-sm rounded-xl overflow-hidden border border-gray-200">
<div class="bg-slate-800 p-4">
<h2 class="text-white font-bold"># 随机图片 API (img.php)</h2>
</div>
<div class="p-6 space-y-6">
<div>
<p>基础链接</p>
<p class="text-gray-400 mb-1">https://myapi.starskyz.top/img.php</code>
<p>JSON功能支持 format=json</p>
<p class="text-gray-400 mb-1">例如 https://myapi.starskyz.top/img.php?type=nature&format=json 返回图片的URL字符串</p>
<p>已开启防盗链并禁用Referer为空的请求!</p>
</div>
<div class="grid grid-cols-1 gap-4">
<div class="p-3 bg-gray-50 rounded border-l-2 border-gray-300">
<span class="font-bold">
全分类随机
</span>
<code class="text-gray-600">
直接访问
</code>
</div>
<div class="p-3 bg-gray-50 rounded border-l-2 border-gray-300">
<span class="font-bold">
二次元桌面端
</span>
<code class="text-gray-600">
https://myapi.starskyz.top/img.php?type=anime_pc
</code>
</div>
<div class="p-3 bg-gray-50 rounded border-l-2 border-gray-300">
<span class="font-bold">
二次元移动端
</span>
<code class="text-gray-600">
https://myapi.starskyz.top/img.php?type=anime_pe
</code>
</div>
<div class="p-3 bg-gray-50 rounded border-l-2 border-gray-300">
<span class="font-bold">
摄影
</span>
<code class="text-gray-600">
https://myapi.starskyz.top/img.php?type=photo
</code>
</div>
<div class="p-3 bg-gray-50 rounded border-l-2 border-gray-300">
<span class="font-bold">
自然
</span>
<code class="text-gray-600">
https://myapi.starskyz.top/img.php?type=nature
</code>
</div>
<div class="p-3 bg-gray-50 rounded border-l-2 border-gray-300">
<span class="font-bold">
抽象
</span>
<code class="text-gray-600">
https://myapi.starskyz.top/img.php?type=abstract
</code>
</div>
</div>
</div>
</section>
</div>
<footer class="mt-10 text-center text-gray-400 text-xs">
© 2026 Starskyz API Documentation
</footer>
</body>
</html>结语
主体代码由Gemini3Pro生成,本人仅作稍微修改,如有差错欢迎指正。
由于小站资金有限,为防止恶意流量对此图片资源开启了防盗链功能。如有不便,敬请谅解。
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 StarSkyZ
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果

