MiniMagick for FileColumn
Posted by devon on March 7th, 2008 filed in Rails应用file_column用于图片上传、缩放非常方便,比如在系统中做用户头像之类。
但file_column默认使用rmagick来做图象处理。在实际使用过程中,发现rmagick有内存泄露问题,所以,将file_column中的图象缩放改为 mini_magick。
在安装 file_column 后,只需要修改 vendor/plugins/file_column/lib/magick_file_column.rb。改动如下(完整代码请参考 http://cookbook.enjoyrails.com/cookbooks/9):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 7,8c7,8 < img = ::Magick::Image::read(absolute_path).first < rescue ::Magick::ImageMagickError --- > img = ::MiniMagick::Image::from_file(absolute_path) > rescue ::MiniMagick::MiniMagickError 38c38 < ::Magick --- > ::MiniMagick 40c40 < require 'RMagick' --- > require 'mini_magick' 51,52c51,52 < img = ::Magick::Image::read(absolute_path).first < rescue ::Magick::ImageMagickError --- > img = ::MiniMagick::Image::from_file(absolute_path) > rescue ::MiniMagick::MiniMagickError 86a87,89 > > img.combine_options do | i | > 90,91c93,94 < img = img.crop(::Magick::CenterGravity, [img.columns, w].min, < [img.rows, h].min, true) --- > i.gravity "Center" > i.crop "#{[img.columns, w].min}x#{[img.rows, h].min}" 95,97c98 < img = img.change_geometry(img_options[:size]) do |c, r, i| < i.resize(c, r) < end --- > i.resize img_options[:size] 98a100,104 > > i.strip # strip unnecessary profiles > i.depth 8 # otherwise PNG bloats, big time > i.quality 80 # save with reasonable quality for a thumbnail > end 100,106c106 < img.write(dest_path) do < if img_options[:attributes] < img_options[:attributes].each_pair do |property, value| < self.send "#{property}=", value < end < end < end --- > img.write(dest_path) #do 208c208 < require 'RMagick' --- > require 'mini_magick' </code> |
Related posts:
- Rails中检测SWF尺寸 安装: cd vendor/plugins svn export http://ruby-imagespec.googlecode.com/svn/trunk/ image_spec 使用: image =...
Related posts brought to you by Yet Another Related Posts Plugin.
Leave a Comment