首页 > 解决方案 > 与 PHP 一起使用时,Imagemagick 转换命令返回错误代码 4

问题描述

我正在使用 PHPexec函数执行 ImageMagick 命令,它返回一个error code 4意思probablyThe system cannot open the file当我在 Windows 终端中运行相同的命令时它工作正常。我正在使用以下命令来调整图像大小:

在终端(工作正常)

convert -resize 150^% ad.png res_ad.png

在 PHP 中(返回错误代码 4)

exec(escapeshellcmd("convert -resize 150% $file_name.png res_$file_name.png"), $output2, $return2)

PS:我已经检查并且图像的路径是正确的。

标签: phpwindowsimagemagickwamp

解决方案


我只使用 php 从 Imagemagick 返回了 1 或 0,所以我不知道 4 是从哪里来的。

我使用了与您类似的代码,但从未在 exec 中使用过 escapeshellcmd。exec 正在调用外部程序,我不确定您是否可以在那里使用它。

尝试(注意在大多数情况下输入图像在转换之后):

exec("convert $file_name.png -resize 150% res_$file_name.png");

您可以在将输入和输出图像文件名发送到 exec() 之前对其进行验证

不同的错误报告:

$array=array();
echo "<pre>";
exec("convert $file_name.png -resize 150% res_$file_name.png 2>&1", $array); 
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";

推荐阅读