[英]Rotate image attached with paperclip
我们有一个使用Paperclip(4.2.0)连接头像的AR模型。 我们希望允许用户旋转此图像,但我在使其工作时遇到了很多困难。 我的代码看起来像:
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
而我正在尝试通过执行以下操作来旋转图像:
user.rotate = 90
user.avatar.reprocess!
我可以看到-rotate 90选项被传递给转换,但它没有做任何事情。 有没有人设法使用回形针使这个工作?
更换target.rotation
与target.rotate
并添加尾随空白的rotate_method
奏效了我。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.