gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:50:23 26 4
gpt4 key购买 nike

嗨,大家好,在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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com