1

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.

1 Answer 1

2

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...

Sign up to request clarification or add additional context in comments.

6 Comments

I simplified my code: It is here: zing-cards.com/imagecompare.php <?php echo "<h1>Compare Images</h1>"; $result = exec("/usr/bin/compare -metric RMSE /home/*username*/public_html/zing/flower.jpg /home/*username*/public_html/zing/woman1.png NULL:"); echo "Result:" . $result; ?>
Are you using Bluehost? Is there anything I have to do to install or activate compare? As I said the convert command works. I just get a blank, no result. Is there a way to make imagemagick output an error code?
I don't have imagick installed: zing-cards.com/imagick.php could that be a problem? This page is coded like this: <?php if (!extension_loaded('imagick')) echo 'imagick not installed'; ?>
Bluehost says that I am getting my data before I load my content type. How do I get around this?
header("Content-type: text/html"); $result = exec("/usr/bin/compare -metric RMSE /home/tcsdesig/public_html/zing/flower.jpg /home/tcsdesig/public_html/zing/woman1.png NULL:"); echo $result;
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.