Sha256: 7379a496f59cf64e32d2bd0feea9897b39f5d13703f7c90e288d3d92a60e1580

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module DynamicImage
  class ImageReader
    class << self
      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
    end

    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?

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

    private

    def file_header
      @file_header ||= StringIO.new(@data, "rb").read(8)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dynamic_image-2.0.25 lib/dynamic_image/image_reader.rb
dynamic_image-2.0.24 lib/dynamic_image/image_reader.rb
dynamic_image-2.0.23 lib/dynamic_image/image_reader.rb
dynamic_image-2.0.22 lib/dynamic_image/image_reader.rb
dynamic_image-2.0.21 lib/dynamic_image/image_reader.rb
dynamic_image-2.0.20 lib/dynamic_image/image_reader.rb