简体   繁体   English

缩略图生成时间

[英]Thumbnail generation time

Idea 理念
I have a function that checks to see if a thumbnail exists in cache folder for a particular image. 我有一个功能,检查特定图像的cache文件夹中是否存在缩略图。 If it does, it returns the path to that thumbnail. 如果是,则返回该缩略图的路径。 If it does not, it goes ahead and generates the thumbnail for the image, saves it in the cache folder and returns the path to it instead. 如果没有,则继续并生成图像的缩略图,将其保存在cache文件夹中并返回其路径。

Problem 问题
Let's say I have 10 images but only 7 of them have their thumbnails in the cache folder. 假设我有10张图片,但只有7张图片在cache文件夹中有缩略图。 Therefore, the function goes to generation of thumbnails for the rest 3 images. 因此,该功能用于生成其余3个图像的缩略图。 But while it does that, all I see is a blank, white loading page. 但是当它这样做时,我看到的只是一个空白的白色加载页面。 The idea is to display the thumbnails that are already generated and then generate the ones that do not exist. 我们的想法是显示已生成的缩略图,然后生成不存在的缩略图。

Code

$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) . "' />";
}

To elaborate on @DCoder's comment, what you could do is; 要详细说明@ DCoder的评论,你可以做的是;

  • If the thumb exists in the cache, return the URL just as you do now. 如果拇指存在于缓存中,请像现在一样返回URL。 This will make sure that thumbs that are in the cache will load quickly. 这将确保缓存中的拇指将快速加载。

  • If the thumb does not exist in the cache, return an URL similar to /cache/generatethumb.php?http://i49.tinypic.com/4t9a9w.jpg where the script generatethumb.php generates the thumbnail, saves it in the cache and returns the thumbnail. 如果缓存中存在拇指,则返回类似于/cache/generatethumb.php?http://i49.tinypic.com/4t9a9w.jpg的URL,其中脚本generatethumb.php生成缩略图,将其保存在缓存中并返回缩略图。 Next time, it will be in the cache and the URL won't go through the PHP script. 下次,它将在缓存中,URL将不会通过PHP脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM