簡體   English   中英

旋轉附有回形針的圖像

[英]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.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