Sha256: 557276ca3ef2ed758d2fe9118191a5f63dbc9539f0d2a1b6ba32b60bbbc6d0d9
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
# encoding: utf-8 module DynamicImage class ImageReader def initialize(data) @data = data end def read raise DynamicImage::Errors::InvalidHeader unless valid_header? MiniMagick::Image.read(@data) end def valid_header? return false if file_header.blank? magic_bytes.each do |expr| if (expr.is_a?(Regexp) && file_header =~ /^#{expr}/) || (expr.is_a?(String) && file_header.start_with?(expr)) return true end end false end private def file_header @file_header ||= StringIO.new(@data, "rb").read(10) end def gif_magic_bytes [ "\x47\x49\x46\x38\x37\x61".force_encoding("binary"), "\x47\x49\x46\x38\x39\x61".force_encoding("binary") ] end def jpeg_magic_bytes [ "\xff\xd8\xff\xdb".force_encoding("binary"), Regexp.new("\xff\xd8\xff\xe0(.*){2}JFIF".force_encoding("binary"), Regexp::MULTILINE), Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"), Regexp::MULTILINE), "\xff\xd8\xff\xee\x00\x0e".force_encoding("binary") # Adobe JPEG ] end def magic_bytes gif_magic_bytes + png_magic_bytes + jpeg_magic_bytes + tiff_magic_bytes end def png_magic_bytes [ "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a".force_encoding("binary") ] end def tiff_magic_bytes [ "\x49\x49\x2a\x00".force_encoding("binary"), "\x4d\x4d\x00\x2a".force_encoding("binary") ] end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dynamic_image-2.0.6 | lib/dynamic_image/image_reader.rb |