A collection of computer systems and programming tips that you may find useful.
 
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Tuesday, May 11, 2010

Image Resizing with Rails Paperclip and ImageMagick

The Rails Paperclip plugin uses ImageMagick for creating thumbnail images, etc.

You specify the size of images using ImageMagick's Geometry syntax which can be a little confusing. Here are the aspects of that which are most useful for Paperclip users.

The basic specification is <width>x<height> in pixels, optionally followed by a modifier. In some cases you can omit either width or height.

  • 256x256

  • This specifies the Maximum dimensions for the image, while preserving the aspect ratio of the image. So if your image were actually 512x256 it would be resized to 256x128 in order to fit inside the specified size.

  • 256x256!

  • This specifies the Exact image size, so a 512x256 image would be changed into 256x256, losing the aspect ratio.

  • 256x

  • This specifies the desired width of the target image, leaving the height to be set so as to preserve the aspect ratio.

  • x256

  • This specifies the desired height of the target image, while preserving the aspect ratio.

  • 256x256^

  • This specifies the Minimum size for the target image, so the resized image may be larger than these dimensions.

  • 256x256>

  • This specifies that the image will be resized only if it is Larger than the dimensions.

  • 256x256<

  • This specifies that the image will be resized only if it is Smaller than the dimensions.


Other options exist within the syntax. See the ImageMagick docs for more details.


 

Archive of Tips