简体   繁体   English

使用Paperclip 5旋转图像

[英]Rotate image with Paperclip 5

I've got photo on my model 我的模特上有照片

has_attached_file :photo, BucketConfig.default.merge(
      processors: [:rotator],
      styles: {
          original: Proc.new { |a| { rotation: a.rotation } }
      },
      default_url:  ActionController::Base.helpers.asset_path('assets/request_default.png')
  )

To catch a rotation param I do something like this 要抓住一个旋转参数,我会做这样的事情

attr_accessor :rotation

    before_save :adjust_rotation
    before_update :reprocess_image

    protected

    def adjust_rotation
      self.rotation = self.rotation.to_i
      self.rotation = self.rotation % 360 if self.rotation >= 360 || self.rotation <= -360
    end

    def reprocess_image
      self.photo.reprocess! unless self.rotation.zero?
    end

my Rotator process in lib/paperclip 我在lib / paperclip中的Rotator进程

module Paperclip
  class Rotator < Processor
    def transformation_command
      if rotator_command
        rotator_command + super.join(' ')
      else
        super
      end
    end

    def rotator_command
      target = @attachment
      if target.rotation.present?
        " -rotate #{target.rotation} "
      end
    end
  end
end

When I call 我打电话的时候

m = Model.first
m.rotation=90
m.save

I've got error: 我有错误:

NoMethodError - undefined method `closed?' for nil:NilClass
Did you mean?  clone:
  paperclip (4.3.7) lib/paperclip/attachment.rb:582:in `block in unlink_files'
  paperclip (4.3.7) lib/paperclip/attachment.rb:581:in `unlink_files'
  paperclip (4.3.7) lib/paperclip/attachment.rb:536:in `post_process_style'
  paperclip (4.3.7) lib/paperclip/attachment.rb:511:in `post_process_styles'
  paperclip (4.3.7) lib/paperclip/attachment.rb:504:in `block (2 levels) in post_process'

Why my reprocess! 为什么我重新加工! method doesn't work ? 方法不起作用? Do I something wrong with my rotator process? 我的旋转器过程有问题吗? Where's the problem? 哪里有问题?

You have no make method: https://github.com/rezeko/paperclip/blob/master/lib/paperclip/processor.rb#L9 你没有make方法: https//github.com/rezeko/paperclip/blob/master/lib/paperclip/processor.rb#L9

A fix is to use class Rotator < Thumbnail , and just inherit the processing setup by the default processor. 修复方法是使用class Rotator < Thumbnail ,并继承默认处理器的处理设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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