【问题标题】:Resize gif file without distortion with Rmagick and Carrierwave使用 Rmagick 和 Carrierwave 调整 gif 文件大小而不失真
【发布时间】:2016-04-23 11:42:42
【问题描述】:

我正在尝试调整 gif 文件的大小,但它会变形。我知道我必须实现类似于底部链接中提到的内容,但我不清楚如何使用 Carrierwave 来实现:

http://www.imagemagick.org/Usage/anim_basics/#coalesced

Resize animated GIF file without destroying animation

这是一个重现错误的脚本:

require 'rubygems'
require 'carrierwave'

class AvatarUploader < CarrierWave::Uploader::Base
  storage :file

  include CarrierWave::RMagick

  version :thumb do
    process resize_to_fit: [200, 200]
  end

  def store_dir
    'images'
  end
end

uploader = AvatarUploader.new

uploader.download! 'https://dl.dropboxusercontent.com/u/3217866/9706f7e6-4d56-11e3-9551-9da854d79892.gif'
uploader.store!

【问题讨论】:

    标签: ruby resize carrierwave gif


    【解决方案1】:

    最后我解决了这个问题,但生成的 GIF 文件非常大,大约从 500 kb 到 4 MB。

    process :fix_resize_issue_with_gif
    
    def fix_resize_issue_with_gif
      if file.extension.downcase == 'gif' && version_name.blank?
        list = ::Magick::ImageList.new.from_blob file.read
    
        if list.size > 1
          list = list.coalesce
          File.open(current_path, 'wb') { |f| f.write list.to_blob }
        end
      end
    end
    

    【讨论】:

      【解决方案2】:

      我发布这个是希望它对某人有用。我无需读/写文件就能解决问题:

      def resize_with_animate(width,height)
        manipulate! do |img|
        if img[:format].downcase == 'gif'
          #coalesce animated gifs before resize.
          img.coalesce
        end
        img.resize "#{width}x#{height}>"
        img = yield(img) if block_given?
        img
      end
      process :resize_with_animate(300,200)
      

      我发现Efficiently converting image formats 上的 wiki 页面很有帮助。

      【讨论】:

      • 这对我不起作用:我收到了ProcessError:无法使用 MiniMagick 进行操作,可能不是图像?原始错误:`mogrify -coalesce
      • 也许你的 imagemagick 或者它所依赖的库之一已经过时了。这似乎发生在其他有类似错误的人身上,例如:stackoverflow.com/questions/9905499/… 或者,您应该尝试使用读/写接受的版本。避免读/​​写的版本仍然适用于我们。
      猜你喜欢
      • 2014-08-16
      • 1970-01-01
      • 2013-08-26
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多