芝士一个为整蛊群友而生的随机图片程序
<?php
// 配置参数
$config = [
'image_folder' => __DIR__ . '/images/', // 使用绝对路径
'allowed_types' => ['jpg', 'jpeg', 'png', 'gif'],
'cache_time' => 300, // 5分钟
];
// 安全检查
if (!is_dir($config['image_folder']) || !is_readable($config['image_folder'])) {
header("HTTP/1.0 500 Internal Server Error");
die('图片目录不可用');
}
// 获取图片列表(带缓存)
$cacheFile = sys_get_temp_dir() . '/img_cache_' . md5($config['image_folder']);
if (file_exists($cacheFile) &&
(time() - filemtime($cacheFile)) < $config['cache_time']) {
$imageFiles = unserialize(file_get_contents($cacheFile));
} else {
$imageFiles = [];
foreach ($config['allowed_types'] as $ext) {
$imageFiles = array_merge(
$imageFiles,
glob($config['image_folder'] . '*.{' . $ext . ',' . strtoupper($ext) . '}', GLOB_BRACE)
);
}
file_put_contents($cacheFile, serialize($imageFiles));
}
// 检查是否有可用图片
if (empty($imageFiles)) {
header("HTTP/1.0 404 Not Found");
die('没有可用图片');
}
// 判断是否显示所有图片
if (isset($_GET['view_all'])) {
// 显示所有图片
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>所有彩音</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
.image-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 15px; }
.image-grid img { max-width: 100%; height: auto; border: 1px solid #ddd; border-radius: 5px; }
.back-button { margin-bottom: 20px; }
</style>
</head>
<body>
<div class="back-button">
<a href="?" style="text-decoration: none; color: #fff; background: #007bff; padding: 10px 15px; border-radius: 5px;">随机彩音</a>
</div>
<div class="image-grid">';
foreach ($imageFiles as $image) {
$imageUrl = 'images/' . basename($image);
echo '<img src="' . htmlspecialchars($imageUrl) . '" alt="一只彩音">';
}
echo '</div>
</body>
</html>';
exit;
}
// 随机选择图片
$randomImage = $imageFiles[array_rand($imageFiles)];
// 路径安全检查
$randomImage = realpath($randomImage);
if (strpos($randomImage, realpath($config['image_folder'])) !== 0) {
header("HTTP/1.0 403 Forbidden");
die('你干嘛嗨嗨哟');
}
// 获取MIME类型
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $randomImage);
finfo_close($finfo);
// 输出随机图片页面
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机彩音</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 20px; }
.random-image { max-width: 100%; height: auto; border: 1px solid #ddd; border-radius: 5px; }
.view-all-button { margin-top: 20px; }
.view-all-button a { text-decoration: none; color: #fff; background: #007bff; padding: 10px 15px; border-radius: 5px; }
</style>
</head>
<body>
<h1>随机彩音</h1>
<div>
<img src="data:' . $mimeType . ';base64,' . base64_encode(file_get_contents($randomImage)) . '" alt="随机图片" class="random-image">
</div>
<div class="view-all-button">
<a href="?view_all=1">查看所有彩音</a>
</div>
</body>
</html>';
exit;
?>
效果还不错,深受群友喜爱
可以展示所有图片,如果不需要可以去掉
顺便还有第一版的直接输出图片的版本,上面的是为了趣味性改的
<?php
// 配置参数
$config = [
'image_folder' => __DIR__ . '/images/', // 使用绝对路径
'allowed_types' => ['jpg', 'jpeg', 'png', 'gif'],
'cache_time' => 300, // 5分钟
];
// 安全检查
if (!is_dir($config['image_folder']) || !is_readable($config['image_folder'])) {
header("HTTP/1.0 500 Internal Server Error");
die('图片目录不可用');
}
// 获取图片列表(带缓存)
$cacheFile = sys_get_temp_dir() . '/img_cache_' . md5($config['image_folder']);
if (file_exists($cacheFile) &&
(time() - filemtime($cacheFile)) < $config['cache_time']) {
$imageFiles = unserialize(file_get_contents($cacheFile));
} else {
$imageFiles = [];
foreach ($config['allowed_types'] as $ext) {
$imageFiles = array_merge(
$imageFiles,
glob($config['image_folder'] . '*.{' . $ext . ',' . strtoupper($ext) . '}', GLOB_BRACE)
);
}
file_put_contents($cacheFile, serialize($imageFiles));
}
// 检查有效性
if (empty($imageFiles)) {
header("HTTP/1.0 404 Not Found");
die('没有可用图片');
}
// 随机选择图片
$randomImage = $imageFiles[array_rand($imageFiles)];
// 路径安全检查
$randomImage = realpath($randomImage);
if (strpos($randomImage, realpath($config['image_folder'])) !== 0) {
header("HTTP/1.0 403 Forbidden");
die('非法文件路径');
}
// 获取MIME类型
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $randomImage);
finfo_close($finfo);
// 输出图片
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . filesize($randomImage));
header('Cache-Control: max-age=0, no-cache, no-store');
header('Pragma: no-cache');
readfile($randomImage);
exit;
?>