This is the first time I use ImageMagick, and I am doing a small test just to see if it actually work for what I want it to work; however, I am getting the error 'compare images failed'. It is very possible that I am doing something wrong since I really don't know exactly how Imagick works. I am open to other suggestions, I just want to compare two images with very few changes. My testing code looks like:
<?php
$image1 = new Imagick();
$image2 = new Imagick();
$image1->readImage("export/image1.jpg");
$image2->readImage("export/image2.jpg");
$result = $image1->compareImages($image2, 1);
$result[0]->setimageFormat("jpg");
echo $result[1] . '\n';
?>
The error looks like this:
root@review1:/var/www/html# php export/image.php
PHP Fatal error: Uncaught exception 'ImagickException' with message 'Compare images failed' in /var/www/html/export/image.php:6
Stack trace:
#0 /var/www/html/export/image.php(6): Imagick->compareimages(Object(Imagick), 1)
#1 {main}
thrown in /var/www/html/export/image.php on line 6
For future users who are having the same issue, I got it to work. If you try to compare two images with different dimensions, this error will appear. All what I did to fix this was to re-size both images so that both have the same dimensions. In my case looks something like this:
$image1->resizeImage(400,400,Imagick::FILTER_LANCZOS, 1);
$image2->resizeImage(400,400,Imagick::FILTER_LANCZOS, 1);