Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Updated to work with Rails 3 and latest version of Paperclip (2.3.8)
class GalleryImage < ActiveRecord::Base
belongs_to :property
has_attached_file :image, :styles => {:small => "300x300#",
:medium => "575x420>",
:small_thumb => "95x95#",
:thumb => "100x100#",
:large => "550x550>",
:featured => "296x477!"},
:default_style => :small,
:processors => [:rotator]
attr_accessor :rotation_degrees, :rotate
before_create :set_defaults
def rotate!(degrees = 90)
self.rotation += degrees
self.rotation -= 360 if self.rotation >= 360
self.rotation += 360 if self.rotation <= -360
self.rotate = true
def rotating?
!self.rotation.nil? and self.rotate
def set_defaults
self.rotation ||= 0
module Paperclip
class Rotator < Thumbnail
def transformation_command
if rotate_command
super + rotate_command
def rotate_command
target = @attachment.instance
if target.rotating?
" -rotate #{target.rotation}"

premjg commented May 2, 2011

There's no rotation method method defined.
Use self.rotation_degrees instead of self.rotation.


roomofwires commented May 2, 2011

Well, it works for me as is. Were you getting an error?

I have the same issue, rotation method is undefined.

Even after changing the self.rotation to self.rotation_degrees I get undefined method `[]' for :rotator:Symbol error. any help appreciated.

using rails 2.3.5, ruby 1.8.7, imagemagick and paperclip

thanks in advance

premjg commented Jun 4, 2011

@crazyscorpio12, can you post the log of your error and some code if possible.

undefined method [] for :rotator:Symbol error is fixed, it was a typo on my end. but, now, I am getting "convert: unrecognized option -rotate 90 @ error/convert.c/ConvertImageCommand/2373." my current imagemagick version is 6.6.2-6 i'll try again after i upgrade it. BTW, the rotate option is available

changing the [convert_options = "-rotate #{target.rotation}"]
in rotator.rb to
[convert_options = ["-rotate", "#{target.rotation_degrees}"]]
fixed the issue!

but some how it does not rotate the original image...


roomofwires commented Jun 5, 2011

Have you both added a rotation field to your model?

nope, I have not, adding it now and testing again. Thanks for pointing it out!


roomofwires commented Jun 5, 2011

You'll probably need to change your rotation_degrees changes back to how they were..?

@tekn0t, thanks a million! adding the rotation field to DB + restoring all changes fixed the issue, btw, for rotating the original image, I added an thumbnail "original" with size as empty, it seems to fix the issue.

Thanks again.


roomofwires commented Jun 5, 2011


I didn't have to add an original thumbnail to get it working, though.


roomofwires commented Jun 6, 2011

Just had a thought: Paperclip doesn't touch the original image, it uses it to create the rotated thumbnail or whatever styles you have. But I guess if you want the original image rotated, then what you have done (created a style called original) is a way of hacking it. But you may find that the angle stored in the rotation field doesn't correspond to the thumbnail rotation, as it will use your rotated original to created then new thumbnails. If that makes sense...?

I hope you don't mind, but I built upon your example here and added what I consider to be several improvements:


roomofwires commented Oct 25, 2011

Not at all! I adapted it from someone else anyway, but thanks for the mention!

dheza commented Dec 11, 2012

Hello, Guys, I

dheza commented Dec 11, 2012

Hello, Guys, I

change line 14 of rotator.rb to avoid string to array error-

[" -rotate #{target.rotation}"]

jwako commented Mar 7, 2013

I could rotate images. thanks a million.
but I use 3 styles such as :thumb, :large, :original like this:

has_attached_file :photo, 
    :styles => { :thumb  => "120x120#", :large => "640x480", :original => "1280x960" }, 
    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml", 
    :path => ":attachment/:id/:style.jpg",
    :processors => [:rotator]

In this case, when i tried to rotate the image 90 degrees,
the original image could rotate 90 degrees, but thumb and large images were rotated 180 degrees.
Is this because thumb and large images are based on the rotated original image ?
and how do I fix it ?

Thanks in advance.

jwako commented Mar 8, 2013

I just added the following code in temporary way. it works well for now.

if target_geometry.to_s == "1280x960" && rotate_command

If you have any other suggestions, I would appreciate it. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment