用Paperclip替换带有mini_magick的RMagick

时间:2014-09-24 09:24:13

标签: ruby-on-rails ruby paperclip rmagick minimagick

我正在使用RMagick并喜欢它,但它正在吃掉大量内存。即使是一个简单的脚本也会使用超过100MB的Ram。在我的本地计算机上这不是问题,但在heroku上,当少数用户同时上传图片时,我的应用程序崩溃。

我找到mini_magick并尝试在我的模型中替换它,但无法找到解决方案。这是我目前的流程:

  # Validations
  validates :scr, presence: true

  # Paperclip
  has_attached_file :scr,
    styles: {
      index:        ['220x170#', :jpg, quality: :better],
      show:         ['1000', :jpg, quality: :better],
      original:     ['100%', :jpg, quality: :better],
      directionals: ['115x70#', :jpg]
    },
    convert_options: {
      show:         '-quality 90 -unsharp 3x0.4+0.4+0 -interlace Plane',
      index:        '-quality 90 -unsharp 3x0.4+0.4+0 -interlace Plane',
      original:     '-quality 90 -interlace Plane',
      directionals: '-quality 90 -interlace Plane'
    },
    processors: [:thumbnail, :compression]

  # Paperclip Validation
  validates_attachment_content_type :scr, content_type: ['image/jpg', 'image/jpeg', 'image/png']

如何更换RMagick并让Mini_magick做魔术?

1 个答案:

答案 0 :(得分:2)

将gemfile.add minimagick中的Rmagick删除到你的gemfile并开始使用它

Add the gem to your Gemfile:

gem "mini_magick"

bundle install 

####now start using it with callback/observer 
image = MiniMagick::Image.open("input.jpg")
image.resize "100x100"
image.write  "output.jpg"

或者

您可以使用delayed_paperclip进行相同操作,以便在运行时仅转换下一页所需的样式....将其放在后台进行转换,使用它而不更改代码。

 process_in_background :avatar, :only_process => [:show,:original,:directionals]
  ##assuming you next page only need style :index..