ImageMagick: convert png fail via PHP and works via bash shell
-
06-07-2019 - |
Question
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
MAMP 1.7.2
Apache/2.0.59
PHP/5.2.6
Thanks!
UPDATE
I updated the following dependencies
libpng from 1.2.35 to 1.2.37
libiconv from 1.12_2 to 1.13_0
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
Solution
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
OTHER TIPS
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"
Make sure the user running the php code has the same permissions on the files and directories.
These should be obvious, but make sure you check things like PHP safe mode, open_basedir, and whether exec
has been disabled.