Create fixed thumbnail using attachment_fu . Example with live pictures included

Send to friend

If you are in a hurry then take a look at following four links

Thumbnails with Rmagick for a normal picture

Thumbnails with Rmagick for a tiny picture

Thumbnails with MiniMagick for a normal picture

Thumbnails with MiniMagick for a tiny picture

Now the longer version.

attachment_fu makes it incredibly easy to upload pictures and to get thumbnails of the pictures. However getting the thumbnail of exact size is not straight forward and this trick is not mentioned in the README of the attachment_fu plugin.

Problem Definition

I want to get thumbnails of size 200×200. Here is the model.

class Picture < ActiveRecord::Base
  has_attachment  :max_size => 5.megabytes,
                  :content_type => :image,
                  :storage => :s3,                  
                  :processor => :MiniMagick, 
                  :thumbnails => { :default_200x200 => '200x200' }

  validates_as_attachment

end

If you upload a picture of height 551 and width 800 then the thumbnails we will get by Rmagick is
this one
and the thumbnail you will get by MiniMagick is
this one
.

Using Rmagick processor with attachment_fu

If you look at the documentation of attachment_fu you will notice that the thumbnail options are something like

:thumbnails => { :small => '10x10>',
                 :thumb => '100x100>' }

I could not understand what that greater than sign > meant at the end of ‘10×10>’.

This is what Rmagick documentation says.

Use > to change the dimensions of the image only if its width or height exceeds the geometry specification. < resizes the image only if both of its dimensions are less than the geometry specification. For example, if you specify ‘640×480>’ and the image size is 256×256, the image size does not change. However, if the image is 512×512 or 1024×1024, it is resized to 480×480.

I could not fully understand the documentation. I uploaded a picture with five different options. Let’s discuss a few of them.

option: 200×200

 
 :processor => :Rmagick, 
 :thumbnails => { :default_200x200 => '200x200' }

Rmagick is being asked to create thumbnail of dimension ‘200×200’. Be default Rmagick maintains the aspect ratio. And that is the reason by thumbnails that are created are not of exact size ( 200×200 ). Instead the thumbnail is of size (138×200)

option: 200×200!

 :processor => :Rmagick, 
 :thumbnails => { :ex_200x200 => '200x200!' }

The addition of an exclamation mark tell Rmagick to stretch the thumbnail to fit to the exact size mentioned . The end result is a thumbnail of the exact size but it is stretched and some people don’t like stretched pictures.

option: c200×200

 :processor => :Rmagick, 
 :thumbnails => { :crop_200x200 => 'c200x200' }
 

In this case Rmagick is being asked to create a thumbnail by cropping it.

Rmagick with five different options.

Click here and you will see a thumbnail created for each option. At the bottom of the page you will get the original image.

I also wanted to see what will happen if I upload a really small image. Here is the result

Using MiniMagick with attachment_fu

Most of options used for Rmagick also worked for MiniMagick except the cropping one. The option for cropping an image in MiniMagick is 200×200c while the option for cropping an image in Rmagick is c200×200. Notice the position of ‘c’.

Here is the result of experiment with MiniMagick .

Here is the MiniMagick working with a smaller image .