15

I installed Imagemagick using Homebrew on Lion, everything is fine except that it doesn't work at all when being called from php. Console:

$ convert -version
Version: ImageMagick 6.7.1-1 2011-07-29 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP   

$ which convert
/usr/local/bin/convert

PHP:

echo exec ('convert -version');

or exec('convert -version', $output); var_dump($output);

Produces nothing (or an empty array).

exec ('/usr/local/bin/convert') // works, but
exec ('which convert') // doesn't

I need to test this locally to make sure I can detect convert in production environment. But I can't properly test it. The PATH is set and it works in Terminal, but not from PHP.

Resolved:

Turns out, for php to work convert should be in /usr/bin/ so this solved it:

ln -s /usr/local/bin/convert /usr/bin/convert

Update

It was becasue of MAMP, here is the fix: http://firedevcom.tumblr.com/post/22791937644/fix-for-homebrew-imagemagick-and-mamp

Open /Applications/MAMP/Library/bin/envvars

And comment out the following lines:

DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH

Done.

aorcsik
  • 15,271
  • 5
  • 39
  • 49
firedev
  • 20,898
  • 20
  • 64
  • 94
  • 1
    Is convert in PHP's path? You may have to specify an `/absolute/path/to/convert` in the exec() call. Remember that the environment in a PHP exec() call can be significantly different from what you get at a shell prompt. – Marc B Aug 23 '11 at 15:33
  • Hmm the thing is - everything works on production server, just not with Homebrew. I thought PATH should be inherited into exec sandbox, no? – firedev Aug 25 '11 at 06:15
  • 1
    php's shell environment is completely different from a user shell environment. you can't compare them directly. try doing `exec('echo $PATH');` to see what the shell's got going. PHP will inherit the Apache environment. – Marc B Aug 25 '11 at 14:26
  • Awesome! That worked! You probably saved me a couple of more hours. I am very grateful @aorcsik! – user1029978 Mar 26 '13 at 10:30
  • Just editing envvars didn't work for me. I had to create symbolic links in /usr/bin/ too to get Imagick to work in exec. Also, remember to restart servers in MAMP after you edit envvars. – Jaffer Jun 24 '13 at 05:58

6 Answers6

5

Adding my own answer here so you can vote:

It was caused by MAMP, here is the fix: http://firedevcom.tumblr.com/post/22791937644/fix-for-homebrew-imagemagick-and-mamp

Open /Applications/MAMP/Library/bin/envvars

And comment out the following lines:

DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH

Done.

firedev
  • 20,898
  • 20
  • 64
  • 94
2
sudo ln -s /usr/local/bin/convert /usr/bin/convert
j0k
  • 22,600
  • 28
  • 79
  • 90
NiLL
  • 13,645
  • 14
  • 46
  • 59
1

Simply use exec("PATH=\$PATH:/usr/local/bin; convert file.pdf file.png"); It will add convert to PATH on runtime.

codefreak
  • 6,950
  • 3
  • 42
  • 51
  • this is exactly what i need. But here i need to get a trick on my PHP code because i don't need the PATH to my live machine. Do you know a way to set on the mac this export path global? – workdreamer Apr 09 '14 at 20:50
1

instead of just exec("convert .... "); use a full path. you can get it by typing the terminal

type convert

you should get something like: convert is hashed (/opt/local/bin/convert)

so now use:

exec("/opt/local/bin/convert .... ");

[credits to @Nikki]

after that comment out

DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH" export DYLD_LIBRARY_PATH

in /Applications/MAMP/Library/bin/envvars

keithics
  • 8,576
  • 2
  • 48
  • 35
1

Verify that convert in is the server's PATH environment variable. Or just specify the full path:

exec('/usr/local/bin/convert -version');
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • It is in the PATH in console, but not in php's exec, however on production server everything seem to work fine. And I need to be able to detect imagemagick automatically. It works on Linux, but not with homebrew. – firedev Aug 25 '11 at 06:18
1

The exec returns the last line from the result of the command which happens to be an empty string. If you want to get the output, just do something like this:

exec('convert -version', $output);
var_dump($output); // it is an array which filled with every line of output from the command
xdazz
  • 158,678
  • 38
  • 247
  • 274