Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.include?(".png") or file.to_s.include?(".jpg")
end

But that might be a little inefficient and not correct. Any ideas?

(I'm using the paperclip plugin, btw, but I don't see any methods to determine whether a file is an image in paperclip)

like image 330
sjsc Avatar asked Feb 08 '10 19:02

sjsc


1 Answers

I would use the ruby-filemagic gem which is a Ruby binding for libmagic.

like image 183
Sinan Ünür Avatar answered Oct 18 '22 13:10

Sinan Ünür