Search code examples
rubyimagemagickcarrierwaveminimagick

Carrierwave + MiniMagick - How to squash animated GIFs to their first frame?


Anyone know how to squash animated GIFs down to their first frame using Carrierwave + MiniMagick?


Solution

  • I think MiniMagick has had some changes, because I just spent three hours trying to find out why Andrey's code didn't work for me.

    I got the following error:

    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"})
    

    Finally I found that MiniMagick::Image has the method collapse! (found here: http://www.ruby-doc.org/gems/docs/j/jf--mini_magick-3.1/MiniMagick/Image.html#method-i-collapse-21 ) which solves the problem:

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