class DynamicImage::Metadata

DynamicImage Metadata

Parses metadata from an image. Expects to receive image data as a binary string.

Public Class Methods

new(data) click to toggle source
# File lib/dynamic_image/metadata.rb, line 9
def initialize(data)
  @data = data
end

Public Instance Methods

colorspace() click to toggle source

Returns the color space of the image as a string. The result will be one of the following: “rgb”, “cmyk”, “gray”.

# File lib/dynamic_image/metadata.rb, line 15
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
content_type() click to toggle source

Returns the content type of the image.

# File lib/dynamic_image/metadata.rb, line 29
def content_type
  reader.format.content_type if valid?
end
dimensions() click to toggle source

Returns the dimensions of the image as a vector.

# File lib/dynamic_image/metadata.rb, line 38
def dimensions
  Vector2d.new(metadata[:width], metadata[:height]) if valid?
end
format() click to toggle source
# File lib/dynamic_image/metadata.rb, line 33
def format
  reader.format.name if valid?
end
height() click to toggle source

Returns the height of the image.

# File lib/dynamic_image/metadata.rb, line 48
def height
  metadata[:height] if valid?
end
valid?() click to toggle source

Returns true if the image is valid.

# File lib/dynamic_image/metadata.rb, line 53
def valid?
  @data && reader.valid_header? && metadata != :invalid
end
width() click to toggle source

Returns the width of the image.

# File lib/dynamic_image/metadata.rb, line 43
def width
  metadata[:width] if valid?
end

Private Instance Methods

metadata() click to toggle source
# File lib/dynamic_image/metadata.rb, line 59
def metadata
  @metadata ||= read_metadata
end
read_image() { |read.autorot| ... } click to toggle source
# File lib/dynamic_image/metadata.rb, line 63
def read_image
  yield reader.read.autorot
end
read_metadata() click to toggle source
# File lib/dynamic_image/metadata.rb, line 71
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:,
      colorspace: image.get("interpretation") }
  end
end
reader() click to toggle source
# File lib/dynamic_image/metadata.rb, line 67
def reader
  @reader ||= DynamicImage::ImageReader.new(@data)
end