ImageMagick convert 在命令行中工作,但不能通过 PHP exec()

2022-01-13 00:00:00 linux imagemagick centos php

我正在使用 PHP 的 exec() 来使用 ImagicMagick 的 convert 转换图像.这是在 CentOS 服务器上运行的.

exec(转换 http://www.google.com/images/srpr/logo3w.png.jpg-resize 640/home/mysite/public_html/public/img/posts/original/1414_301a4.jpg);

使用 exec() 不会导致新图像出现在目标文件夹中.但是,如果我要在 shell 中运行相同的命令,它会完美运行!

我相信这是一个 PATH 问题.如果是这样,如何检查 PHP 使用的路径,以及如何在 PHP 中设置正确的路径?

解决方案

首先,您应该尝试使用一个确实存在且可检索的输入文件:

wget http://www.google.com/images/srpr/logo3w.png.jpg--2012-08-21 20:55:24-- http://www.google.com/images/srpr/logo3w.png.jpg正在解析 www.google.com (www.google.com)... 173.194.35.179, 173.194.35.177, 173.194.35.178, ...正在连接到 www.google.com (www.google.com)|173.194.35.179|:80... 已连接.已发送 HTTP 请求,等待响应... 404 Not Found2012-08-21 20:55:24 错误 404:未找到.

然后,查看您的 PHP 使用了哪个 convert:

exec(which convert 2>/tmp/whichconvert.2 1>whichconvert.1)

cat/tmp/whichconvert.{1,2}

最后,还可以尝试convert的完整路径:

exec(/usr/local/full/path/to/convert logo:-resize 640/home/mysite/public_html/public/img/posts/original/1414_301a4.jpg);

然后

识别/home/mysite/public_html/public/img/posts/original/1414_301a4.jpg


更新:

您要检索的文件可能不是logo3w.png.jpg,而是logo3w.png:

<上一页>wget http://www.google.com/images/srpr/logo3w.png--2012-08-21 21:04:22-- http://www.google.com/images/srpr/logo3w.png正在解析 www.google.com (www.google.com)... 173.194.35.180, 173.194.35.177, 173.194.35.179, ...正在连接到 www.google.com (www.google.com)|173.194.35.180|:80... 已连接.HTTP 请求已发送,等待响应... 200 OK长度:7007 (6.8K) [图片/png]保存到:'logo3w.png'100%[=======================================================>] 7,007 --.-K/s 在 0.02 秒内2012-08-21 21:04:22 (451 KB/s) - ‘logo3w.png’ 已保存 [7007/7007]

另外, 如果它仅适用于本地文件(不适用于远程 http-URI),您可能需要检查 convert<使用的 http 委托 是否/code> 确实安装在您的系统上:

convert -list 委托 |grep httphttps =>/opt/local/bin/curl"-s -k -o %o"https:%M"

另外,检查运行 PHP 的用户帐户是否确实有权写入目标目录:

exec(touch/home/mysite/public_html/public/img/posts/original/touchtest);

然后

ls -l/home/mysite/public_html/public/img/posts/original/touchtest

I am using PHP's exec() to convert an image using ImagicMagick's convert. This is being run on a CentOS server.

exec(convert http://www.google.com/images/srpr/logo3w.png.jpg 
    -resize 640 /home/mysite/public_html/public/img/posts/original/1414_301a4.jpg);

Using exec() does not cause the new image to appear in the destination folder. However if I were to run the same command in the shell, it works perfectly!

I believe this is a PATH problem. If so, how can I check the path that PHP is using, and how can I set the correct path in PHP?

解决方案

First, you should try with an input file that does indeed exist and is retrievable:

wget http://www.google.com/images/srpr/logo3w.png.jpg

  --2012-08-21 20:55:24--  http://www.google.com/images/srpr/logo3w.png.jpg
  Resolving www.google.com (www.google.com)... 173.194.35.179, 173.194.35.177, 173.194.35.178, ...
  Connecting to www.google.com (www.google.com)|173.194.35.179|:80... connected.
  HTTP request sent, awaiting response... 404 Not Found
  2012-08-21 20:55:24 ERROR 404: Not Found.

Then, to see which convert your PHP uses:

exec(which convert 2>/tmp/whichconvert.2 1>whichconvert.1)

and

cat /tmp/whichconvert.{1,2}

Last, also try with the full path to convert:

exec(/usr/local/full/path/to/convert logo: 
-resize 640 /home/mysite/public_html/public/img/posts/original/1414_301a4.jpg);

and then

identify /home/mysite/public_html/public/img/posts/original/1414_301a4.jpg


Update:

The file that you meant to retrieve was probably not logo3w.png.jpg, but logo3w.png:

  wget http://www.google.com/images/srpr/logo3w.png
  --2012-08-21 21:04:22--  http://www.google.com/images/srpr/logo3w.png
  Resolving www.google.com (www.google.com)... 173.194.35.180, 173.194.35.177, 173.194.35.179, ...
  Connecting to www.google.com (www.google.com)|173.194.35.180|:80... connected.
  HTTP request sent, awaiting response... 200 OK
  Length: 7007 (6.8K) [image/png]
  Saving to: ‘logo3w.png’
  
  100%[=====================================================>] 7,007       --.-K/s   in 0.02s   
  
  2012-08-21 21:04:22 (451 KB/s) - ‘logo3w.png’ saved [7007/7007]

Also, in case it works for local files only (not remote http-URIs) you may need to check if the http delegate used by convert is indeed installed on your system:

convert -list delegate | grep http
      https =>          "/opt/local/bin/curl" -s -k -o "%o" "https:%M"

Plus, check if the user account your PHP runs under does indeed have the right to write to the target directory:

exec(touch /home/mysite/public_html/public/img/posts/original/touchtest);

and then

ls -l  /home/mysite/public_html/public/img/posts/original/touchtest

相关文章