Idea
我有一个函数,用于检查特定图像的cache文件夹中是否存在缩略图。如果是的话,它将返回到该缩略图的路径。如果没有,则继续生成图像的缩略图,将其保存在cache文件夹中,然后返回到它的路径。
问题
假设我有10张图片,但其中只有7张在cache文件夹中有缩略图。因此,该功能将用于生成其余3幅图像的缩略图。但是当它这样做的时候,我看到的只是一个空白的,白色的加载页面。其思想是显示已经生成的缩略图,然后生成不存在的缩略图。
码
$images = array(
"http://i49.tinypic.com/4t9a9w.jpg",
"http://i.imgur.com/p2S1n.jpg",
"http://i49.tinypic.com/l9tow.jpg",
"http://i45.tinypic.com/10di4q1.jpg",
"http://i.imgur.com/PnefW.jpg",
"http://i.imgur.com/EqakI.jpg",
"http://i46.tinypic.com/102tl09.jpg",
"http://i47.tinypic.com/2rnx6ic.jpg",
"http://i50.tinypic.com/2ykc2gn.jpg",
"http://i50.tinypic.com/2eewr3p.jpg"
);
function get_name($source) {
$name = explode("/", $source);
$name = end($name);
return $name;
}
function get_thumbnail($image) {
$image_name = get_name($image);
if(file_exists("cache/{$image_name}")) {
return "cache/{$image_name}";
} else {
list($width, $height) = getimagesize($image);
$thumb = imagecreatefromjpeg($image);
if($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallest_side = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallest_side = $width;
}
$thumb_size = 200;
$thumb_image = imagecreatetruecolor($thumb_size, $thumb_size);
imagecopyresampled($thumb_image, $thumb, 0, 0, $x, $y, $thumb_size, $thumb_size, $smallest_side, $smallest_side);
imagejpeg($thumb_image, "cache/{$image_name}");
return "cache/{$image_name}";
}
}
foreach($images as $image) {
echo "<img src='" . get_thumbnail($image) . "' />";
}发布于 2013-02-03 15:05:56
要详细说明@DCoder的评论,您可以做的是;
/cache/generatethumb.php?http://i49.tinypic.com/4t9a9w.jpg的URL,其中脚本generatethumb.php生成缩略图,将其保存在缓存中并返回缩略图。下一次,它将在缓存中,URL将不会通过PHP脚本。https://stackoverflow.com/questions/14673802
复制相似问题