嗨,大家好,在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
数组中可能不存在写入stderr的任何内容。更好,更灵活的方法是使用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/