我正在使用 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 发挥作用?
最佳答案
从 gemfile.add 中删除 Rmagick minimagick到你的 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..
关于ruby-on-rails - 用带有 Paperclip 的 mini_magick 替换 RMagick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26013221/