Simple image rotation with Paperclip and Amazon S3

For a site I have been working on we are providing a way for members to upload images, and then subsequently rotate them if required (how many times have you just added a photo from your camera and forgotten to rotate the ones taken in portrait?)….

Originally the site was going to be running on a standard Linux-based host with regular file system access, however it was decided during the development process to run the site on Heroku . Now Heroku, in short, is an amazing platform – simple, powerful and inexpensive – but because of its architecture there is a limitation of a read only file system. That basically means that the only place your application can write to is the tmp directory.

For all apps which I work on requiring file uploads I use Paperclip , from Thoughtbot . Paperclip has a nifty solution which gets me around the Heroku ROFS issue – S3 storage. With a few simple configuration adjustments you can have all your files uploaded stored into an Amazon S3 bucket.

But, with the image rotation this caused a bit of an issue. Originally the image was being re-opened, and rotated for each style we have in the attachment, using RMagick2. While this worked it was a) a bit ‘manual’ and b) had a dependency on the file existing with the local file system.

The solution to this was to stop using RMagick directly, and move onto a custom Paperclip Processor. This isn’t actually as daunting as it sounds. Its as easy as inheriting from a base class (in this case, the built in Thumbnail process which Paperclip provides), and overriding a method which builds up the commands.

There is already a great example of this for image cropping on Railscasts (Episode 182, Cropping an Image), which goes into all the details – and the code I’ve used for rotation was an adaptation of this.

You can find the code as a Gist on GitHub at http://gist.github.com/206145 – hopefully this will be helpful to others who need to do the same thing. Once again, I’m astounded by the flexibility of the framework and plugins I work with.

  • egarcia

    I used your code (which was very useful, thanks!) but noticed that my rotated thumbnails looked "bigger" in some occasions. This happened because the "thumbnailing" happened before the rotation.

    In order to fix this, I had to change the order in which the operators are sent to Imagemagick – first rotate, then thumbnail (super).

    I’ve made a fork of your gist here:

    http://gist.github.com/238906

    I’d send you a pull request, but I don’t know how to do those on gists.

    Thanks and best regards!

  • egarcia

    I used your code (which was very useful, thanks!) but noticed that my rotated thumbnails looked "bigger" in some occasions. This happened because the "thumbnailing" happened before the rotation.

    In order to fix this, I had to change the order in which the operators are sent to Imagemagick – first rotate, then thumbnail (super).

    I’ve made a fork of your gist here:

    http://gist.github.com/238906

    I’d send you a pull request, but I don’t know how to do those on gists.

    Thanks and best regards!

  • Cédric Bousmanne

    I used your code but ran into a “Can’t convert String into Array” error with rails 3.0.4.

    I’ve made a fork with my update here :

    https://gist.github.com/1159037

    Hope it’ll help someone

    Cédric