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

后端 未结 6 1845
醉酒成梦
醉酒成梦 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条回答
  •  醉话见心
    2020-12-28 17:57

    As an addition to Joel's answer, in Rails 5 I had to transform the comparison string to a bytecode. Eg:

    def jpeg?(data)
      return data[0,4]=="\xff\xd8\xff\xe0".b
    end
    

提交回复
热议问题