ImageMagick command with PNG alpha transparency behaves differently in PHP

I’m using ImageMagick’s convert utility to carry out some image manipulation. Here’s the command I’m running via PHP’s exec command:

/usr/local/bin/convert /path/to/photo/4.jpg -format PNG32 -background transparent -thumbnail x330 -resize '440x<' -resize 50% -gravity center -crop 220x165+0+0 -rotate "-4.4" /path/to/photo/4.png 2>&1

The command runs perfectly via the CLI (see shell.png), but when run in PHP it outputs a completely different image (see php.jpg). php.jpg was originally named php.png as it was the file being outputted when the command was run via PHP, however when uploading the image as an attachment for this post the uploader told me that “This JPEG image has the incorrect file extension” - this would suggest that the mime type of the file that the convert command is outputting when run via PHP is image/jpeg? If I take the ‘-background transparent’ switch out of the command it runs the same in PHP as on the CLI, but I have no idea why!

Here’s what the command is supposed to achieve:

  • Set the format of the image that will be outputted to a 32-bit PNG with a transparent background
  • Resize the image to an appropriate size and then crop it from the center of the image
  • Rotate the image -4.4 degrees
  • The result of the command should then be output to a PNG file
  • The 2>&1 is simply a redirection of stderr to the same location as stdout so that I can capture any error in PHP

On my local development setup (MAMP on OS X Leopard) I am running ImageMagick 6.3.6 11/28/07 Q8 (on the live server the version of ImageMagick is 6.0.7 05/03/07 Q16, so I’m expecting functionality to be pretty similar?)

I’m open of course to any suggestions that use a different ImageMagick method to achieve the results I require - any help is greatly appreciated as this has been baffling me for a good while now, thanks!

I would say version 6.0.7 is probably about 3 years old; I think its the version that comes with the standard linux/unix installations.

Q8 is 8bit and Q16 is 16bit.

So there is quite a differnce in the 2 versions.

Thanks for the information Rubble, that’s definitely useful to know, however the problem I’m getting is when testing with my local development setup (newer version of ImageMagick). The key part of my problem is that I can’t seem to output a PNG with an alpha transparent background - I’ve tried several different approaches, but I’ve found the ImageMagick documentation regarding transparency and PNGs to be rather confusing!

Just to confirm what you are talking about:

On your local development machine you are running as a server and using php - this does not work. You get the black background

When run with the shell on the same machine it is OK and you get the transparent background.

You have not tried it on the online server which is running 6.0.7

I presume the problem must be something to do with the way the code is being interperated?

This works for me using XAMPP on windows XP:

<?php
$array=array();
echo "<pre>";
exec("convert 2007_1226christmas0004.JPG -format PNG32 -background transparent -thumbnail x330 -resize \\"440x<\\" -resize 50&#37; -gravity center -crop 220x165+0+0 -rotate -4.4 output1.png 2>&1", $array);
echo "<br>".print_r($array)."<br>";
echo "</pre>";
?>

Note if I use -resize \“440x<\” it works but if I use -resize ‘440x<’ it fails. This is just something to do with windows.
Version: ImageMagick 6.3.7 11/14/07 Q16
I can use either on my server and they work Version: ImageMagick 6.3.5 08/29/07 Q16
I have had nothing to do with OS X and presume it must be something similar with the way the code is written ?

P.S. If I use the XP command line I must use -resize “440x<” not -resize ‘440x<’ as well.

You’ve got PM Rubble :slight_smile: