2016-06-20 14 views
0

私はRuby on Railsとpaper-clip Gemを使用しています。.gif画像がアニメーション化されていない場合gifの場合、.gif画像を.jpegに変換したいと思います。与えられた.gifイメージがアニメーションgifでない場合、.gifイメージを.jpegに変換するにはどうすればよいですか?

これは私のコードです:私は自分で

has_attached_file :image, styles: Proc.new { |file| file.instance.check_image_gif? ? { 
     :'960' => ["960>x960", :gif], 
     :'640' => ["640>x640", :gif], 
     :'320' => ["320>x320", :gif] 
     }:{ 
     :'960' => ["960>x960", :jpg], 
     :'640' => ["640>x640", :jpg], 
     :'320' => ["320>x320", :jpg] 
     } 
    } 
    def check_image_gif? 
     # I want to check animation gif here. 
     image.instance.image_content_type =~ %r(gif) ? true : false 
    end 
+0

これはおそらく助けることができます:http://stackoverflow.com/questions/27238816/how-to-tell-if-gif-is-animated – IngoAlbers

+0

ありがとうございますが、私はpaperclipからrmagickにデータを送信することを理解できません。 .. –

+0

Paperclipは既にImageMagickを使用していますので、 'check_image_gif?'に 'Magick :: ImageList.new(image.url).scenes> 0'を試してみてください。 – IngoAlbers

答えて

0

解決。

def check_image_gif? 
    begin 
    file = image.instance.image.queued_for_write[:original] 
    img = Magick::Image.read(file.path) 
    animation = (img.size>1) 
    unless animation 
     image.instance.image_content_type = "image/jpeg" 
    end 
    rescue => e 
    end 
    image.instance.image_content_type =~ %r(gif) ? true : false 
end 

rmagickを付け加えました。

関連する問題