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.