[英]Thumbnail generation time
理念
我有一个功能,检查特定图像的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) . "' />";
}
要详细说明@ DCoder的评论,你可以做的是;
如果拇指存在于缓存中,请像现在一样返回URL。 这将确保缓存中的拇指将快速加载。
如果缓存中不存在拇指,则返回类似于/cache/generatethumb.php?http://i49.tinypic.com/4t9a9w.jpg
的URL,其中脚本generatethumb.php
生成缩略图,将其保存在缓存中并返回缩略图。 下次,它将在缓存中,URL将不会通过PHP脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.