Executing imagemagick with PHP [SOLVED]

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
brianatlarge

Executing imagemagick with PHP [SOLVED]

Post by brianatlarge »

So I've got ImageMagick installed and it's working, except for what I'm trying to get it to do.

I'm running Ubuntu with PHP5.

I've got this PHP script:

Code: Select all

<?php
exec("/usr/local/bin/convert DSP_7201.JPG -resize 546x274^ -quality 80 temp.jpg");
?>
If I go through the terminal and run it using "php test.php" it runs it perfectly, but when running through the browser, it thinks a bit and then is done, but no file is made.

I thought that it was because www-data doesn't have the same permissions that my regular user does, so I did "sudo visudo" and first tried adding "www-data ALL=NOPASSWD: /usr/local/bin/convert" to the last line, and that didn't work, so then I tried "www-data ALL=NOPASSWD: ALL" and that still didn't work. So now I'm not sure if it's a permission issue at all.

Also, I've tried using the chown command to give the www-data user permissions for the directory I'm working out of, and I've also chmodded that directory to 777. Still no luck.

Then, I looked in my apache error log, and I'm getting this error:

Code: Select all

convert: unable to open image 'temp.jpg': @ error/blob.c/OpenBlob/2584
Google doesn't seem to yield any answers, and I can't think of any other reason why this wouldn't be working.

Suggestions?
Last edited by brianatlarge on 2010-11-23T15:38:24-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Executing imagemagick with PHP

Post by Bonzo »

Your problem could be the ^ what happens if you leave it out?

Try adding some error reporting:

Code: Select all

$array=array(); 
echo "<pre>";
exec("/usr/local/bin/convert DSP_7201.JPG -resize 546x274^ -quality 80 temp.jpg 2>&1", $array);  
echo "<br>".print_r($array)."<br>";  
echo "</pre>"; 
Where did you get the path to convert from ?
Confirm with this code:

Code: Select all

<?php 
echo "<pre>"; 
system("type convert");  
echo "</pre>"; 
?> 
brianatlarge

Re: Executing imagemagick with PHP

Post by brianatlarge »

The ^ fills the image with the largest of the two dimentions: http://www.imagemagick.org/Usage/resize/#fill

Ok, this is the output:

Code: Select all

Array
(
    [0] => convert: unable to open image `temp.jpg':  @ error/blob.c/OpenBlob/2584.
)

convert is /usr/local/bin/convert

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Executing imagemagick with PHP

Post by Bonzo »

I was forgetting its windows that you need to escape the ^

Your path to convert is OK

It works OK for me ( not that that helps much but proves the command is OK ) as you say it looks like a setup error of some sort. That I am afraid is beyond me; hopefuly somebody will be along later that can help.
brianatlarge

Re: Executing imagemagick with PHP

Post by brianatlarge »

Ok, so I got it to work.

So I changed

Code: Select all

<?php
exec("/usr/local/bin/convert DSP_7201.JPG -resize 546x274^ -quality 80 temp.jpg");
?>
to

Code: Select all

<?php
exec("/usr/local/bin/convert DSP_7201.JPG -resize 546x274^ -quality 80 /tmp/temp.jpg");
?>
and it wrote the file to /tmp

After that, I changed it to

Code: Select all

<?php
exec("sudo /usr/local/bin/convert DSP_7201.JPG -resize 546x274^ -quality 80 temp.jpg");
?>
And it worked perfectly! I had already added the www-data user to my sudoers file, I just wasn't using the sudo command.

Thanks
Post Reply