Sha256: c179ead335f91cea3b1a70c944d32701a0f87fdc1f9bdc4513a63ec3519fec58

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 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 exif
      raise DynamicImage::Errors::InvalidHeader unless valid_header?

      MiniExiftool.new(string_io)
    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 ||= string_io.read(8)
    end

    def string_io
      StringIO.new(@data, "rb")
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dynamic_image-2.1.5 lib/dynamic_image/image_reader.rb
dynamic_image-2.1.4 lib/dynamic_image/image_reader.rb
dynamic_image-2.1.3 lib/dynamic_image/image_reader.rb
dynamic_image-2.1.2 lib/dynamic_image/image_reader.rb
dynamic_image-2.1.1 lib/dynamic_image/image_reader.rb
dynamic_image-2.1.0 lib/dynamic_image/image_reader.rb