Resizing with crop

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
porton
Posts: 5
Joined: 2008-10-04T07:37:42-07:00

Resizing with crop

Post by porton »

I have an image and a given size. Image must be changed to fit exactly into given size. It must be cropped (with -gravity Center) if either width or height does not fit into the given size. Resizing must be proportional. How?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Resizing with crop

Post by magick »

Use Resize() to resize the image. By default it maintains the aspect ratio. Use Extent() with a gravity of center to fill in any empty space. Something like Resize('400x400') and Extent(geometry=>'400x400', background=>'white', gravity=>'center').
porton
Posts: 5
Joined: 2008-10-04T07:37:42-07:00

Re: Resizing with crop

Post by porton »

magick wrote:Use Resize() to resize the image. By default it maintains the aspect ratio. Use Extent() with a gravity of center to fill in any empty space. Something like Resize('400x400') and Extent(geometry=>'400x400', background=>'white', gravity=>'center').
I don't understand how Resize() maintains the aspect ratio. AFAIK, Resize(geometry => "400x400") does not maintain aspect ratio. Is Resize('400x400') different than Resize(geometry => "400x400")?

Also I need (before cropping) to resize the image to the size probably more than the (final) size of crop, and only then to accomplish crop. Will your code do this?
porton
Posts: 5
Joined: 2008-10-04T07:37:42-07:00

Re: Resizing with crop

Post by porton »

magick wrote:Use Resize() to resize the image. By default it maintains the aspect ratio. Use Extent() with a gravity of center to fill in any empty space. Something like Resize('400x400') and Extent(geometry=>'400x400', background=>'white', gravity=>'center').
Oh, well, now I noticed that Resize() maintains the aspect ratio. But using Extent() for my task is wrong. Extent() fills borders with a background color. I instead need to resize the image so that it becomes of the minimal size such that it completely covers the given size (minimum size which is greater than the given size, maintaining aspect ratio), and crop the center (-gravity Center).

How?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Resizing with crop

Post by magick »

Perl is a scripting language. You can compute the dimension you need with Get('width') and Get('height') and then Resize(width=>???, height=>???).
Post Reply