Nullcreations.net | Ramblings & Photography of Jerrett Taylor

crop_resized in RMagick

December 23rd, 2005 | 0 comments

One thing ImageMagick (and thus RMagick) seemed to be missing was to be able to give a desired width and height to an image, and have it resized to exactly that width and height – without distortion.

If you use resize, it will give you the correct sized image, but it will be distorted unless the aspect ratio before/after is the same. The solution to this is to use change_geometry to get dimensions that won’t distort, but then you are no longer getting exactly the width/height you wanted… which can be a problem with thumbnails.

To solve this I created a crop_resized method which takes the width and height, resizes based on the smaller edge, and then crops the excess. This way your image will always be the desired size, without distortion. This works great for when you are creating thumbnails for photo sites or anything else that requires undistorted images all the same size, and is what I used on this site for the photography stuff.

I found myself copying this code for a couple different things I was using it on, and then I saw somebody ask how to accomplish this on a mailing list, so I sent this over to Tim (developer of RMagick) and after passing it back and forth a couple times we arrived at an even better solution which now accepts a third paramater, an ImageMagick gravity constant which instructs it where to crop from. (NorthWestGravity, CenterGravity, etc.)

Tim has already put this new method into cvs, so the next version of RMagick will contain crop_resized(width,height,gravity) and crop_resized!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Magick
  class Image

    def crop_resized(ncols, nrows, gravity=CenterGravity)
      copy.crop_resized!(ncols, nrows, gravity)
    end

    def crop_resized!(ncols, nrows, gravity=CenterGravity)
      if ncols != columns || nrows != rows
        scale = [ncols/columns.to_f, nrows/rows.to_f].max
        resize!(scale*(columns+0.5), scale*(rows+0.5))
      end
      crop!(gravity, ncols, nrows, true) if ncols != columns || nrows != rows
      self
    end
  end
end

0 Comments

Musings

04.14.08

Trying to decide where to travel to? Wikitravel.org Random Page has the answers!

02.23.08

sd's embedded_actions has a new home on github

02.18.08

Very cool.

02.17.08

Open Source Food? - looks like a nice recipe site!

02.15.08

For anyone using capistrano with more than one target for deployment (staging, production), check out cap multistage

02.15.08

Wow, I just realized how bad my code blocks break my blog in safari. I should fix that!

01.27.08

Mike has created another nice netbeans theme - go check it out, if you use netbeans!

01.21.08

A handy tool, Rubular is a web based regex editor

01.15.08

Dan put together a sweet code editor plugin for mce with syntax highlighting etc.

12.13.07

On OSX? Add RubyImporter to Spotlight and search your source!

12.04.07

sd has released a new version of embedded_actions

11.27.07

RejectConf videos are online thanks to confreaks!

11.19.07

Good news - we upgraded a project at work to rails 2.0 rc2 and the speed to run specs dropped from 30-35 seconds to 10-15 seconds!

11.08.07

Eigenclass.org has a great big list of Ruby 1.9 changes that is being kept up to date!

11.06.07

Rein is working on a trac replacement in ruby called eskort , and it looks cool. More info on his goals