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.


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.


$md5image1 = md5(file_get_contents($image1));
$md5image2 = md5(file_get_contents($image2));
if ($md5image1 == $md5image2) {

}

libpuzzle is a PHP extension that can compare images.


A similar question was asked on this Stackoverflow thread and I had developed something for my own use. Posting it here so that it may help others.

It takes two (or more images) and gives you options about checking the difference between them. Options like resolution to use, and strictness.

I wrote a more comprehensive blogpost on it as well.


Image Comparison Function in PHP with GD Library http://www.robert-lerner.com/imagecompare.php