【问题标题】:Rails 5 Carrierwave and MiniMagick breaks gif at resizing for thumbnailRails 5 Carrierwave 和 MiniMagick 在调整缩略图大小时打破了 gif
【发布时间】:2018-07-21 20:02:36
【问题描述】:

我正在使用carrierwave 和mini_magick 来调整我的图像大小,生成缩略图。它适用于 jpg 和 png 等静止图像,但当我尝试使用 gif 时,它会扭曲它。您可以在下面看到原始与调整大小的示例。知道如何让它生成 gif thumb 而不会破坏它吗?

原始 GIF :) https://imgur.com/oi1f8XT

生成的缩略图 GIF :( https://imgur.com/a/PwAXv


ps 为什么缩略图的尺寸比原来的大?原图为 800*600px,缩略图为 400*300px。无论如何,生成缩略图的重点是文件大小更小。

谢谢!


image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url(*args)
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end
  def default_url(*args)
    ActionController::Base.helpers.asset_path("fallback/" + [version_name, "post.jpg"].compact.join('_'))
  end

  # Process files as they are uploaded:
  # process scale: [800, 600]
  # process :resize_to_fit => [800, 600]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process resize_to_fit: [50, 50]
  # end
  version :thumb do
    process resize_to_fit: [400, 300]
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_whitelist
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 carrierwave gif minimagick


    【解决方案1】:

    您尝试调整大小的 gif 已优化,因此在第一帧之后,每个后续帧仅包含需要更改的像素。当 gif 动画时,帧被堆叠在一起创建完整的动画。

    此外,每个帧仅大到足以包含需要更改的像素。所以,每一帧的大小是不同的。当您调整 gif 大小时,Imagemagick 会将每帧的大小调整为 400 x 300 像素,而不管其原始大小如何,这会导致您看到的失真。

    您可以应用名为 coalesce 的 Imagemagick 命令(在 Ruby 中使用 Minimagick 绑定)来解决此问题。它对原始图像进行去优化,使每一帧都是画布的完整大小。

    调用coalesce 会使文件变大,因此在完成调整大小后有必要重新优化 gif。

    这是一个例子:

    version :thumb do
      process my_resize: [400, 300]
    end
    
    def my_resize(width, height)
      if @file.content_type == "image/gif"
        gif_safe_transform! do |image|
          image.resize "#{width}x#{height}" # Perform any transformations here.
        end
      else
        # Process other filetypes if necessary.
      end
    end
    
    def gif_safe_transform!
      MiniMagick::Tool::Convert.new do |image|
        image << @file.path
        image.coalesce # Remove optimizations so each layer shows the full image.
    
        yield image
    
        image.layers "Optimize" # Re-optimize the image.
        image << @file.path
      end
    end
    

    我用一些例子写了更深入的解释here

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 2014-09-04
      • 2012-09-10
      • 2014-04-03
      • 1970-01-01
      • 2011-11-19
      • 2011-07-04
      • 1970-01-01
      相关资源
      最近更新 更多