Class: DynamicImage::ImageReader

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_image/image_reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ImageReader

Returns a new instance of ImageReader.



20
21
22
# File 'lib/dynamic_image/image_reader.rb', line 20

def initialize(data)
  @data = data
end

Class Method Details

.magic_bytesObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dynamic_image/image_reader.rb', line 6

def magic_bytes
  @magic_bytes ||= [
    "\x47\x49\x46\x38\x37\x61",         # GIF
    "\x47\x49\x46\x38\x39\x61",
    "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", # PNG
    "\xff\xd8",                         # JPEG
    "\x49\x49\x2a\x00",                 # TIFF
    "\x4d\x4d\x00\x2a",
    "\x42\x4d",                         # BMP
    "\x52\x49\x46\x46"                  # WEBP
  ].map { |s| s.dup.force_encoding("binary") }
end

Instance Method Details

#exifObject



24
25
26
27
28
# File 'lib/dynamic_image/image_reader.rb', line 24

def exif
  raise DynamicImage::Errors::InvalidHeader unless valid_header?

  MiniExiftool.new(string_io)
end

#readObject



30
31
32
33
34
# File 'lib/dynamic_image/image_reader.rb', line 30

def read
  raise DynamicImage::Errors::InvalidHeader unless valid_header?

  MiniMagick::Image.read(@data)
end

#valid_header?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# File 'lib/dynamic_image/image_reader.rb', line 36

def valid_header?
  return false if file_header.blank?

  self.class.magic_bytes.each do |str|
    return true if file_header.start_with?(str)
  end
  false
end