MattM
MattM

Reputation: 3217

Compare two images with ImageMagick

I am trying to take two images and compare them using ImageMagick.

I want to find out if the images are close to the same and return a number to be stored in a variable in php and displayed in my browser screen.

My hosting account is with bluehost and I have tested by resizing an image and ImageMagick is installed and running. The following code works here: http://zing-cards.com/imagetest2.php

<?php
$photo="/home/tcsdesig/public_html/zing/flower.jpg";
$cmd = "/usr/bin/convert $photo -thumbnail 100x100 JPG:-";

header("Content-type: image/jpeg");
passthru($cmd, $retval);

?>

I understand the the following command line code (according to this post) is supposed to get the results in a number format:

compare -metric RMSE first.png second.png NULL:

Here is my code so far:

<?php
$photoPath1="/home/*username*/public_html/flower.jpg";
$photoPath2="/home/*username*/public_html/flower1.jpg";

$cmd = "/usr/bin/compare -metric RMSE $photoPath1 $photoPath2 NULL:";
exec($cmd);
?>

How do I make this value show in my browser?

Other than the above code I have tried:

$result = exec($cmd);
echo $result;

From what I understand this should display a low number (since the images are equal) and if I use completely different images they should display a high number but I don't get any results.

Upvotes: 1

Views: 4519

Answers (1)

MarcoS
MarcoS

Reputation: 17711

Here if I do

<?php
    $photoPath1="/home/*username*/public_html/flower.jpg";
    $photoPath2="/home/*username*/public_html/flower1.jpg";

    $result = exec("/usr/bin/compare -metric RMSE /usr/share/pixmaps/faces/dice.jpg /usr/share/pixmaps/faces/fish.jpg NULL:");
    echo $result;
?>

I get

20455.4 (0.312129)

Which is what I expect, because the images are completely different...

I suppose you should try compare on the command-line, beforehand...

For example, if you don't have compare installed on your system, you don't get any results...

UPDATE: To further investigate in case of persistent lack of output, please do:

<?php
    $photoPath1 = "/home/*username*/public_html/flower.jpg";
    $photoPath2 = "/home/*username*/public_html/flower1.jpg";

    $compareCommand = "/usr/bin/compare";
    if (!is_executable($compareCommand)) die("Sorry, can't execute $compareCommand!");
    $output = shell_exec("$compareCommand -metric RMSE $photoPath1 $photoPath2 NULL: 2>&1");
    print $output;
?>

UPDATE 2

Answering OP question in comment:

In the convert man page it's not specified if and how it's possible to print only the RMSE error percentage...

You could add (for example) a | sed -e 's/.*(\(.*\))/\1/g' to the command, to just keep RMSE error percentage...

Upvotes: 2

Related Questions