Resize Image, Keep aspect ratio and fill blank space

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
carlosgoce
Posts: 9
Joined: 2012-04-25T07:37:26-07:00
Authentication code: 13

Resize Image, Keep aspect ratio and fill blank space

Post by carlosgoce »

Hello,

First of all, excuse my English. I was trying for three days and tried everything and couldn't solve this.

I just want to scale random images (portraits and landscapes) to a determined size for making thumbnails. Here is the problem:
If i want a size of 100x100 its impossible to fit a image of 400x100 on it, so, i want to scale the picture to 100x100 but keeping the aspect ratio. On the left and right side i want a color of my choice. It looks quite simple but i cant do it.

I tried with this code that i found on the imagemagick website but is not working for me, my picture is always on the left side and the background on the right.
convert input.jpg -resize 800x600 -background black -compose Copy -gravity center -extent 800x600 output.jpg

Seems like -gravity center is not working.

Here is a example of what i get.
Image
I just want the picture on the middle of the background.

Thank you for the help
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize Image, Keep aspect ratio and fill blank space

Post by fmw42 »

try removing the -compose copy

convert input.jpg -resize 800x600 -background black -gravity center -extent 800x600 output.jpg

If that does not work, then please identify your IM version and platform. You may need an upgrade of IM
carlosgoce
Posts: 9
Joined: 2012-04-25T07:37:26-07:00
Authentication code: 13

Re: Resize Image, Keep aspect ratio and fill blank space

Post by carlosgoce »

When i try with your code i get the following error:
"unable to open image '800x600'

I'm using ImageMagick-6.7.6-7 on a 1&1 server. The SO is Linux Debian 6.0.4.

But i'm testing on Windows 7 with Xampp and i think imagemagick version 6.3 or above.

EDIT:
Yeah you was right, it works perfect on the Debian server. So, its because Imagemagick version i suppose...

Thank you very much.

But i have a last question if someone can answer me:
convert input.jpg -thumbnail 1000 output.jpg - works OK. 1000 is the width and the height is auto
convert input.jpg -thumbnail 1000x0 output.jpg - the same as above
convert input.jpg -thumbnail 0x1000 output.jpg - does not work. why?

I want to give only the height

EDIT2:
fixed with convert input.jpg -thumbnail x1000 output.jpg
carlosgoce
Posts: 9
Joined: 2012-04-25T07:37:26-07:00
Authentication code: 13

Re: Resize Image, Keep aspect ratio and fill blank space

Post by carlosgoce »

Here i'm again!

One last problem (i hope). With the previous code it works ok

Code: Select all

convert input.jpg -resize 800x600 -background black -compose Copy -gravity center -extent 800x600 output.jpg 
The thing is, when the height is really low imagemagick do a strange thing. Just see this:
This is width:600 and height:250
Image

This is what happens when i set width:600 and height:100
Image

Any idea to fix this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize Image, Keep aspect ratio and fill blank space

Post by fmw42 »

what is the exact command you used? also post a link to your input image.

type

convert -version

what does that tell you about your version.
carlosgoce
Posts: 9
Joined: 2012-04-25T07:37:26-07:00
Authentication code: 13

Re: Resize Image, Keep aspect ratio and fill blank space

Post by carlosgoce »

The command is

Code: Select all

convert input.jpg -resize 300x50 -background white -gravity center -extent 300x50 output.jpg
Version: ImageMagick 6.6.0-4 2012-03-05 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP
The OS is Linux Debian 6.0.4.

When i use this command by SSH it works perfect, when i try to execute the command from PHP i get the deformed picture.
I have the same command on my php:

Code: Select all

 
exec
("convert input.jpg -resize 300x50 -background white -gravity center -extent 300x50 output.jpg");
Header("Content-Type: image/jpeg");
readfile("output.jpg");
The "test image" size is 12 MB (i want to use pictures from 1-10 MB) with pictures less than 10 MB seems to works fine. But its really weird, as i said, executing the command from the SSH works perfect.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize Image, Keep aspect ratio and fill blank space

