<?php
error_reporting(0);
// 获取图片文件列表
$img_array = glob("photos/*.{gif,jpg,jpeg,png,webp}", GLOB_BRACE);
if (empty($img_array)) {
http_response_code(404);
die('No images found');
}
$img_index = array_rand($img_array);
$img_path = $img_array[$img_index];
if (!file_exists($img_path)) {
http_response_code(404);
die('Image not found');
}
$file_info = pathinfo($img_path);
$file_size = filesize($img_path);
$mime_types = [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp'
];
$extension = strtolower($file_info['extension']);
$mime_type = isset($mime_types[$extension]) ? $mime_types[$extension] : 'application/octet-stream';
header('Content-Type: ' . $mime_type);
header('Content-Length: ' . $file_size);
header('Cache-Control: public, max-age=3600'); // 缓存1小时
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($img_path)) . ' GMT');
readfile($img_path);
exit();
?>