Get ImageMagick version with PHP

Posted in Programming  
E-Mail This Post/Page   

Knowing what version of ImageMagick is usually important if you're going to be shelling to it to perform tasks. For me it was the --sepia-tone option that was added in either 6.2.1 or 6.2.2. Prior to that, using that option caused the conversion to fail.

The following function will return the numeric version of ImageMagick you have installed.

PHP:
  1. function ImageMagick_version()
  2. {
  3. $convert_path = '/usr/bin/convert --version';
  4. $returnshell_exec($convert_path);
  5. $x = preg_match('/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return, $arr_return);
  6. return $arr_return[1];
  7. }

Of course you'll have to make sure that $convert_path points to the correct path for your ImageMagick command and that you have permissions to run the command.

Free Software Magazine - An online magazine focusing on free software
Canon updates firmware for 20D / 20Da and 350D
XML-RPC vulnerability found
Finally upgraded to WordPress 1.5.2
Irfanview 4.0 released!

5 Comments on “Get ImageMagick version with PHP”

Ryan on

It seems that this may not work with older versions of ImageMagick. I’ve had a few cases where this function returned an empty string. In those cases I’ve been unable to determine the ImageMagick version, so I’m just assuming it’s an older version issue. I’m thinking a version number like 5.5 may cause a similar problem because of the regex used.

Darren on

Can’t you just:

$convert_path = ‘convert -version’;

The double hyphen before “version” causes my ImageMagick (6.0.7) to spit out an impartial manual along with the version string. A single hyphen returns just the version string. The preg_match wouldn’t even be needed.

But I’m new at this. Sorry if I have made a huge oversight; just trying to help!

Ryan on

You know, I’ve had a lot of issues with some versions of ImageMagick returning a blank string. Your note may be the reason. I guess I should go and try to find a copy of an older version of ImageMagick and see what happens.

christian on

i just have a script with this:

good enough for me.

christian on

hmm. code didn’t display. i’ll try it again.

echo shell_exec(’convert -version’);

Leave a Comment