simplfuzz
simplfuzz

Reputation: 12915

Handling error output of jpegtran when called with php system command

    $cmd = 'jpegtran a.jpg > b.jpg';
    system($cmd);
    $newsize = filesize('b.jpg');
    if($newsize != 0) {
       copy('b.jpg','a.jpg');
    }

I want to run jpegtran on a bunch of images and copy the output to original file only if the command was successful.

When I run the above php code, for certain images I get errors like 'premature end of jpg file' or 'empty input file' etc.

How do I capture those errors and act accordingly in my code?

Upvotes: 0

Views: 1672

Answers (1)

mobius
mobius

Reputation: 5174

I am not sure how jpegtran returns on error but more usually the return code on error is something different than 0.

If so you could use exec() - http://php.net/manual/en/function.exec.php instead of system() and get the result of jpegtran in the return_var like so:

exec('jpegtran a.jpg > b.jpg', $result, $return_var);

if( $return_var ) {
  // Error log here
}

Edit: I forgot system() returns return_var as well. So the same can be accomplished with system('jpegtran a.jpg > b.jpg', $return_var);

Upvotes: 1

Related Questions