Fang
Fang

Reputation: 334

PHP exec ImageMagick always returns 0

I'm trying to get ImageMagick to count the amount of pages in a PDF file for me. The function is as follows:

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

However, that function always returns 0. I have verified that ImageMagick is running properly, so that shouldn't be a problem. Am I not using exec() properly? Should I retrieve the output in another way? I've also tried using $debug, but that didn't give me any output, oddly.

I bet I'm doing something stupid here, but I just don't see it. Can anyone give me a push in the right direction? Thanks!

Upvotes: 0

Views: 681

Answers (1)

jrennie
jrennie

Reputation: 1957

As noted in the man page, exec provides the return status of the executed command via the third argument. A value of 0 means that it exited normally. It sounds like you should be using something like popen.

Here's an example lifted from Example #3 of the fread man page (edited to use 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
?>

Upvotes: 1

Related Questions