3

I am trying to validate while putting images in my server

1.If the image names are different but the image is same then do nothing.

How can I detect that

My code looks something like this

        if (file_exists($ficherof)) {
            $x=file_get_contents($ficherof) ;
            if($imagen['sha1']!=sha1($x))
            {
                //do something

            }
        }
        else
//here I have to check if the file name is different but the files are same then donot upload
        file_put_contents($ficherof, $contenido);
Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130

1 Answers1

5

Checking the sha sum of two images in order to compare them is NOT correct! You can have two identical files with different sha sums - because the date/time can be embedded in the EXIF data, or the PNG headers, or the TIFF tags.

The way to do it correctly is in my answer... here

Watch this. I create 2 IDENTICAL black squares and compare them:

convert -size 256x256 xc:black 1.png
convert -size 256x256 xc:black 2.png
diff [12].png
Binary files 1.png and 2.png differ

Why are they different? Because the date is in them:

identify -verbose 1.png 2.png | grep date
date:create: 2015-04-03T13:31:30+01:00
date:modify: 2015-04-03T13:31:30+01:00
date:create: 2015-04-03T13:31:33+01:00
date:modify: 2015-04-03T13:31:33+01:00

What if I create a 256x256 black square GIF and an identical 256x256 black PNG?

convert -size 256x256 xc:black 1.png
convert -size 256x256 xc:black 1.gif

and look at them, they are different sizes, and will clearly have different checksums

ls -l 1*
-rw-r--r--  1 mark  staff  392  3 Apr 13:34 1.gif 
-rw-r--r--  1 mark  staff  279  3 Apr 13:34 1.png

but ImageMagick correctly tells me there are ZERO pixels different

convert -metric ae 1.png 1.gif -compare -format "%[distortion]" info:
0

Of course, if you compare sha sums and they are identical then the images are the same. All I am saying is that the images may be identical even though their checksums differ.

Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432