public
Last active — forked from amasses/gallery_images_controller.rb

Updated to work with Rails 3 and latest version of Paperclip (2.3.8)

  • Download Gist
app/models/GalleryImage.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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
self.image.reprocess!
self.save
end
def rotating?
!self.rotation.nil? and self.rotate
end
 
private
def set_defaults
self.rotation ||= 0
end
end
lib/paperclip_processors/rotator.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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?
" -rotate #{target.rotation}"
end
end
end
end

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

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

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

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!

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.

Great!

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

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: http://www.matthuggins.com/articles/rotating-paperclip-image-attachments-in-rails

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

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

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

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.

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.