Max
Max

Reputation: 13334

capture imagemagick command output in php

I'm running the below imagemgaick command which outputs to stdout:

compare <img1> <img2> -metric MAE null:

I'm trying to capture the output of this command from PHP. Normally I use the exec($cmd,$output) commands which stops cmd output from going to stdout and instead places into the $output array. However for some reason here that doesn't, output still goes to stdout and the output array is empty.

Any idea how to workaround this issue?

Thanks.

Upvotes: 1

Views: 869

Answers (2)

jmz
jmz

Reputation: 5479

Compare prints to STDERR.

Use:

exec("compare <img1> <img2> -metric MAE null: 2>&1", $output);

Upvotes: 0

reko_t
reko_t

Reputation: 56430

exec() only places the output to STDOUT in the output array, however various imagemagick tools also output messages to STDERR. You can redirect messages from STDERR to STDOUT (and hence also get them in $output array), by appending this at the end of your command: 2>&1

Alternatively if you want to be able to differentiate where the messages were originally output, you can use proc_open that allows you to specify separate pipes for STDOUT and STDERR, and capture output from them separately.

Upvotes: 2

Related Questions