Rubblewebs

THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.

I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.

IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.

Locating and analysing errors

The standard output from ImageMagick is 0 for successful image generation and 1 for a failure.
This is not very helpful for finding the reason you image failed and so you can alter your command as below and it will report any errors.

You can use the 1 or 0 like this:


exec("convert header.jpg -crop x246+0+0 -resize 182x246! header_cropped.jpg", $output, $return); 
if ($return == "0") { echo '<br>Image generation sucssesful<br>'; } 
else { echo '<br>Image generation failed<br>'; } 

With this code if the image generation fails the code will display "Image generation failed". If the image is generated it will display "Image generation sucssesful".


A better way to display the actual error


$array=array();
echo "<pre>";
exec("convert House.jpg -channel B -separate Output_test.png 2>&1", $array); 
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";

Output:

Array
(
    [0] => convert: unable to open image `House.jpg': No such file or directory.
    [1] => convert: missing an image filename `Output_test.png'.
)
1

In this case House.jpg can not be found and as the code has failed and you also get the second error.


$array=array();
echo "<pre>";
exec("convert -channel B -separate Output_test1.png 2>&1", $array); 
echo "<br>".print_r($array)."<br>";
echo "</pre>"; 

Output:

Array
(
    [0] => convert: missing an image filename `Output_test1.png'.
)
1

This error output is not as helpful; you get the "missing an image filename" error but you are not told anything else that could be causing the problem. The true problem is that there is no input image.


A different error reporting method sugested on the ImageMagick forum


$cmd_debug= (TRUE)? \'-debug exception\' : \'\';
//or false; the \'\' is two single quote marks. A NULL should work also
$command= "sunflower.jpg -resize x160 -resize \"160x<\" -resize 50%
-gravity center -crop 80x80+0+0 +repage";
//your string on a single line
$file = "68a.jpg";
//php path rule to file
exec("convert $cmd_debug $command $file", $IMarray, $code);
echo "<pre>";
print_r($IMarray);
echo "</pre>";