views:

1842

answers:

8

I've got a very weird bug which I've yet to find a solution. UPDATE see solution below

What I am trying to do is convert a full size picture into a 160x120 thumbnail. It works great with jpg and jpeg files of any size, but not with png.

ImageMagick command:

/opt/local/bin/convert '/WEBSERVER/images/img_0003-192-10.png' -thumbnail x320 -resize '320x<' -resize 50% -gravity center -crop 160x120+0+0 +repage -quality 91 '/WEBSERVER/thumbs/small_img_0003-192-10.png'

PHP function (shortened)

...
$cmd = "/opt/local/bin/convert '/WEBSERVER/images/img_0003-192-10.png' -thumbnail x320 -resize '320x<' -resize 50% -gravity center -crop 160x120+0+0 +repage -quality 91 '/WEBSERVER/thumbs/small_img_0003-192-10.png'";
exec($cmd, $output, $retval);
$errors += $retval;
if ($errors > 0) {
    die(print_r($output));
}

When this function runs $retval equal 1 which means the convert command failed (thumbnail isn't created).

This is where it gets interesting, if I run the exact same command in my shell, it works.

wedbook:~ wedix$ /opt/local/bin/convert '/WEBSERVER/images/img_0003-192-10.png' -thumbnail x320 -resize '320x<' -resize 50% -gravity center -crop 160x120+0+0 +repage -quality 91 '/WEBSERVER/thumbs/small_img_0003-192-10.png'
wedbook:~ wedix$

I've tried using different PHP function such as system, passthru but it didn't work. I thought maybe someone here knew the solution.

I'm using

  1. MAMP 1.7.2
    • Apache/2.0.59
    • PHP/5.2.6

Thanks!

UPDATE

I updated the following dependencies

  1. libpng from 1.2.35 to 1.2.37
  2. libiconv from 1.12_2 to 1.13_0
  3. ImageMagick 6.5.2-4_1 to 6.5.2-9_0

However, it did not fix my problem.

2nd UPDATE

I finally found something that might help, when the function runs this is what gets printed in the Apache logs:

dyld: Library not loaded: /opt/local/lib/libiconv.2.dylib
  Referenced from: /opt/local/bin/convert
  Reason: Incompatible library version: convert requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0

3rd UPDATE

libiconv.2.dylib is version 8.0.0...

bash-3.2$ otool -L /opt/local/lib/libiconv.2.dylib 
/opt/local/lib/libiconv.2.dylib:
    /opt/local/lib/libiconv.2.dylib (compatibility version 8.0.0, current version 8.0.0)
    /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)

4th UPDATE

Problem was related to MAMP, see solution below

+2  A: 

Does the user running the php code have the same permissions on the files and directories?

jjclarkson
Yes, both images and thumbs folder are also world-writable.
philhq
+1  A: 

Does the web server have permission to run the convert program, and is the path of convert (/opt/local/bin/, which is not included by default) in your PHP include_path?

Dustin Fineout
/opt/local/bin is in my include_path and the web server has permission to run convert: -rwxr-xr-x 2 root admin 13148 Jun 20 04:09 convert
philhq
That sucks. Haha sorry I couldn't resist; let me scratch my head a bit more...
Dustin Fineout
I've tried to execute a shell script (.sh) with the command inside instead of calling the convert command directly and it still doesn't work. If I call the same script via the command line using "sh myscript.sh", the thumbnail gets created. Weird...
philhq
Im thinking the Environement variables might be different which makes the convert command failed, one of the ENV vars COMMAND_MODE is set to legacy when the script is called through php and is set to unix2003 when I execute it from my shell, I have no idea what this var is though.
philhq
A: 

These should be obvious, but make sure you check things like PHP safe mode, open_basedir, and whether exec has been disabled.

tylerl
They aren't disable, I was able to generate thumbnails from JPG and JPEG files but not from PNG files. See solution below
philhq
+3  A: 

Solved it!

It turns out the environement variable DYLD_LIBRARY_PATH wasn't set properly.

Mac OS X Leopard comes with libiconv 7.0.0 but convert requires 8.0.0 (see 2nd UPDATE above)

bash-3.2$ otool -L /usr/lib/libiconv.2.dylib 
/usr/lib/libiconv.2.dylib:
    /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
    /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.1)

ImageMagick and all dependencies was installed with MacPorts under /opt/local. This requires to manually add the path /opt/local/lib to DYLD_LIBRARY_PATH.

If I add the path /opt/local/lib to DYLD_LIBRARY_PATH in the Mac OS X Leopard apachectl envvars file /usr/sbin/envvars it doesn't work. Why? It's because I don't use apache from Mac OS X Leopard, I use MAMP.

MAMP has its own apachectl script and it's own envvars file.

I added the path /opt/local/lib to DYLD_LIBRARY_PATH in the MAMP apachectl envvars file /Applications/MAMP/Library/bin/envvars

DYLD_LIBRARY_PATH="/opt/local/lib:/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"

Now my PNG thumbnails are being generated and no errors are generated in the apache error log!

I hope this will help someone and next time I'll remember to check every logs files before asking for help!

Phil

philhq
Was going to suggest this was a Leopard-specific issue after reading about the COMMAND_MODE issue. Nice work :)
Dustin Fineout
man this is not working for me and I am pulling my hair here... any other instruction not mentioned here you might have done?
mga
hi... I could make a "hello world" like `exec("convert logo: logo.gif")` and it works fine... however, a more complex convert will work in shell but not in exec(): `/opt/local/bin/convert -size 758x185 /Applications/MAMP/htdocs/recetas/img/photo/old.tmp -crop 470x185+142+0! -quality 100 +profile "*" /Applications/MAMP/htdocs/recetas/img/photo/new.jpg`... I am creating logo.gif in the same dir as old.tmp so it is not a matter of paths (besides, the thing works if I copy/paste the command in bash or sh. The error in apache (no php errors) is `sh: command_here: No such file or directory`
mga
for the record: seems the `escapeshellarg()` function does something with the paths causing ImageMagick not to work. I had never used that before but saw it as a recommendation in php.net so went ahead with that and have just wasted threee whole days debugging this freaking thing... oh well
mga
A: 

Thank you so much!

Shaun
A: 

Thanks wedix! You just saved my day ;-)

MidnightMotion
A: 

My path was /opt/local/bin, but even adding that to DYLD_LIBRARY_PATH didn't work. Finally when I changed just plain ole PATH, it worked via PHP.

;Did not work...

;DYLD_LIBRARY_PATH="/opt/local/bin:/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"

;export DYLD_LIBRARY_PATH

; This works!

export PATH="$PATH:/opt/local/bin"

A: 

I'm having a similar issue with libiconv, trying to get ImageMagic to work on Snow Leopard, but AFAIK the DYD_LIBRARY_PATH is correct. I build version 1.13, but it still says it only provides version 7.0.0

ראובן