Unable to execute ImageMagick command via PHP

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?".
effbee

Unable to execute ImageMagick command via PHP

Post by effbee »

Hi ! I've installed ImageMagick. I'm on Mac OS X Leopard. ImageMagick works fine. I'm able to execute command lines via the Terminal.

However, when it's time to execute an ImageMagick command via PHP... it doesn't work !

system("identify toto.jpg");
system("/usr/bin/identify toto.jpg");


Doesn't work. PHP Safe mode is off. I think it might be the permissions or the access because the Apache error logs tell me :

sh: identify: command not found
sh: /usr/bin/identify: No such file or directory


Thanks for helping me !
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Unable to execute ImageMagick command via PHP

Post by Bonzo »

It looks like a setup/path problem try this:

Code: Select all

<?php
echo "<pre>";
system("type convert"); 
echo "</pre>";
?> 
Some of the other codes on these pages should help find your setup details:
http://www.rubblewebs.co.uk/imagemagick ... server.php
effbee

Re: Unable to execute ImageMagick command via PHP

Post by effbee »

Test 1 -

Code: Select all

<?php
echo "<pre>";
system("type convert"); 
echo "</pre>";
?> 
Terminal - convert is /Library/ImageMagick/ImageMagick-6.4.0/bin/convert
Firefox - Blank page (Apache error log : sh: line 0: type: convert: not found)

Test 2 -

Code: Select all

<?php
echo "<pre>";
system('which convert',$path); print_r($path); 
echo "</pre>";
?> 
Terminal - /Library/ImageMagick/ImageMagick-6.4.0/bin/convert
Firefox displays : 1 (No apache error log)

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

Re: Unable to execute ImageMagick command via PHP

Post by Bonzo »

Hopefuly Fred can help you as he is a "mac" man; but I think your problem is a path problem.

What happens if you try

Code: Select all

system(" /Library/ImageMagick/ImageMagick-6.4.0/bin/identify toto.jpg");
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Unable to execute ImageMagick command via PHP

Post by fmw42 »

Are you on your own computer running PHP and Apache or are you running this from an ISP server. My only experience has been a brief test using PHP on Godaddy with IM and it depended upon what version of PHP they had as to what the command syntax was. Of course this had nothing to do with Mac as Godaddy is hosting on Unix for my web site.

This worked for me:

<?php
system("/usr/local/bin/convert -version");
?>

as did

<?php
$IM_version=shell_exec("/usr/local/bin/convert -version");
echo $IM_version
?>

as did

<?php
exec("/usr/local/bin/convert -version",$out,$returnval);
print_r($out[0]);
?>

I think you may have a path problem. Where did you install IM on your computer or where is it on the server?
effbee

Re: Unable to execute ImageMagick command via PHP

Post by effbee »

Yeah it was a path problem, now it works ! :)

Code: Select all

/usr/local/bin/convert -version


works, but I had to create an alias for the ImageMagick directory too !

Code: Select all

ln -s /Library/ImageMagick/ImageMagick-6.4.0/ /ImageMagick-6.4.0
shafique

Re: Unable to execute ImageMagick command via PHP

Post by shafique »

Hello, I'm also having problems executing "convert" using PHP.

First the path was wrong, and the system command gave me a return value of 127. Then I fixed the path to the convert command (/Applications/ImageMagick-6.4.4/bin/convert), and I fixed the paths to the input and output filenames. Here is the code I'm using:

<?php
$resizefile = "/Applications/ImageMagick-6.4.4/bin/convert /Users/shafiquejamal/outsideofwebdir/uploads/IMG_0041.JPG /Users/shafiquejamal/outsideofwebdir/uploads/41.png";

echo $resizefile;
$last_line = system($resizefile , $retval);

// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>

The return value is 5, when I execute this; it does not convert the file. BUT When I copy and past the resize command (without quotes) into the terminal window, it works just fine - it converts the file. It just doesn't work from php. I'm running MAC OS X Leopard 10.5.5 on a MacBook Pro (Intel), and using ImageMagick-6.4.4 I've changed permissions on the source and target folders using chmod a+rw {paths}, and checked that everyone can read and write to those directories.

Can anyone help?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Unable to execute ImageMagick command via PHP

Post by Bonzo »

Firstly I know nothing about macs :?

Are you running php as a local host ? If so Imagemagick should have been found automaticaly.

What does this code return?

Code: Select all

<?php
echo "<pre>";
system("type convert"); 
echo "</pre>";
?> 
I always use relative paths, try putting the code into the same folder as the photo first and put the path from the code above:

Code: Select all

$resizefile = "/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png";
shafique

Re: Unable to execute ImageMagick command via PHP

Post by shafique »

Hello Bonzo,

Thanks for replying to my post. I am indeed running php as a localhost. I followed your instructions

a) When I ran that code, it yielded no output at all; I just got a blank webpage

