开发者

Create new JPG image thumbnail if original file has changed

I have a simple PHP script using imagecreatefromjpeg to create a thumbnail version of original and save it to a new folder. To speed parsing (all done in the back end prior to upload to static site) I am using file_exist before the creation to check it exists and show - if not create new and show. This works fine but if the original image changes the script does not generate as the thumbnail image exists in it's old form.

I guess I need to use 开发者_StackOverflow社区MD5 test but as a n00b not sure how to test if the resulting thumbnail version would be different from the existing as produced by the main image.

Current Logic:

if thumb exist == do nothing,
if thumb does not exist == create it

Additional logic

if thumb will change due to change in original == create it,
if thumb will stay the same == do nothing

The existing PHP is very poor and clobbered together so happy for any pointers from a fresh (expert) view.

Thanks, John


You want to check the time the file was modified.

http://php.net/manual/en/function.filemtime.php

if the original file's timestamp is newer than the thumbnail's timestamp, then make a new thumbnail.

Thanks guys - not sure if that will work if the original image was created previously but only just being used eg stock images that are on file elsewhere and then dragged into the /images/ folder to replace existing image but with the same filename.. does the timestamp show the date dragged or when it was created?

If you move the files, the modified timestamp does not change.

If you copy the files, the copies are created by the copy action.

When you create a file, its Date Modified timestamp is the same as its Date Created timestamp.

So if your drag is copying the files, then the modified timestamp is the timestamp of the moment that the files were copied.


It should be enough to compare the last modification date of the thumb and the original image. if the original image is newer than the thumb, you should recreate it.


In simple terms, say we have an image that is a 200x200 image filled with red and only red (so yeah a red square). We create 80x80 thumbnails. If the original image changes but is still just the same red filled square, the thumbnail will not change.

The thing is that in order to detect this you will need to create the thumbnail or apply advanced image comparison techniques, that you may as well save it.

You can however use MD5 to create a one-on-one map from original to thumbnail, by naming the thumbnails as the MD5 hash of the original. The drawback of this approach is that you will need a way to remove unused thumbnails when their originals are no longer used.

To use this approach use something like:

<?php
$thumbname = md5_file($bigimage_path);
if( !file_exists("images/thumbs/$thumbname.jpg") )
{
    create_thumb($bigimage_path, $thumbname);
}
?>

This will accomplish that if you have the exact same big image as different filenames, they will all map to the same thumbnail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