Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 576
Alistair Holt Avatar asked Nov 20 '12 19:11

Alistair Holt


2 Answers

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
like image 185
rorlork Avatar answered Oct 23 '22 04:10

rorlork


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
like image 3
Andrey Konoplenko Avatar answered Oct 23 '22 04:10

Andrey Konoplenko