Ryuzaki
Ryuzaki

Reputation: 15

Imagick `convert` works only in terminal and not in PHP

ACTUAL SITUATION

MacBook Pro early 2008

OSX Lion 10.7.3

MAMP 2.0.5

ImageMagick-x86_64-apple-darwin11.3.0.tar.gz - installed and perfectly working from Terminal

gplgs-8.71.dmg - installed and perfectly working from Terminal and Imagick

WHAT WORKS?

Using terminal the "convert" command works perfectly! I can convert PDF in JPG without any problem...

WHAT IS THE PROBLEM?

If I try to use Imagick in PHP lunching the (simplest) demo command:

"convert logo: logo.gif"

Nothing happens! I followed this guide step by step and I know that I've to modify the "envvars" file and I made it but... it is not the solution!

I tried to read shell errors but nothing is returned in PHP... I tried all kind of commands:

define('MAGICK_PATH', '/Applications/MAMP/bin/ImageMagick/ImageMagick-6.7.5/bin/');

echo exec(MAGICK_PATH.'convert logo: logo.gif', $output);
var_dump($output);
=> array(0) { }


$output = shell_exec(MAGICK_PATH."convert logo: logo.gif");
echo "<pre>$output</pre>";
=> *nothing*


$last_line = system(MAGICK_PATH.'convert logo: logo.gif', $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
=> Last line of the output:
=> Return value: 5


$last_line = system(MAGICK_PATH.'convert -version', $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
=> Last line of the output:
=> Return value: 5


$last_line = system(MAGICK_PATH."convert -colorspace RGB -interlace none -density 104.6x104.6 -quality 100 -bordercolor white doc.pdf[0] doc.png", $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
=> Last line of the output:
=> Return value: 5

I thought maybe the program wasn't lunched but...I can see it in the task manager and the CPU is working for some seconds, Imagick is doing something but in the end I haven't any kind of output!!! >_< I found a lot of topic about that but I haven't found a working solution...

Upvotes: 0

Views: 1045

Answers (2)

halfer
halfer

Reputation: 20440

Also, as an addendum to my earlier comment: try supplying fully-qualified paths to your input and output filenames. Commands are launched in PHP with a default current working directory - and this may not be the same dir that the script is actually contained within.

If you want to see what your default directory is for system commands, try:

exec('pwd');

If any of your paths have spaces in (or if at any stage they contain user input) you should run your parameters through escapeshellarg() to make sure they will work (and that they are safe to run).

Upvotes: 0

Cal
Cal

Reputation: 7157

Try wrapping commands like this:

exec($cmd." 2>&1", $out, $ret);
if ($ret){
    echo "There was a problem!\n";
    print_r($out);
}else{
    echo "Everything went better than expected!\n";
}

exec() lets you capture all output and get the exit code. Adding 2>&1 makes sure to redirect STDERR to STDOUT so you can see any error messages.

Upvotes: 1

Related Questions