Sha256: 83d0bbb6d788bf268880549b42c16c63a14c3e3da32be79f06ca4a22044efd35

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

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
      return unless valid?

      case metadata[:colorspace].to_s
      when /rgb/i
        "rgb"
      when /cmyk/i
        "cmyk"
      when /gray/i, /b-w/i
        "gray"
      end
    end

    # Returns the content type of the image.
    def content_type
      reader.format.content_type if valid?
    end

    def format
      reader.format.name if valid?
    end

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

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

    # Returns the height of the image.
    def height
      metadata[:height] 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.autorot if image.respond_to?(:autorot)
      yield image
    end

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

    def read_metadata
      read_image do |image|
        height = if image.get_fields.include?("page-height")
                   image.get("page-height")
                 else
                   image.get("height")
                 end

        { width: image.get("width"),
          height: height,
          colorspace: image.get("interpretation") }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dynamic_image-3.0.0 lib/dynamic_image/metadata.rb