PHP exec resize

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
ncoo

PHP exec resize

Post by ncoo »

Brand new to IM, known about it for ages, but finally decided to take the plunge, looks good so far.

Having seen the comand line code below I want to convert it into PHP.

I must be doing something wrong, I've checked the help pages but can't find out how to do this.

convert logo: \
-resize x160 -resize '160x<' -resize 50% \
-gravity center -crop 80x80+0+0 +repage space_fill.jpg

exec("convert test/68a.jpg: \
-resize x160 -resize '160x<' -resize 50% \
-gravity center -crop 80x80+0+0 +repage done/68a.jpg",$report);
print_r($report);


I've managed to so simple things like:

system('convert "test/68b.jpg" -resize 400 "done/68b.jpg"');

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

Re: PHP exec resize

Post by Bonzo »

As you are going over multiple lines and using php you need \\ not just \ as the \ needs "escaping".

test/68a.jpg: does not need the : at the end; this is only for ImageMagick built in image.

You have resize three times why ?

Try:

Code: Select all

exec("convert test/68a.jpg -resize 50% \\
-gravity center -crop 80x80+0+0 +repage done/68a.jpg",$report);
print_r($report);
ncoo

Re: PHP exec resize

Post by ncoo »

The first two resizes test the image so that the crop image is as large as possible when cropped. So that the image can fill any shape and keep the ratio.

That exec() didn't work for me.

http://www.imagemagick.org/Usage/resize/#space_fill

convert logo: \
-resize x160 -resize '160x<' -resize 50% \
-gravity center -crop 80x80+0+0 +repage space_fill.jpg
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PHP exec resize

Post by Bonzo »

I didn't know about the resize to fill a space; but then again there is a lot I do not know :shock:

Did you use my modified code ?

Try:

Code: Select all

exec("convert test/68a.jpg \\
-resize x160 -resize '160x<' -resize 50% \\
-gravity center -crop 80x80+0+0 +repage done/68a.jpg",$report);
print_r($report);
ncoo

Re: PHP exec resize

Post by ncoo »

That was the feature that convinced me to have a good look at IM.

Tried both modified codes still no image and the array is blank.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PHP exec resize

Post by Bonzo »

I would try putting the code all onto one line then changing your error reporting to this and see what you get.

Code: Select all

exec("convert test/68a.jpg -resize x160 -resize '160x<' -resize 50% -gravity center -crop 80x80+0+0 +repage done/68a.jpg 2>&1", $array); 
echo "<br>".print_r($array)."<br>"; 
echo "</pre>"; 
One thing I have just noticed is your crop could be the same size as your image so the code could fail on that. You may get an image but not what you expect.
ncoo

Re: PHP exec resize

Post by ncoo »

I was just using the sample resizes to get it working, that 80x80, 160 50% is taken straight from the site, so I expect it should work, they've got the sample image next to it.

That still returns an empty array.

I was just browing your site, you can do some great stuff with IM. Like the sunflower one.

Maybe it might be easier to do the maths, then resize and then crop. So PHP does more of the work?
ridera

Re: PHP exec resize

Post by ridera »

Your syntax is wrong. Try this:

$cmd_debug= (TRUE)? '-debug exception' : ''; //or false; the '' is two single quote marks. A NULL should work also

$command= "IM command string"; //your string on a single line
$file //php path rule to file

exec("convert $cmd_debug $command $file", $IMarray, $code);

print_r($IMarray);

Actually, I recommend using Imagick, the php extension. Syntax is much easier to deal with.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PHP exec resize

Post by Bonzo »

Interesting piece of code ridera; would you mind if I put it on my website ?

Anyway running the code of ridera's and mine I generate an image with both lots of code but with mine I output a "1" which is a fail. Although it does create an image but the error array in both cases is empty.

Array
(
)

Array ( )
1

Code: Select all

<?php

$cmd_debug= (TRUE)? '-debug exception' : ''; //or false; the '' is two single quote marks. A NULL should work also

$command= "sunflower.jpg -resize x160 -resize \"160x<\" -resize 50% -gravity center -crop 80x80+0+0 +repage"; //your string on a single line
$file = "68a.jpg";//php path rule to file

exec("convert $cmd_debug $command $file", $IMarray, $code);

echo "<pre>";
print_r($IMarray);
echo "</pre>";

exec("convert sunflower.jpg -resize x160 -resize \"160x<\" -resize 50% -gravity center -crop 80x80+0+0 +repage 168a.jpg 2>&1", $array); 
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";

?>
Thats interesting if I echo $code; in rideras example it outputs 0 which is success !

Array
(
)
0

Array ( )
1

Are the 1 returned and the 0 returned the same thing ?
Post Reply