| <?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; |
| |
| |
| $imageInfo = getimagesize($imagePath); |
| $mimeType = $imageInfo['mime']; |
| |
| |
| header('Content-Type: ' . $mimeType); |
| header('Content-Length: ' . filesize($imagePath)); |
| |
| |
| readfile($imagePath); |
| exit; |
| } else { |
| |
| header("HTTP/1.0 404 Not Found"); |
| echo 'No images found in the directory.'; |
| exit; |
| } |
| ?> |
|
|