Rails, Paperclip, -auto-orient, and resizing...

April 09, 2010

I recently had a situation in which I wanted to generate thumbnail images and have them constrained to 67×50 (specifically “67×50>”). No problem so far, but I also wanted to auto rotate (via Imagemagick’s -auto-orient option) the images as they all come from a digital camera that provides this information. This causes a problem. Namely that some images ended up being 50×67 instead of 67×50.

I was using the following paperclip definition:

:styles => {:thumb => {:geometry => '67x50>', :format => :jpg, :convert_options => "-auto-orient"}

This resulted in a call to ImageMagick like this:

convert source -resize '67x50>' -auto-orient destination

The problem is the order in which the options are specified. The above first resized the image, then rotated it. This resulted in some of the images being 50×67.

The fix is a patch to Paperclip:http://github.com/thoughtbot/paperclip which adds :pre_convert_options. These options will appear after the source, but before the -resize options. Hopefully this will be included into Paperclip itself, but until then you can get the patch here.