22

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)

sjsc
  • 4,552
  • 10
  • 39
  • 55

7 Answers7

17

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"
EugZol
  • 6,476
  • 22
  • 41
Rafeeq
  • 231
  • 2
  • 3
  • I don't know why you got downvoted but this works absolutely fine in Rails 5.0 ! Thank you. – Vlad Jun 18 '16 at 09:07
16

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

Neil Slater
  • 26,512
  • 6
  • 76
  • 94
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
14

One approach is to use the "magic number" convention to read the first bits of a file.
http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html

Examples:

  "BM" is a Bitmap image
  "GIF8" is a GIF image
  "\xff\xd8\xff\xe0" is a JPEG image

Example in Ruby:

  def bitmap?(data)
    return data[0,2]=="MB"
  end

  def gif?(data)
    return data[0,4]=="GIF8"
  end

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

  def file_is_image?(filename)
    f = File.open(filename,'rb')  # rb means to read using binary
    data = f.read(9)              # magic numbers are up to 9 bytes
    f.close
    return bitmap?(data) or gif?(data) or jpeg?(data)
  end

Why use this instead of the file name extension or the filemagic module?

To detect the data type before writing any data to disk. For example, we can read upload data stream before we write any data to disk. If the magic number doesn't match the web form content type, then we can immediately report an error.

We implement our real-world code slightly differently. We create a hash: each key is a magic number string, each value is a symbol like :bitmap, :gif, :jpeg, etc. If anyone would like to see our real-world code, feel free to contact me here.

n1313
  • 20,555
  • 7
  • 31
  • 46
joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119
  • 4
    Another approach at checking `jpeg?` would be `return data[0,3].bytes == [255, 216, 255]` – n1313 Jul 15 '13 at 04:12
7

Since you're using Paperclip, you can use the built in "validates_attachment_content_type" method in the model where "has_attached_file" is used, and specify which file types you want to allow.

Here's an example from an application where users upload an avatar for their profile:

has_attached_file :avatar, 
                  :styles => { :thumb => "48x48#" },
                  :default_url => "/images/avatars/missing_avatar.png",
                  :default_style => :thumb

validates_attachment_content_type :avatar, :content_type => ["image/jpeg", "image/pjpeg", "image/png", "image/x-png", "image/gif"]

The documentation is here http://dev.thoughtbot.com/paperclip/classes/Paperclip/ClassMethods.html

Peter Brown
  • 50,956
  • 18
  • 113
  • 146
3

i honestly think this is way easier, use mimemagic gem:

first install it

gem 'mimemagic'

open stream(bytes of target image)

url="https://i.ebayimg.com/images/g/rbIAAOSwojpgyQz1/s-l500.jpg"
result = URI.parse(url).open

then check data-stream's file type

for example:

MimeMagic.by_magic(result).type == "image/jpeg"

even though this might be more elegant

%w(JPEG GIF TIFF PNG).include?(MimeMagic.by_magic(result).type)

d1jhoni1b
  • 7,497
  • 1
  • 51
  • 37
1

imagemagick has a command called identity that handles this - check w/ the paperclip documentation - there's probably a way to handle this from within your RoR app.

John Topley
  • 113,588
  • 46
  • 195
  • 237
BandsOnABudget
  • 3,966
  • 2
  • 17
  • 19
  • Thanks for the tip Claude and John on the identity. I'll have a check. Thank you so much for the referral. – sjsc Feb 08 '10 at 21:45
1

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
Tony Dong
  • 11
  • 2