Skip to content

Get ImageMagick version with PHP

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.

function ImageMagick_version()
{
$convert_path = '/usr/bin/convert --version';
$return =  shell_exec($convert_path);
$x = preg_match('/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return, $arr_return);
return $arr_return[1];
}

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.

Published inProgramming

5 Comments

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

  2. Darren Darren

    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!

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

Leave a Reply

Your email address will not be published. Required fields are marked *