How to detect change in Source image
I currently have some image processing scripts I wrote in PHP which I use for CMS websites. One of them takes an source image which the client has uploaded, resizes it, writes out the processed image with a specific filename (eg 100x100_id240.jpg for a 100 x 100 thumbnail for page id 240) and most importantly alters the timestamp on 开发者_开发问答the new processed image to match the source image.
Each time the page is requested by the browser the php script checks the timestamp on both the source and processed image to make sure they match - if they don't then the client must have changed the source image so the processing is repeated and the new version overwrites 100x100_id240.jpg. If the timestamps do match then we don't need to reprocess the images so we just use the already processes thumbnail.
This works great. However, every now and then I get a client who uploads a pile of images with exactly the same timestamp, to 100th of a second. If then then alter the source image for a page to an alternate image with the same timestamp the php script go merrily on it way and compares the source images timestamp to the processed image timestamp, finds the timestamps are the same and so do not reprocess the new source image. We then get an incorrect thumbnail output.
I have thought of perhaps writing a random timestamp to the source image on upload, which would solve the problem. But I would rather preserve this stamp. So I need another method (which is not heavy on processing, like counting all the pixels in the source image which are red for example). I wondered if I could write a unique code to exif data or something. But exif is only for jpg right? Any ideas on how to solve this? Thanks
You can use md5_file or sha1_file to check if the file is same.
You should try to compute the CRC32 of the image and compare it. This way you can know if the image was altered by someone else.
精彩评论