Carrierwave + MiniMagick - 如何将动画 GIF 压缩到第一帧



>有人知道如何使用Carrierwave + MiniMagick将动画GIF压缩到第一帧吗?

我认为MiniMagick有一些变化,因为我只是花了三个小时试图找出为什么Andrey的代码对我不起作用。

我收到以下错误:

ActiveRecord::RecordInvalid (Validation failed: 
Image Failed to manipulate with MiniMagick, maybe it is not an image? 
Original Error: Command 
("mogrify -scene /var/folders/0o/0oqNck+++TI/-Tmp-/mini_magick2022-499-15zc.gif") 
failed: {:status_code=>1, :output=>"mogrify: invalid argument for option 
`/var/folders/0o/0oqNck+++TI/-Tmp-/mini_magick2022-499-15zc.gif': -scene 
@ error/mogrify.c/MogrifyImageCommand/5558.n"})

最后我发现MiniMagick::image有方法collapse!(在这里找到:http://www.ruby-doc.org/gems/docs/j/jf--mini_magick-3.1/MiniMagick/Image.html#method-i-collapse-21)解决了这个问题:

process :remove_animation
def remove_animation
  manipulate! do |img|
    if img.mime_type.match /gif/
      img.collapse!
    end
    img
  end
end

对我有用:

def only_first_frame
    manipulate! do |img|
      if img.mime_type.match /gif/
        if img.scene == 0
          img = img.cur_image #Magick::ImageList.new( img.base_filename )[0]
          else
            img = nil # avoid concat all frames
        end
      end
      img
    end
  end

然后你必须打电话:

process :only_first_frame

最新更新