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.



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

def initialize(data)
  @data = data
end

Class Method Details

.magic_bytesObject



6
7
8
9
10
11
12
13
14
15
16
# 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
  ].map { |s| s.dup.force_encoding("binary") }
end

Instance Method Details

#readObject



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

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

  MiniMagick::Image.read(@data)
end

#valid_header?Boolean

Returns:

  • (Boolean)


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

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