Create a gist now

Instantly share code, notes, and snippets.

Updated to work with Rails 3 and latest version of Paperclip (2.3.8)
module Paperclip
class Rotator < Thumbnail
def transformation_command
if rotate_command
super + rotate_command
else
super
end
end
def rotate_command
target = @attachment.instance
if target.rotating?
[convert_options = "-rotate #{target.rotation}"]
end
end
end
end
@premjg
premjg commented May 2, 2011

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

@tekn0t
Owner
tekn0t commented May 2, 2011

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

@crazyscorpio12

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
premjg commented Jun 4, 2011

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

@crazyscorpio12

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

@crazyscorpio12

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...

@tekn0t
Owner
tekn0t commented Jun 5, 2011

Have you both added a rotation field to your model?

@crazyscorpio12

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

@tekn0t
Owner
tekn0t commented Jun 5, 2011

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

@crazyscorpio12

@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.

@tekn0t
Owner
tekn0t commented Jun 5, 2011

Great!

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

@tekn0t
Owner
tekn0t 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...?

@mhuggins

I hope you don't mind, but I built upon your example here and added what I consider to be several improvements: http://www.matthuggins.com/articles/rotating-paperclip-image-attachments-in-rails

@tekn0t
Owner
tekn0t commented Oct 25, 2011

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

@dheza
dheza commented Dec 11, 2012

Hello, Guys, I

@dheza
dheza commented Dec 11, 2012

Hello, Guys, I

@qmclaugh

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

[" -rotate #{target.rotation}"]
@jwako
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
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