File size: 1,278 Bytes
dc45f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
$directory = 'img';
$images = [];

// 支持的图片扩展名
$imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'tiff', 'svg'];

// 打开目录并读取图片文件
if (is_dir($directory)) {
    if ($dh = opendir($directory)) {
        while (($file = readdir($dh)) !== false) {
            $filePath = $directory . '/' . $file;
            // 获取文件扩展名并检查是否为图片
            $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
            if (is_file($filePath) && in_array($fileExtension, $imageExtensions)) {
                $images[] = $file;
            }
        }
        closedir($dh);
    }
}

// 随机选择一张图片
if (!empty($images)) {
    $image = $images[array_rand($images)];
    $imagePath = $directory . '/' . $image;
    
    // 获取图片的MIME类型
    $imageInfo = getimagesize($imagePath);
    $mimeType = $imageInfo['mime'];
    
    // 设置HTTP头部
    header('Content-Type: ' . $mimeType);
    header('Content-Length: ' . filesize($imagePath));
    
    // 输出图片内容
    readfile($imagePath);
    exit;
} else {
    // 如果没有找到图片,则返回404错误
    header("HTTP/1.0 404 Not Found");
    echo 'No images found in the directory.';
    exit;
}
?>