Ruby on Rails: How do you check if a file is an image?

后端 未结 6 1834
醉酒成梦
醉酒成梦 2020-12-28 17:02

How would you check if a file is an image? I\'m thinking you could use an method like so:

def image?(file)
  file.to_s.include?(\".gif\") or file.to_s.includ         


        
6条回答
  •  -上瘾入骨i
    2020-12-28 17:37

    Please check it once

    MIME::Types.type_for('tmp/img1.jpg').first.try(:media_type)
    => "image"
    
    MIME::Types.type_for('tmp/img1.jpeg').first.try(:media_type)
    => "image"
    
    MIME::Types.type_for('tmp/img1.gif').first.try(:media_type)
    => "image"
    
    MIME::Types.type_for('tmp/ima1.png').first.try(:media_type)
    => "image"
    

提交回复
热议问题