Post by fmw42 »

try adding the correct path to convert (typically /usr/bin or /usr/local/bin

find out how many IM versions you have:

type -a convert

If more than one, select the most current and then put the path to convert into your command. See if that helps.

Also trap for error messages:

exec("path2/convert input.jpg -resize 300x50 -background white -gravity center -extent 300x50 output.jpg 2>&1", $out, $returnval);
foreach($out as $text)
{echo "$text<br>";}
carlosgoce
Posts: 9
Joined: 2012-04-25T07:37:26-07:00
Authentication code: 13

Re: Resize Image, Keep aspect ratio and fill blank space

Post by carlosgoce »

Hi Again, thank you very much for your help.

Code: Select all

<?php
    exec("/usr/bin/convert tal.jpg -resize 100x100 -background white -gravity center -extent 300x50 nuevo.jpg 2>&1", $out, $returnval);
    foreach($out as $text)
    {echo "$text<br>";}
?>
When i try with this, i get this error:

Code: Select all

Warning: exec() [function.exec]: Unable to fork [/usr/bin/convert input.jpg -resize 100x100 -background white -gravity center -extent 300x50 output.jpg 2>&1] in /homepages/url/to/mysite.php on line 2
By the way, what means the "2>&1" ?

AGAIN, if i try directly from SSH it works! I don't get it.
Should i try with another thing instead of exec?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize Image, Keep aspect ratio and fill blank space

Post by fmw42 »

It would appear that there may be some fault in your PHP config.

What exactly did you get from

type -a

Did it show more than one path to convert?

Try something simple

<?php
exec("/usr/bin/convert -version" 2>&1,$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}

?>

If that works, then

<?php
exec("/usr/bin/identify -verbose rose: 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>

The 2>&1 allows the IM command to capture error messages in the $out.

Also see what happens if you use /usr/local/bin. Perhaps there is another version of IM available
carlosgoce
Posts: 9
Joined: 2012-04-25T07:37:26-07:00
Authentication code: 13

Re: Resize Image, Keep aspect ratio and fill blank space

Post by carlosgoce »

Hello,

when i use /usr/bin/convert
i only get 1 directory:
/usr/bin/convert

But i recently instaled imagick 6.7 on my home directory dont know why dont appears on the list. I tried to use fullpathto/myhome/directory/bin/convert and still the same issue. Maybe the instalation is wrong?

I going to try your code now.

EDIT:
I get this error
Parse error: syntax error, unexpected T_LNUMBER in /mywebsite/test.php on line 2

Using

Code: Select all

    
exec("/usr/bin/convert -version" 2>&1,$out,$returnval);
    foreach($out as $text)
    {echo "$text<br>";}
EDIT2:
Fixed moving the " "

I get this
Version: ImageMagick 6.6.0-4 2012-03-05 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP

And with my home instalation:
Version: ImageMagick 6.7.6-7 2012-04-26 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

With the other Script:

Code: Select all

exec("/usr/bin/identify -verbose rose: 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
I get this on response:
Image: ROSE
Base filename:
Format: ROSE (70x46 Truecolor rose)
Class: DirectClass
Geometry: 70x46+0+0
Resolution: 72x72
Print size: 0.972222x0.638889
Units: Undefined
Type: TrueColor
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
Red:
min: 35 (0.137255)
max: 255 (1)
mean: 145.712 (0.57142)
standard deviation: 69.2953 (0.271746)
kurtosis: -1.385
skewness: 0.14637
Green:
min: 22 (0.0862745)
max: 255 (1)
mean: 89.2602 (0.35004)
standard deviation: 52.4698 (0.205764)
kurtosis: 2.63898
skewness: 1.81893
Blue:
min: 24 (0.0941176)
max: 255 (1)
mean: 80.4683 (0.315562)
standard deviation: 55.1114 (0.216123)
kurtosis: 3.01818
skewness: 1.9795
Image statistics:
Overall:
min: 22 (0.0862745)
max: 255 (1)
mean: 78.8602 (0.309256)
standard deviation: 73.1284 (0.286778)
kurtosis: 0.0669201
skewness: 0.96904
Rendering intent: Undefined
Interlace: None
Background color: white
Border color: rgb(223,223,223)
Matte color: grey74
Transparent color: black
Compose: Over
Page geometry: 70x46+0+0
Dispose: Undefined
Iterations: 0
Compression: Undefined
Orientation: Undefined
Properties:
date:create: 2012-04-27T00:42:15+02:00
date:modify: 2012-04-27T00:42:15+02:00
signature: a698f2fe0c6c31f83d19554a6ec02bac79c961dd9a87e7ed217752e75eb615d7
Artifacts:
verbose: true
Tainted: False
Filesize: 9.67KB
Number pixels: 3.22KB
Pixels per second: 161KB
User time: 0.040u
Elapsed time: 0:01.020
Version: ImageMagick 6.6.0-4 2012-03-05 Q16 http://www.imagemagick.org

And with my imagemagick at home/imagick6.7 :
Image: ROSE
Base filename:
Format: PPM (Portable pixmap format (color))
Class: DirectClass
Geometry: 70x46+0+0
Resolution: 72x72
Print size: 0.972222x0.638889
Units: Undefined
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
Red:
min: 35 (0.137255)
max: 255 (1)
mean: 145.712 (0.57142)
standard deviation: 69.2953 (0.271746)
kurtosis: -1.385
skewness: 0.14637
Green:
min: 22 (0.0862745)
max: 255 (1)
mean: 89.2602 (0.35004)
standard deviation: 52.4698 (0.205764)
kurtosis: 2.63898
skewness: 1.81893
Blue:
min: 24 (0.0941176)
max: 255 (1)
mean: 80.4683 (0.315562)
standard deviation: 55.1114 (0.216123)
kurtosis: 3.01818
skewness: 1.9795
Image statistics:
Overall:
min: 22 (0.0862745)
max: 255 (1)
mean: 105.147 (0.412341)
standard deviation: 59.4199 (0.233019)
kurtosis: 1.2733
skewness: 1.45983
Rendering intent: Perceptual
Gamma: 0.45455
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Interlace: None
Background color: white
Border color: srgb(223,223,223)
Matte color: grey74
Transparent color: black
Compose: Over
Page geometry: 70x46+0+0
Dispose: Undefined
Iterations: 0
Compression: Undefined
Orientation: Undefined
Properties:
date:create: 2012-04-27T00:44:41+02:00
date:modify: 2012-04-27T00:44:41+02:00
signature: a698f2fe0c6c31f83d19554a6ec02bac79c961dd9a87e7ed217752e75eb615d7
Artifacts:
filename: rose:
verbose: true
Tainted: False
Filesize: 9.67KB
Number pixels: 3.22K
Pixels per second: 32.2TB
User time: 0.000u
Elapsed time: 0:01.000
Version: ImageMagick 6.7.6-7 2012-04-26 Q16 http://www.imagemagick.org
carlosgoce
Posts: 9
Joined: 2012-04-25T07:37:26-07:00
Authentication code: 13

Re: Resize Image, Keep aspect ratio and fill blank space

Post by carlosgoce »

I think that i have an idea.

The problem is with big pictures and now i get better results using -thumbnail instead of -resize. But the biggest pictures are still a problem.

Wich command is the more fast and less processor-eater (don't know how to say that on english) to resize?


EDIT:
Finally i found my solution for large images. It needs lees memory to do it and works fine.
This is my code:

Code: Select all

            
$conversion = "convert -quality 90% input.jpg";
exec("$url.to/convert $conversion -scale 100x100 -background white -gravity center -extent 100x100 output.jpg");
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Resize Image, Keep aspect ratio and fill blank space

Post by anthony »

Fastest resize is -sample, but the results are horrible
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize Image, Keep aspect ratio and fill blank space

Post by fmw42 »

-scale will not give as good quality as -resize or -thumbnail. -thumbnail will be slightly faster and write a smaller file as it does not save much meta data.

P.S. Sorry about the misplaced quote.
Post Reply