Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare 2 images in php

Comparing 2 images to see if they are both the same files are easy, threw the files MD5, but is it possible or even plausible to determine if 2 images are same by using PHP GD to get the difference of the two images. If we where to get the difference of the two, and it was all white (id assume white or even black), then we would now know its both the same photo?

Also side note: id like to know if its possible to get 2 images of equal size to create an onion skin effect, 50% transparency on 1 and 50% on the other.

like image 682
Joseph Avatar asked Jul 17 '10 09:07

Joseph


People also ask

How do you compare two images for similarity?

The similarity of the two images is detected using the package “imagehash”. If two images are identical or almost identical, the imagehash difference will be 0. Two images are more similar if the imagehash difference is closer to 0.


2 Answers

Most of the other answers refer to using various hashing functions. The question explicitly is asking about comparing the contents of the images, not about comparing the files.

This means you end up having to actually understand the contents of the image. In PHP there are two extensions often used for this, ImageMagick and GD.

ImageMagick offers various tools you can use for this, through the PHP ImageMagick extension.

http://www.php.net/manual/en/function.imagick-compareimages.php

Biggest problem is that the documentation for that library is pretty much non-existing, so there will be a lot of trial-and-error involved. The PHP extension is a pretty thin wrapper around the ImageMagick library, so details of how the compareimages() function behaves can be found in the ImageMagick documentation.

like image 155
kander Avatar answered Oct 06 '22 15:10

kander


$md5image1 = md5(file_get_contents($image1)); $md5image2 = md5(file_get_contents($image2)); if ($md5image1 == $md5image2) {  } 
like image 33
Hamada Mido Avatar answered Oct 06 '22 14:10

Hamada Mido