Rotate an image attached with a paperclip
We have an AR model with an avatar attached with Paperclip (4.2.0). We like to allow users to rotate this image, but I'm having a lot of difficulties so it works. My code looks like this:
module Paperclip
class Rotator < Thumbnail
def initialize(file, options = {}, attachment = nil)
options[:auto_orient] = false
super
end
def transformation_command
if rotate_command
"#{rotate_command} #{super.join(' ')}"
else
super
end
end
def rotate_command
target = @attachment.instance
if target.rotation.present?
" -rotate #{target.rotation}"
end
end
end
end
class User < ActiveRecord::Base
has_attached_file :avatar, {
styles: {
small: ['72x72#', :png]
},
processors: [:rotator]
}
attr_accessor :rotate
end
And I am trying to rotate the image by doing:
user.rotate = 90 user.avatar.reprocess!
I can see the -rotate 90 parameter is passed in for the conversion, but it does nothing. Has anyone been able to get this working with paperclip?
+3
source to share
1 answer
Replace target.rotation
with target.rotate
and add a trailing space in the rotate_method
helped.
module Paperclip
class Rotator < Thumbnail
def transformation_command
if rotate_command
rotate_command + super.join(' ')
else
super
end
end
def rotate_command
target = @attachment.instance
if target.rotate.present?
" -rotate #{target.rotate} "
end
end
end
end
class User < ActiveRecord::Base
has_attached_file :avatar, {
styles: {
small: ['72x72#', :png]
},
processors: [:rotator]
}
attr_accessor :rotate
end
+1
source to share