Learn how to differentiate 2 images using Imagick with PHP.

Nowadays there are a lot of advanced cross-platform image file Comparisong tools that can be easily used from any computer. If you're a PHP developer, you can achieve such a feature in your server using Imagick. In this article we'll show you how to differentiate the following 2 images (Image A and Image B) using Imagick in PHP:

Images to compare imagick

Note

To provide the path to the image, we use the getcwd function to retrieve the current working directory, because as you may know Imagick doesn't work properly with a relative path. Instead you should provide the absolute path to the image.

1. Comparing with secondary image on the background

As first example, we'll explain you how to differentiate 2 images in the most simple way. By default, the compareImages method will abstractly compose the image over the first one and will highlight automatically what doesn't match with the first image with red color.

<?php 

// Create an instance of every image to compare
$image1 = new Imagick(getcwd(). "/example_a.png");
$image2 = new Imagick(getcwd(). "/example_b.png");

// Compare the Images
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
// The first item of the result array is an Imagick Image object
// so convert it into its PNG format
$result[0]->setImageFormat("png");

// Output image to the browser
header("Content-Type: image/png");
echo $result[0];

The previous code would generate the following response on the browser:

Imagick compose differences and highlight

2. Retrieve transparent image with differences

In case that you can't appreciate the differences with the image generated by the basic example of Imagick, then you would like instead to draw the images over a new empty image instead. You can achieve this by relying on the logic of ImageMagick, specifically by using the compose argument in the command line. The Image Composition is the process of merging two (and only two) images in a variety of ways. From all the ways to compose an image with Imagick, we'll use one of the 'clearing' methods namely Src. You can add this option to the image using the setOption method. Using the lowlight-color option you can specify the background color of the result image and then read finally the original image (image 1) to compare with the another one. The result object can be used to retrieve the resulting image.

Check out the following example:

<?php 
// Create an empty Imagick object (to be able to set some options later)
$image1 = new Imagick(); 
// Create
$image2 = new Imagick(getcwd(). "/example_b.png");

// Set Lowlight (resulting background) to transparent, not "original image with a bit of opacity"
$image1->setOption('lowlight-color','transparent');

// Switch the default compose operator to Src, like in example
$image1->setOption('compose', 'Src');

// Now read the original image (image 1)
$image1->readImage(getcwd(). "/example_a.png");

// Result will not have a background
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);

$result[0]->setImageFormat("png");

// Output image to the browser
header("Content-Type: image/png");
echo $result[0];

The previous code would generate as output the following image:

Imagick differences images

In case of exception

Usually this process shouldn't fail if Imagick is correctly configured. In case you face the following exception with your code: Uncaught exception ImagickException Compare images failed, we recommend you to verify if the dimensions of the images, because if you compare 2 images whose size isn't the same will produce the error (normally it shouldn't happen as if the dimensions aren't the same the differentiation process should highlight that the images haven't the same dimensions).

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors