Sha256: a871be1c1f1b1acb9990fabdeac2c89dc6d90faae62ea2149683056335ac3286

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

# encoding: utf-8

module DynamicImage
  # = DynamicImage Metadata
  #
  # Parses metadata from an image. Expects to receive image data as a
  # binary string.
  class Metadata
    def initialize(data)
      @data = data
    end

    # Returns the color space of the image as a string. The result will be one
    # of the following: "rgb", "cmyk", "gray".
    def colorspace
      if valid?
        case metadata[:colorspace]
        when /rgb/i
          'rgb'
        when /cmyk/i
          'cmyk'
        when /gray/i
          'gray'
        end
      end
    end

    # Returns the content type of the image.
    def content_type
      "image/#{format.downcase}" if valid?
    end

    # Returns the dimensions of the image as a vector.
    def dimensions
      Vector2d.new(*metadata[:dimensions]) if valid?
    end

    # Returns the width of the image.
    def width
      dimensions.try(:x)
    end

    # Returns the height of the image.
    def height
      dimensions.try(:y)
    end

    # Returns the format of the image.
    def format
      metadata[:format] if valid?
    end

    # Returns true if the image is valid.
    def valid?
      @data && reader.valid_header? && metadata != :invalid
    end

    private

    def metadata
      @metadata ||= read_metadata
    end

    def read_image
      image = reader.read
      image.auto_orient
      result = yield image
      image.destroy!
      result
    rescue MiniMagick::Invalid
      :invalid
    end

    def reader
      @reader ||= DynamicImage::ImageReader.new(@data)
    end

    def read_metadata
      read_image do |image|
        {
          colorspace: image[:colorspace],
          dimensions: image[:dimensions],
          format:     image[:format]
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynamic_image-2.0.2 lib/dynamic_image/metadata.rb
dynamic_image-2.0.1 lib/dynamic_image/metadata.rb