我正在使用PHP的exec()
来使用ImagicMagick的convert
转换图像。这是在CentOS服务器上运行。
exec(convert http://www.google.com/images/srpr/logo3w.png.jpg
-resize 640 /home/mysite/public_html/public/img/posts/original/1414_301a4.jpg);
使用exec()
不会导致新图像出现在目标文件夹中。但是,如果我在shell中运行相同的命令,它可以完美运行!
我认为这是一个路径问题。如果是这样,我如何检查PHP正在使用的路径,以及如何在PHP中设置正确的路径?
答案 0 :(得分:4)
function execute($command)
{
# remove newlines and convert single quotes to double to prevent errors
$command = str_replace(array("\n", "'"), array('', '"'), $command);
# replace multiple spaces with one
$command = preg_replace('#(\s){2,}#is', ' ', $command);
# escape shell metacharacters
$command = escapeshellcmd($command);
# execute convert program
return shell_exec($command); // or whatever you like
}
答案 1 :(得分:3)
首先,您应该尝试使用确实存在且可检索的输入文件:
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.
然后,查看您的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);
然后
identify /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 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]
此外,如果它只适用于本地文件(不是远程http-URI),您可能需要检查convert
使用的 http委托确实安装在你的系统上:
convert -list delegate | grep http
https => "/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
答案 2 :(得分:0)
对我有用的是提供ImageMagick
二进制文件的完整路径。而不是convert
,请使用/path/to/convert
。
$command = $this->imageMagickFullBinaryPath . ' -resize ' . $dimensions .
' ' . $imageFileToResize . ' ' . $resultingThumbnail;
exec($command, $output);