Checking version information on ImageMagick in PHP

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Davvolun

Checking version information on ImageMagick in PHP

Post by Davvolun »

I'm getting a return value of '1' in php when running 'convert -version', which I don't understand. You can test the results with:

Code: Select all

<html><body>
<?php
$path="/usr/local/convert -version";

exec($path,$convert_version,$convert_return);

print( "<br />\n" );
print( "path: $path<br />\n" );
print( "convert_return: $convert_return<br />\n" );
print( "convert_version:<br />\n" );
foreach( $convert_version as $key ) {
  print( "  $key: $convert_version[$key]<br />\n" );
}
?>
</body></html>
The exact results are:
path: /usr/local/convert -version
convert_return: 1
convert_version:
Version: ImageMagick 6.4.9-2 2009-03-03 Q16 OpenMP http://www.imagemagick.org:
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC:
Clearly IM is being found and run correctly, but for some reason it returns a failure code (1) instead of success (0). For context, I'm using Omeka (http://omeka.org/) which is checking that IM exists before it tries to use it. Luckily for me, Omeka is open source, so I can modify the Omeka code to always 'return true' that IM is installed correctly. After that, IM and Omeka work perfectly together.

I think someone has a problem here; is it the value IM is returning when checking the version, or is it the manner in which Omeka checks if IM exists? I would think IM should return 'success' when it successfully performs what the user asked of it (i.e. printing the version information), you tell me... For reference, Omeka simply compares the $convert_return value to 0 to determine success/failure/existence-of-IM.

If a call to version is supposed to return failure, can you point me to where this is stated? Some sort of convention guidelines or something? I originally posted this on the Omeka forums, so if you tell me this isn't a bug with IM, I'm gonna go back and tell the Omeka people; I'll need support to explain why version printing should return failure.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Checking version information on ImageMagick in PHP

Post by Bonzo »

Try:

Code: Select all

<?php
echo "<pre>";
system("convert -version");  
echo "</pre>";
?> 
Your path should just be:

Code: Select all

$path = "/usr/local/convert";
My code runs Ok just using convert without the /usr/local/convert but depends on the server setup.
Davvolun

Re: Checking version information on ImageMagick in PHP

Post by Davvolun »

Yeah that works also..I should note that I didn't *need* to include the full path for my system configuration, it works fine for me just calling exec('convert -version') or system('convert -version'), I just thought it might be useful for whoever read my post here.

As for the rest, Omeka works fine (for my configuration, where I know IM is installed and works) if I replace their code:

Code: Select all

protected static function checkForImageMagick($path) {
    exec($path, $convert_version, $convert_return);
    return($convert_return == 0);
}
with

Code: Select all

protected static function checkForImageMagick($path){
  return True;
}
A better solution would of course be to check $convert_version (or the output from system($path)) for some sort of validity.

BUT, the point is, I don't understand why I should have to do that! This could be a failure on my part, but it's my understanding that if I request to get the version of IM, and IM successfully prints that information, then IM should return a 'success' code (in this case, a zero value). I can certainly see that there may be better ways to check IM for validity from the Omeka-programmers view, but I'm trying to figure out what's the right way to do this. Clearly someone from IM decided to return a value of 1 when IM prints the version information; I'm just trying to figure out where to go next.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Checking version information on ImageMagick in PHP

Post by Bonzo »

If I run your code on my localhost I get a return of :
path: /usr/local/convert -version
convert_return: 1
convert_version:
If I run this:

Code: Select all

<?php
exec("convert -version", $output1, $output2);  
echo "<pre>";
print_r( $output1 );
echo "</pre>";
echo $output2;
?> 
I get this returned:
Array
(
[0] => Version: ImageMagick 6.4.0 04/16/08 Q16 http://www.imagemagick.org
[1] => Copyright: Copyright (C) 1999-2008 ImageMagick Studio LLC
[2] =>
)

0
If I use system:

Code: Select all

<?php
echo "<pre>";
system("convert -version", $output);  
echo "</pre>";
echo $output;
?>
I get:
Version: ImageMagick 6.4.0 04/16/08 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2008 ImageMagick Studio LLC


0
Your code is the only one that returns a 1 not a 0
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Checking version information on ImageMagick in PHP

Post by Bonzo »

If I try this:

Code: Select all

<?php
echo "<pre>";
exec("convert -version 2>&1", $array); 
echo "<br>".print_r($array)."<br>";   
echo "</pre>";
?> 
The output is:
Array
(
[0] => Version: ImageMagick 6.4.0 04/16/08 Q16 http://www.imagemagick.org
[1] => Copyright: Copyright (C) 1999-2008 ImageMagick Studio LLC
[2] =>
)
1
If I use system instead of exec the output is:
Version: ImageMagick 6.4.0 04/16/08 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2008 ImageMagick Studio LLC

0
1
The o must be returned by the system comand and the 1 is contained in the array. So you get two differnt results returned, it depends which on you belive ?
Davvolun

Re: Checking version information on ImageMagick in PHP

Post by Davvolun »

Here's what I get:

Code: Select all

Version: ImageMagick 6.4.9-2 2009-03-03 Q16 OpenMP http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC

system: 1
return: 1


exec: 1
return: 1Array
(
    [0] => Version: ImageMagick 6.4.9-2 2009-03-03 Q16 OpenMP http://www.imagemagick.org
    [1] => Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC
    [2] => 
)

1

Array
(
    [0] => Version: ImageMagick 6.4.9-2 2009-03-03 Q16 OpenMP http://www.imagemagick.org
    [1] => Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC
    [2] => 
)

1
when executing

Code: Select all

echo "<pre>";
echo "system: " . system("convert -version 2>&1", $convert_return_sys);
echo "return: ".print($convert_return_sys)."<br/>";
echo "</pre>";

echo "<pre>";
echo "<br/>exec: " . exec("convert -version 2>&1", $array, $convert_return_exec);
echo "return: ".print($convert_return_exec)."<br/>";
echo "<br/>".print_r($array)."<br/>";
echo "</pre>";

exec("convert -version", $output1, $output2);
echo "<pre>";
print_r( $output1 );
echo "</pre>";
echo $output2;
Which would be a compilation of the last few code segments, all of which agree IM is returning 1 on my machine.

From my output, it appears that it doesn't matter for my system whether I use system or exec, and both the return value of exec/system and the return variable agree that the return is '1'. Obviously I'm using IM 6.4.9, whereas you're using 6.4.0, but if anything your results look less correct to me since the return values don't agree (if IM was actually changed from your version to mine, I would think mine would be the "correct" value since it's more consistent).

Still confused by this...
Post Reply