PHP + Apache in Mac OS X with ImageMagick (paths)

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
jpm
Posts: 3
Joined: 2010-02-26T09:36:33-07:00
Authentication code: 8675308

PHP + Apache in Mac OS X with ImageMagick (paths)

Post by jpm »

I was able to get ImageMagick to install and work under Terminal prompt in Mac OS X. But, PHP cannot "see" the paths set, unless I use putenv or create a "wrapper" shell script.

I have the following in /private/etc/profile
export MAGICK_HOME="/usr/local/ImageMagick"
export PATH="$MAGICK_HOME/bin:$PATH"
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib"

In a new Terminal window, typing "convert --version" displays the version info as expected.

However, in PHP, I have this...
passthru("echo \$PATH");
passthru("echo \$DYLD_LIBRARY_PATH");
passthru("convert -version",$ret);

The Path variable only shows the default system path (not the /usr/local/ImageMagick appended to it), and DYLD_LIBRARY_PATH is empty, even though they are both set in the /private/etc/profile and display when typing the commands from the Terminal. The Apache user, _www doesn't seem to load the profile.

I tried SetEnv in httpd.conf, but it doesn't help. The only way I could get it to work was with these 2 lines at the top of my PHP page.
putenv("PATH={$_SERVER["PATH"]}:/usr/local/ImageMagick/bin");
putenv("DYLD_LIBRARY_PATH=/usr/local/ImageMagick/lib");


Certainly someone else has been able to get ImageMagick + Apache + PHP on Mac OS X to work without using putenv. (That's not a good solution for me, since I use this as a Development area, and the live copy has different paths).

How do I get _www user to set the path/env. variables (or "read" the /private/etc/profile)?

For the time-being, I have a wrapper script and an alias set up in /usr/bin (which is in the _www user's path), but I really don't want to keep it that way.

Anyone know how to get _www "see" the profile variables that are set in /private/etc/profile?? I have been all over various forum posts and Google, and cannot find an answer (besides MacPorts). I've wasted about 3 hours, and since this is related to ImageMagick, I decided to try posting here first....

Thanks in advanced for any assistance.

-Jim

Running: OS X 10.6.2
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PHP + Apache in Mac OS X with ImageMagick (paths)

Post by fmw42 »

not an expert on unix or PHP with IM, but to get

convert -version

to work, I had to specify the full path to convert, usually /usr/local/bin/convert or /user/bin/convert as I could not figure out how to get shell paths over to PHP

try

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

or to find where IM convert is located do

find /usr | grep "convert"
jpm
Posts: 3
Joined: 2010-02-26T09:36:33-07:00
Authentication code: 8675308

Re: PHP + Apache in Mac OS X with ImageMagick (paths)

Post by jpm »

fmw42 wrote: I had to specify the full path to convert, usually /usr/local/bin/convert or /user/bin/convert as I could not figure out how to get shell paths over to PHP
Thanks... But the DYLD_LIBRARY_PATH variable would still be missing if I just specified the path. And, the path is different on my Mac then on the live server where I am copying it to.

So far, the only solution that has worked so far was a "wrapper" script. I have an alias called "convert" in /usr/bin (which PHP/Apache can see and run). That wrapper script calls the real convert from the ImageMagick folder... Since I can get /private/etc/profile to work for _www, that's the only solution that's worked on my Mac. I got the wrapper script from the Internet somewhere as a work-around.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PHP + Apache in Mac OS X with ImageMagick (paths)

Post by Bonzo »

I have never tried thas and do not know anything about mac ! Worth a try ?

Code: Select all

<?php
// In my case $HTTP_ENV_VARS['PATH'] = bin:usr/bin
$env_vars = explode( ':', $HTTP_ENV_VARS['PATH']);
// Combine the required part from $HTTP_ENV_VARS['PATH'] and convert for your path.
$env_path = $env_vars[1]."/convert";
// Now $env_path = usr/bin/convert

// Usage:
exec("$env_path image.jpg -resize 100x100 output.jpg");
?> 
jpm
Posts: 3
Joined: 2010-02-26T09:36:33-07:00
Authentication code: 8675308

Re: PHP + Apache in Mac OS X with ImageMagick (paths)

Post by jpm »

Bonzo wrote:I have never tried thas and do not know anything about mac ! Worth a try ?
I appreciate your reply, but I am looking for something that does not have to be inserted into my PHP code. I can get it to work just fine using putenv in my PHP code, but the paths differ from the live (I know your code would help eliminate this but the truth is the live server does not need putenv or paths - it just works as it is supposed to).

I guess my question is almost more apache/mac related, in a way. It seems that the _www user on the Mac doesn't properly set the environment variables and paths, and therefore won't let PHP see them. So far, I am just continuing with my work-around code... I'd really like to see the _www user be able to set the variables correctly so programs like ImageMagick work as intended.

Thanks again everyone for your input......

-Jim.
Post Reply