Unable to execute ImageMagick command via PHP
Unable to execute ImageMagick command via PHP
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 !
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 !
Re: Unable to execute ImageMagick command via PHP
It looks like a setup/path problem try this:
Some of the other codes on these pages should help find your setup details:
http://www.rubblewebs.co.uk/imagemagick ... server.php
Code: Select all
<?php
echo "<pre>";
system("type convert");
echo "</pre>";
?>
http://www.rubblewebs.co.uk/imagemagick ... server.php
Re: Unable to execute ImageMagick command via PHP
Test 1 -
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 -
Terminal - /Library/ImageMagick/ImageMagick-6.4.0/bin/convert
Firefox displays : 1 (No apache error log)

Code: Select all
<?php
echo "<pre>";
system("type convert");
echo "</pre>";
?>
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>";
?>
Firefox displays : 1 (No apache error log)

Re: Unable to execute ImageMagick command via PHP
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
What happens if you try
Code: Select all
system(" /Library/ImageMagick/ImageMagick-6.4.0/bin/identify toto.jpg");
- 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
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?
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?
Re: Unable to execute ImageMagick command via PHP
Yeah it was a path problem, now it works ! 
works, but I had to create an alias for the ImageMagick directory too !

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
Re: Unable to execute ImageMagick command via PHP
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?
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?
Re: Unable to execute ImageMagick command via PHP
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?
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:

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>";
?>
Code: Select all
$resizefile = "/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png";
Re: Unable to execute ImageMagick command via PHP
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!
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!
Re: Unable to execute ImageMagick command via PHP
Try running your code like this:
I do not know how to use find either; does the code above give you any output ?
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>";
?>
Re: Unable to execute ImageMagick command via PHP
Hi Bonzo,
When I tried this new code you sent, here's what I get for the output:
so then I changed the code to the following:
which yielded:
changing the exec command to:
yielded:
so then put the IMG_0041.JPG file into the Webserver directory, and changed the exec command back to
and ran the script again. I still got the
Is it because I'm missing this library? Thing is, the command
works fine from the terminal window.
Thanks,
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
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>";
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
Code: Select all
exec("pwd", $array);
Code: Select all
Array
(
[0] => /Users/shafiquejamal/Webserver
)
1
Code: Select all
exec("/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png 2>&1", $array);
error:Image not found
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
Code: Select all
/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png 2>&1
Thanks,
Re: Unable to execute ImageMagick command via PHP
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.
I would say its a path/setup problem but I do not know how to fix it.
Re: Unable to execute ImageMagick command via PHP
Would you know anything about Unix and symolic links? That may be where I should look. I'm focussing on the following error:
Thanks,
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:[0] => dyld: Library not loaded: /ImageMagick-6.4.4/lib/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?-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
Thanks,
Re: Unable to execute ImageMagick command via PHP
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
It gets halfway through before giving me errors:
Thanks,
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:
Argh! Do you have any advice?+ 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.
Thanks,
Re: Unable to execute ImageMagick command via PHP
Oops, sorry I got the two different aliases mixed up: Bonzo and gyrofoo. Sorry about that!