The subject of cropping and resizing images comes up fairly often in the Rails community, with a number of tools out there to automate these manipulations.

When manipulating images, I like to use MiniMagick to keep the memory footprint of the application server itself small. Since MiniMagick just wraps the command-line convert and mogrify tools, special-casing your image resizer is as simple as modifying the flags passed to the underlying system command.

There are a number of resizing algorithms in use. Various approaches include maintaining the original image’s aspect ratio, cropping over-sized images, and padding resized images.

In order to achieve good fit an image may need to be both scaled and cropped. A well-fit image will maintain as much of the original image as possible while still conforming to the final target dimensions and aspect.

To illustrate the desired results, we’ll be manipulating the speakersitter image.
Original 300 × 586

Check out some examples of the various approaches to fixed-size output.

  1. With over-sized images, by far the simplest approach is the naive crop. Unfortunately, this fails us when the image is too small, and looks pretty terrible in most cases anyway.

    Cropped to 150 × 90

  2. Next up is the naive scale. This approach is nice in that it delivers consistent results no matter what the image’s original dimensions were. On the down side, this consistent behavior typically sucks.

    Scaled to 150 × 90

  3. Padding can be a nice way of getting generally good-looking output. A padding algorithm will scale an image maintaining its aspect ratio so that none of its dimensions is greater than the corresponding output dimension, and then will composite that scaled image onto the center of a blank image of the target dimensions. In this example, the checkerboard pattern represents the background image onto which the original image is composited. In real world usage, this image would be pure white or transparent.

    Scaled to 46 × 90, Padded to 150 × 90

  4. The good fit approach is similar to the padding approach, in that the source image is immediately scaled with a fixed aspect ratio. In the padding approach the image’s dimensions such that none is greater than its corresponding output dimension, both are constrained such that none is less than the output dimension. This guarantees that part of the source image will be cropped unless it is exactly the same aspect as the output dimensions.

    The scaled source image can be cropped according to one of nine different gravities. If your application deals primarily with portraits and face images, North gravity is typically the best.

    Perfect Fit 150 × 90 North Gravity

    For most other applications, including avatar and thumbnail generation, Center Gravity can be preferable.

    Perfect Fit 150 × 90 Center Gravity

Sadly, MiniMagick doesn’t support doing this all from the command line without a little pre-computation. We must sadly scale and then crop according to offsets we compute ourselves. Fortunately, I have already taken care of this for you!

I monkey-patched these changes directly into attachment_fu‘s mini_magick_processor, but the calculations and generated commandline flags should serve you wherever you use MiniMagick.

span class="st0">''
    # Resize image to minimize difference between actual dimension and target dimension
"#{g.width.to_i}x""x#{g.height.to_i}"'+matte''NorthWest'"#{g.width.to_i}x#{g.height.to_i}+#{width_offset}+#{height_offset}!""Gravity must be one of #{GRAVITY_TYPES.inspect}""Original dimensions must be supplied as a [ width, height ] array""Cropped dimensions must be supplied as a [ width, height ] array"# No vertical offset for northern gravity


  1. Cher

    How does my model’s thumbnail code need to change to include the gravity?

  2. duncanbeevers

    It’s been some time since I flexed any of this code, but glancing over the attachment_fu code (still at the February 2008 version) it seems it could take a bit of work to forward gravity information all the way through attachment_fu. Take a look at how thumbnail_resize_options is threaded through attachment_fu to crop_and_resize_image within the minimagick processor.

  3. Jason Kotchoff

    This didn’t maintain the correct gravity orientation for me when I used this exact code. I needed to remove the:

    line of code to make it work.

Leave a Comment

Enclose code in <code lang="ruby"></code> if you care.
Preview your comment using the button below.