Sha256: ef9e2b0d577f54da652007d93db1db0b7bdc058a7162edf4a93b8b2d0b828439

Contents?: true

Size: 1.62 KB

Versions: 8

Compression:

Stored size: 1.62 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
      if valid?
        "image/#{format.downcase}"
      end
    end

    # Returns the dimensions of the image as a vector.
    def dimensions
      if valid?
        Vector2d.new(*metadata[:dimensions])
      end
    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
      if valid?
        metadata[:format]
      end
    end

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

    private

    def metadata
      @metadata ||= read_metadata
    end

    def read_metadata
      image = MiniMagick::Image.read(@data)
      image.auto_orient
      metadata = {
        colorspace: image[:colorspace],
        dimensions: image[:dimensions],
        format:     image[:format]
      }
      image.destroy!
      metadata
    rescue MiniMagick::Invalid
      :invalid
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
dynamic_image-2.0.0 lib/dynamic_image/metadata.rb
dynamic_image-2.0.0.beta7 lib/dynamic_image/metadata.rb
dynamic_image-2.0.0.beta6 lib/dynamic_image/metadata.rb
dynamic_image-2.0.0.beta5 lib/dynamic_image/metadata.rb
dynamic_image-2.0.0.beta4 lib/dynamic_image/metadata.rb
dynamic_image-2.0.0.beta3 lib/dynamic_image/metadata.rb
dynamic_image-2.0.0.beta2 lib/dynamic_image/metadata.rb
dynamic_image-2.0.0.beta1 lib/dynamic_image/metadata.rb