ImageMagick:通过PHP转换png失败并通过bash shell工作

时间:2021-12-01 09:00:45

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.

我想要做的是将全尺寸图片转换为160x120缩略图。它适用于任何大小的jpg和jpeg文件,但不适用于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)

PHP函数(缩短)

...
$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).

当此函数运行$ retval等于1时,表示转换命令失败(未创建缩略图)。

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

这是它变得有趣的地方,如果我在我的shell中运行完全相同的命令,它的工作原理。

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.

我尝试过使用不同的PHP函数,如system,passthru,但它没有用。我想也许有人在这里知道解决方案。

I'm using

  1. MAMP 1.7.2
    • Apache/2.0.59
    • PHP/5.2.6
  2. 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. libpng从1.2.35到1.2.37

  3. libiconv from 1.12_2 to 1.13_0
  4. libiconv从1.12_2到1.13_0

  5. ImageMagick 6.5.2-4_1 to 6.5.2-9_0
  6. ImageMagick 6.5.2-4_1到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:

我终于发现了一些可能有用的东西,当函数运行时,这是在Apache日志中打印的内容:

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...

libiconv.2.dylib是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

问题与MAMP有关,请参阅下面的解决方案

4 个解决方案

#1


Solved it!

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

事实证明环境变量DYLD_LIBRARY_PATH未正确设置。

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

Mac OS X Leopard附带libiconv 7.0.0但转换需要8.0.0(参见上面的第二个更新)

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.

ImageMagick和所有依赖项都是使用/ opt / local下的MacPorts安装的。这需要手动将路径/ opt / local / lib添加到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.

如果我在Mac OS X Leopard apachectl envvars文件/ usr / sbin / envvars中将路径/ opt / local / lib添加到DYLD_LIBRARY_PATH它不起作用。为什么?这是因为我不使用Mac OS X Leopard的apache,我使用MAMP。

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

MAMP有自己的apachectl脚本,它有自己的envvars文件。

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

我将路径/ opt / local / lib添加到MAMP apachectl envvars文件/ Applications / MAMP / Library / bin / envvars中的DYLD_LIBRARY_PATH

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!

现在我的PNG缩略图正在生成,并且在apache错误日志中没有生成错误!

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

我希望这会对某人有所帮助,下次我会记得在寻求帮助之前检查每个日志文件!

Phil

#2


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.

我的路径是/ opt / local / bin,但即使将它添加到DYLD_LIBRARY_PATH也行不通。最后,当我改变普通的ole PATH时,它通过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"

#3


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

确保运行php代码的用户对文件和目录具有相同的权限。

#4


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

这些应该是显而易见的,但请确保检查PHP安全模式,open_basedir以及是否已禁用exec等内容。

#1


Solved it!

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

事实证明环境变量DYLD_LIBRARY_PATH未正确设置。

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

Mac OS X Leopard附带libiconv 7.0.0但转换需要8.0.0(参见上面的第二个更新)

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.

ImageMagick和所有依赖项都是使用/ opt / local下的MacPorts安装的。这需要手动将路径/ opt / local / lib添加到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.

如果我在Mac OS X Leopard apachectl envvars文件/ usr / sbin / envvars中将路径/ opt / local / lib添加到DYLD_LIBRARY_PATH它不起作用。为什么?这是因为我不使用Mac OS X Leopard的apache,我使用MAMP。

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

MAMP有自己的apachectl脚本,它有自己的envvars文件。

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

我将路径/ opt / local / lib添加到MAMP apachectl envvars文件/ Applications / MAMP / Library / bin / envvars中的DYLD_LIBRARY_PATH

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!

现在我的PNG缩略图正在生成,并且在apache错误日志中没有生成错误!

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

我希望这会对某人有所帮助,下次我会记得在寻求帮助之前检查每个日志文件!

Phil

#2


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.

我的路径是/ opt / local / bin,但即使将它添加到DYLD_LIBRARY_PATH也行不通。最后,当我改变普通的ole PATH时,它通过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"

#3


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

确保运行php代码的用户对文件和目录具有相同的权限。

#4


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

这些应该是显而易见的,但请确保检查PHP安全模式,open_basedir以及是否已禁用exec等内容。