Files
MoYuBan/api/picture.php
2025-11-18 14:18:28 +08:00

71 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// 图片代理随机或指定来源heisi/wapmeinvpic/baisi/meinvpic返回 { url, source }
header('Content-Type: application/json; charset=utf-8');
$type = strtolower($_GET['type'] ?? '');
$map = [
'heisi' => 'https://api.iyuns.com/api/heisi',
'wapmeinvpic' => 'https://api.iyuns.com/api/wapmeinvpic',
'baisi' => 'https://api.iyuns.com/api/baisi',
'meinvpic' => 'https://api.iyuns.com/api/meinvpic',
'random4kPic' => 'https://api.iyuns.com/api/random4kPic',
'jk' => 'https://api.iyuns.com/api/jk',
];
if (!$type || !isset($map[$type])) {
$keys = array_keys($map);
$type = $keys[array_rand($keys)];
}
$endpoint = $map[$type];
function fetch_json($url) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 8,
CURLOPT_TIMEOUT => 12,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_USERAGENT => 'PictureProxy/1.0',
]);
$body = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) { return null; }
$json = json_decode($body, true);
return is_array($json) ? $json : null;
}
$resp = fetch_json($endpoint);
$url = '';
if ($resp && ($resp['code'] ?? 0) == 200) {
$url = preg_replace('/[`\s]/', '', (string)($resp['data'] ?? ''));
}
if (!$url) {
// 远程失败时,回退到本地随机图片(遍历 pictures/images 下的所有图片)
$root = __DIR__ . '/../pictures/images';
$candidates = [];
if (is_dir($root)) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));
foreach ($rii as $file) {
if ($file->isFile()) {
$ext = strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
if (in_array($ext, ['jpg','jpeg','png','gif','webp'])) {
$rel = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $file->getPathname()));
$candidates[] = $rel;
}
}
}
if (!empty($candidates)) {
shuffle($candidates);
$url = $candidates[0];
$type = 'local';
}
}
// 如果本地也没有,则后备一张 Unsplash 图片
if (!$url) {
$url = 'https://images.unsplash.com/photo-1523206489230-c012c4783a90?w=1200&h=800&fit=crop';
}
}
echo json_encode(['url' => $url, 'source' => $type], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);