PHP exec ImageMagick 总是返回 0


PHP exec ImageMagick always returns 0

我正在尝试让ImageMagick为我计算PDF文件中的页数。函数如下:

<?php
function countPdfPages($filepath)
{
    $magick = "identify -format %n ".$filepath;
    exec($magick, $debug, $result);
    return $result;
}
?>

但是,该函数始终返回 0 。我已经验证了ImageMagick是否正常运行,所以这应该不是问题。我没有正确使用exec()吗?我应该以其他方式检索输出吗?我也尝试过使用 $debug ,但奇怪的是,这并没有给我任何输出。

我敢打赌我在这里做了一些愚蠢的事情,但我只是没有看到它。谁能给我一个正确的方向?谢谢!

如手册页中所述,exec通过第三个参数提供已执行命令的返回状态。 值为 0 表示它已正常退出。 听起来你应该使用类似的东西 popen .

下面是从 fread 手册页的示例 #3 中提取的示例(编辑为使用 popen):

<?php
// For PHP 5 and up
$handle = popen("identify -format %n myfile.jpg", "r");
$contents = stream_get_contents($handle);
pclose($handle);
// $contents is the output of the 'identify' process
?>