简体   繁体   English

旋转附有回形针的图像

[英]Rotate image attached with paperclip

We have an AR model with an avatar attached using Paperclip (4.2.0). 我们有一个使用Paperclip(4.2.0)连接头像的AR模型。 We like to allow users to rotate this image, but I'm having a lot of difficulty making it work. 我们希望允许用户旋转此图像,但我在使其工作时遇到了很多困难。 My code looks like: 我的代码看起来像:

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'm trying to rotate the image by doing: 而我正在尝试通过执行以下操作来旋转图像:

user.rotate = 90
user.avatar.reprocess!

I can see the -rotate 90 option being passed to convert, but it doesn't do anything. 我可以看到-rotate 90选项被传递给转换,但它没有做任何事情。 Has anyone managed to get this working using paperclip? 有没有人设法使用回形针使这个工作?

Replace target.rotation with target.rotate and add a trailing whitespace to the rotate_method did the trick for me. 更换target.rotationtarget.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM