5

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

0

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

5 Comments

Was going to suggest this was a Leopard-specific issue after reading about the COMMAND_MODE issue. Nice work :)
man this is not working for me and I am pulling my hair here... any other instruction not mentioned here you might have done?
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
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
Hey you rock! This helped me out. I installed imagick using homebrew and with the new version of OSX (Lion), X11 is already installed. I had a problem where i included /usr/local/lib but I still could not use png format. So I had to include /usr/X11/lib. Now it works!
5

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"

2 Comments

Thank you, thank you, thank you! This helped me solve the problem :)
Thanks! I know that these "thank you" comments are discouraged, but this ended a long, confusing battle for me.
1

Make sure the user running the php code has the same permissions on the files and directories.

1 Comment

Yes, both images and thumbs folder are also world-writable.
0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.