문제

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

도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top