b) moving the pictures into the same folder as the IM application, and changing $resizefile to what you gave me yielded the same result as before: Return value : 5, and the image did get converted.

A friend suggested that IM may be trying to access libraries or other directories to which it does not have permissions. He said to run convert from the command line, then use the "find" command so see which files were most recently modified, after I run convert. I don't know how to do this.

Do you have any other suggestions? Thanks!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Unable to execute ImageMagick command via PHP

Post by Bonzo »

Try running your code like this:

Code: Select all

<?php
$array=array();
echo "<pre>";
exec("convert IMG_0041.JPG 41.png 2>&1", $array); 
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";
?> 
I do not know how to use find either; does the code above give you any output ?
shafique

Re: Unable to execute ImageMagick command via PHP

Post by shafique »

Hi Bonzo,

When I tried this new code you sent, here's what I get for the output:

Code: Select all

Array
(
    [0] => sh: convert: command not found
)

1
so then I changed the code to the following:

Code: Select all

$array=array();
echo "<pre>";
exec("/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png 2>&1", $array);
echo "<br>".print_r($array)."<br>";
echo "</pre>";
which yielded:

Code: Select all

Array
(
    [0] => dyld: Library not loaded: /ImageMagick-6.4.4/lib/libMagickCore.1.dylib
    [1] =>   Referenced from: /Applications/ImageMagick-6.4.4/bin/convert
    [2] =>   Reason: image not found
)
1
changing the exec command to:

Code: Select all

exec("pwd", $array);
yielded:

Code: Select all

Array
(
    [0] => /Users/shafiquejamal/Webserver
)

1
so then put the IMG_0041.JPG file into the Webserver directory, and changed the exec command back to

Code: Select all

exec("/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png 2>&1", $array);
and ran the script again. I still got the
Image not found
error:

Code: Select all

Array
(
    [0] => dyld: Library not loaded: /ImageMagick-6.4.4/lib/libMagickCore.1.dylib
    [1] =>   Referenced from: /Applications/ImageMagick-6.4.4/bin/convert
    [2] =>   Reason: image not found
)

1
Is it because I'm missing this library? Thing is, the command

Code: Select all

/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png 2>&1
works fine from the terminal window.

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

Re: Unable to execute ImageMagick command via PHP

Post by Bonzo »

There have been other posts about the code working in terminal and not in php.

I would say its a path/setup problem but I do not know how to fix it.
shafique

Re: Unable to execute ImageMagick command via PHP

Post by shafique »

Would you know anything about Unix and symolic links? That may be where I should look. I'm focussing on the following error:
[0] => dyld: Library not loaded: /ImageMagick-6.4.4/lib/libMagickCore.1.dylib
I think that once I fix this, it may solve the Image Not Found problem. I did a listing of the IM lib directory, and go this:
-rwxr-xr-x@ 1 shafique admin 6187196 Oct 15 23:04 libMagickCore.1.dylib
lrwxr-xr-x 1 shafique admin 21 Oct 24 15:46 libMagickCore.dylib -> libMagickCore.1.dylib
(among other files). So the library is present, its just that apache/php can't load it, but the terminal user can. From the directory listing, it seems that all users have permission to read and execute the above files, and I doubt I would need to change the write permissions (I never access 'convert' with sudo -u root). Thanks for your help so far. Do you know where I should look to get answers to this kind of issue?

Thanks,
shafique

Re: Unable to execute ImageMagick command via PHP

Post by shafique »

Hi Gonzo,

I uninstalled IM (which I had installed from the binary release, and then installed Mac Port. You know, I think I just have the worst luck; ImageMagick just won't install on my MacBook Pro (Leopard) using Ports. I typed

Code: Select all

sudo port install ImageMagick


It gets halfway through before giving me errors:
+ ln -s ../../lib/GL/mesa/drivers/osmesa/libOSMesa.4.dylib .
rm -f libOSMesa.4.0.dylib
mv -f libOSMesa.4.0.dylib~ libOSMesa.4.0.dylib
mv: rename libOSMesa.4.0.dylib~ to libOSMesa.4.0.dylib: No such file or directory
make[5]: *** [libOSMesa.4.0.dylib] Error 1
make[4]: *** [all] Error 1
make[3]: *** [all] Error 1
make[2]: *** [all] Error 1
make[1]: *** [World] Error 2
make: *** [World] Error 2

Error: The following dependencies failed to build: XFree86 bzip2 expat fontconfig freetype zlib libiconv gperf jpeg libpng libxml2 pkgconfig tiff
Error: Status 1 encountered during processing.
Argh! Do you have any advice?

Thanks,
shafique

Re: Unable to execute ImageMagick command via PHP

Post by shafique »

Oops, sorry I got the two different aliases mixed up: Bonzo and gyrofoo. Sorry about that!
Post Reply