简体   繁体   中英

How can I understand if Image from URL has been changed?

So, I get lots of image urls and process them into my blob storage and map it my CDN. Now, the problem is, sometimes images are updated in original url but url is not changed. For example:

For first image:

Original ImageUrl: "<websiteUrl>/image1.jpg";
My BlobUrl: "<bloburl>/myimage1.jpg";
My CDN Url: "<cdnurl>/myimage1.jpg

For update image:

orignal imageurl is same, so I cannot check if image url is updated or not. I thought I could check exif datetime property but unfortunately those images does not datetime property in the exif. Only information I have in exif is:

File Size 236 kB 
File Type JPEG 
MIME Type image/jpeg 
Image Width 1000 
Image Height 1500 
Encoding Process Baseline DCT, Huffman coding
Bits Per Sample 8 
Color Components 3
X Resolution 72 
Y Resolution72 
YCbCr Sub Sampling YCbCr4:2:0 (2 2)

My question is, how can I detect if image from the url is different to my image in my blob in this case.

In the first instance you should check the LastModified date of the file itself, that will tell you whether the file has actually changed since the last time you accessed it. Then simply reload the file if it's newer than the date you have stored. You could also check the size of the file, if it's not the same as it was before it's almost certainly changed.

However, if that information isn't available to you the only way to do this is to compare the pixels of the image to see if they're different.

However, you don't have to do pixel by pixel comparison. If you generate a checksum from the image when you first load it and store that in your database alongside the url. You can then repeat the process when you next need to display the image and compare the stored checksum against the newly calculated one. If they are different then the images are different.

It does mean that you have to retrieve the image each time whether it's been changed or not.

You should create a checksum for your file and store that with it. Then, you can easilly calculate a new checksum to see if it has changed:

using (var md5 = MD5.Create())
{
    using (var stream = File.OpenRead(filename))
    {
        return md5.ComputeHash(stream);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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