【问题标题】:Paperclip not replacing the cropped image. at s3回形针不替换裁剪的图像。在 s3
【发布时间】:2014-08-22 02:28:36
【问题描述】:

我正在使用回形针上传图片。我的存储空间在 S3。我正在使用带有回形针的 Jcrop 来裁剪图像,并遵循 #RailsCast182。

一切都很顺利,我已经解决了 RailsCast 中的一些问题。

我想处理裁剪文件并将其上传到 s3。

这是我的日志,显示图像被裁剪


Command :: file -b --mime-type '/tmp/249ac5b855195d2e761e123315dd229b20140701-3395-1mz9lji'

Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]' 2>/dev/null

Command :: identify -format %m '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]'

Command :: convert '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]' -crop 2586x2586+2534+0 -auto-orient -resize "100x100>" '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl20140701-3395-stdr75'

convert.im6: geometry does not contain image `/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl' @ warning/transform.c/CropImage/574.

Command :: file -b --mime '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl20140701-3395-stdr75'

Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]' 2>/dev/null

Command :: identify -format %m '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]'

Command :: convert '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]' -crop 2586x2586+2534+0 -auto-orient -resize "50x50>" '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl20140701-3395-bhth1i'

convert.im6: geometry does not contain image `/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl' @ warning/transform.c/CropImage/574.

Command :: file -b --mime '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl20140701-3395-bhth1i'

After that it shows that image is uploaded at s3 but it was the same org image that uploaded.

我的依恋模型:

class Attachment < ActiveRecord::Base 
    has_attached_file :file, styles: { medium: "100x100>", thumb: "50x50#" }, :processors => [:cropper]

    attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

  def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
  end
end

#paperclip custom cropper

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')
      else
        super
      end
    end

    def crop_command
      target = @attachment.instance
      if target.cropping?
        ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]
      end
    end
  end
end

但回形针永远不会在 S3 更改图像。

我走遍了很多环节,但没有碰过天空。

期待来自伟大社区的帮助。

问候 亚当

【问题讨论】:

    标签: ruby-on-rails-3 ruby-on-rails-4 amazon-s3 paperclip jcrop


    【解决方案1】:

    您需要更改 lib 中的cropper.rb。裁剪器工作正常,但只取图像中心。如果你改变你的cropper.rb,你的图像保存得很好

    module Paperclip  
      class Cropper < Thumbnail  
        def transformation_command  
          if crop_command
            original_command = super
            if original_command.include?('-crop')
              original_command.delete_at(super.index('-crop') + 1)
              original_command.delete_at(super.index('-crop'))
            end
            crop_command + original_command
          else  
            super  
          end  
        end  
    
        def crop_command  
          target = @attachment.instance  
          if target.cropping?
            ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]
          end  
        end  
      end  
    end
    

    然后您需要检查模型中的代码

    def avatar_geometry(style = :original)
        @geometry ||= {}
        avatar_path = (avatar.options[:storage] == :s3) ? avatar.url(style) : avatar.path(style)
        @geometry[style] ||= Paperclip::Geometry.from_file(avatar_path)
      end
    

    如果 s3 以一种大小保存所有文件,则需要将此代码更改为

    has_attached_file :avatar, :styles => {
          :thumb => {:geometry => "90x50#", :processors => [:cropper, :thumbnail]},
    

    注意!更改此文件后,您需要重新启动服务器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多