Question

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

Was it helpful?

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

OTHER TIPS

It's works for me:

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

Then you must call:

process :only_first_frame
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top