In PHP, exec("convert") won't work

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
nelstrom

In PHP, exec("convert") won't work

Post by nelstrom »

I am trying to run ImageMagick's convert program from within a PHP script, with frustrating results. I need to convert an image from .PNG to .GIF format. I have tried the following:

Code: Select all

<?php
$input = "image.png";
$output = "image.gif";
$convert = "/usr/local/bin/convert";
$command = "$convert $input $output";
exec($command, $return);
foreach ($return as $val){
	echo "<br/>".$val;
}
?>
(The image.png file is in the same directory as the php script).
I thought this code would at least output something useful to the page when run in the browser, but I get nothing. Eventually, I found the following output:
convert: no decode delegate for this image format `image.png'.
convert: missing an image filename `image.gif'.


in the Apache log file /private/var/log/httpd/error_log. I don't believe that ImageMagick doesn't have a decode delegate for .png format. For a start, I am able to convert from .PNG to .GIF when I run convert at the command line. Just to double-check, I ran the following at the command line

Code: Select all

identify -list format
Which output:
Format Module Mode Description
-------------------------------------------------------------------------------
A* RAW rw+ Raw alpha samples
ART* ART r-- PFS: 1st Publisher
...
GIF* GIF rw+ CompuServe graphics interchange format
...
PNG* PNG rw- Portable Network Graphics (libpng 1.2.12)
...

* native blob support


And out of interest, I tried running the following PHP:

Code: Select all

<?php
$command = "/usr/local/bin/identify -list format";
exec($command, $return);
foreach ($return as $val){
	echo "<br/>".$val;
}
?>
Which output the following when loaded in the browser:
Format Module Mode Description
-------------------------------------------------------------------------------

* native blob support


i.e. Nothing! Incidentally, if I change the php code above to

Code: Select all

$command = "/usr/local/bin/identify";
	// *or*
$command = "/usr/local/bin/convert";
then, on running it in the browser, the page is filled with exactly the same messsage as I would get if I just execute 'identify' or 'convert' on the command line with no arguments.
I'm pretty confused as to what all of this means. It seems as though PHP is capable of running the programs at their very simplest (ie with no arguments), but can't cope when options or arguments are added.

I've googled to find that others have had similar problems (e.g. ImageMagick working on command line, but not from PHP with exec() call), but none of the solutions given have helped me. Here are some details which may be relevant:
  • PHP is not running in safe mode.
  • In PHP, open_basedir is not set (discussed here: viewtopic.php?f=10&t=7267)
  • I also searched the httpd.conf file for the string "open_basedir", which returned 0 results.
  • Permissions for convert, by ls -l /usr/local/bin/convert are:
    -rwxr-xr-x 1 root admin 34656 Sep 24 19:00 /usr/local/bin/convert
    (discussed here: http://www.webhostingtalk.com.au/archiv ... -3401.html)
I am completely stuck! Any guidance at all would be greatly appreciated.
And if you're still reading, thanks.
:?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: In PHP, exec("convert") won't work

Post by Bonzo »

I would try something simple first that should work:

Code: Select all

exec("/usr/local/bin/convert input_image.jpg output_image.gif ");
echo "<img src=\"output_image.gif \">";
Just thinking about it is this line wrong :
$command = "$convert $input $output";
Should be $command = "$convert.$input.$output";

Anyway I am at work and can not test that.

I have a few ImageMagick and php examples on my site that may be of interest but I have not had your problem.

http://www.rubblewebs.co.uk/imagemagick/
nelstrom

Re: In PHP, exec("convert") won't work

Post by nelstrom »

Thanks for your reply Bonzo.

I tried running your simple example, but it did not work. The link that you posted includes the following advice:
  • If you are using a path to a file e.g. ../folder/image.jpg it will probably break the code but if you put $name = '../folder/image.jpg' and use $name in the code instead it should work OK
I do eventually want to make the script act on image files which are not kept in the same directory as the script, so I tried following this advice. Unfortunately, my code won't even work if the input image is in the same directory as the script!

It looks as though my code is fine. I am wondering now whether there is something I need to reconfigure, either in Apache, PHP or ImageMagick.

Any ideas?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: In PHP, exec("convert") won't work

Post by Bonzo »

It sounds as though there must be a problem with your setup if something as simple as changing a jpg to a gif will not work.

I had a similar error to this the other night
convert: no decode delegate for this image format `image.png'.
when trying to resize an error.log by mistake !

I can not help any further I am afraid.
Post Reply