php - 捕获imagemagick的正确方法转换PHP exec中的错误

标签 php error-handling imagemagick

嗨,大家好,在php exec()中捕获imagemagick转换错误的正确方法是什么?

exec('convert img.jpg -resize 100x100 img.jpg',$output);
var_dump($output);
$output始终返回一个空白数组。

最佳答案

对于exec,评估返回状态;这是第三个参数。

exec('convert img.jpg -resize 100x100 img.jpg',$output,$exit_status);
if( $exit_status !== 0 ) {
  // Error handle here
}

但是,$output数组中可能不存在写入的任何内容。更好,更灵活的方法是使用proc_open和管道。

$pipe_handle = array();
$pipe_spec = array(
  array('pipe','r'), // stdin
  array('pipe','w'), // stdout
  array('pipe','w')  // stderr
);

$pid = proc_open('convert img.jpg -resize 100x100 img.jpg',$pipe_spec,$pipe_handle);
// Close stdin
fclose($pipe_handle[0]);
// Read what's in stdout buffer & close
$pipe_stdout = stream_get_contents($pipe_handle[1]);
fclose($pipe_handle[1);
// Read what's in stderr buffer & close
$pipe_stderr = stream_get_contents($pipe_handle[2]);
fclose($pipe_handle[2]);

// Get exist status
$exit_status = proc_close($pid);

if( $exit_status === -1 ) {
  // Handle error and evaluate stderr
} else {
  // Handle success and evaluate stdout
}

需要更多的工作,但是可以将错误与正常消息(如果有的话)完全区分开

关于php - 捕获imagemagick的正确方法转换PHP exec中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23806604/

相关文章:

javascript - 将 JSON 响应放入 var 并输出

ruby - 比较两张图像,其中一张被稍微裁剪(缩放)

php - ImageMagick - 文本到矩形

php - 通过 PHP 函数截断 MySQL 表不起作用

php - Zend_Validator_Regex 抛出错误 : Internal error while using the pattern

php - 来自 PHP 的 mp4 - 不在 HTML5 视频标签中播放

r - 插入符::varImp for GLM model worked before and gives me an error

powershell - 命令完成后如何获取ReturnValue?

javascript - 在删除具有相同 key 的文档后,如何通过使用Mongoose的deleteOne方法和 key 来获取错误?

imagemagick - 将许多 png 转换为多页 tiff