Class: DynamicImage::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_image/metadata.rb

Overview

DynamicImage Metadata

Parses metadata from an image. Accepts a Pathname, IO, or binary string.

Every reader returns nil for data that isn't a readable image.

Examples:

 = DynamicImage::Metadata.new(Pathname.new("image.jpg"))
.valid?       # => true
.content_type # => "image/jpeg"
.dimensions   # => Vector2d(320, 200)

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Metadata

Returns a new instance of Metadata.

Parameters:

  • data (Pathname, IO, String)

    the image, as a path, an open file or a binary string



17
18
19
# File 'lib/dynamic_image/metadata.rb', line 17

def initialize(data)
  @data = data
end

Instance Method Details

#alpha?Boolean?

Returns true if the image has an alpha channel.

This is the presence of the channel, not of actual transparency: an image can carry a fully opaque alpha channel. Reading it costs nothing, where scanning the channel for transparency would mean decoding every pixel.

Returns:

  • (Boolean, nil)

    true if the image has an alpha channel



87
88
89
# File 'lib/dynamic_image/metadata.rb', line 87

def alpha?
  [:alpha] if valid?
end

#colorspaceString?

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

Returns:

  • (String, nil)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dynamic_image/metadata.rb', line 25

def colorspace
  return unless valid?

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

#content_typeString?

Returns the content type of the image.

Returns:

  • (String, nil)


41
42
43
# File 'lib/dynamic_image/metadata.rb', line 41

def content_type
  reader.format.content_type if valid?
end

#dimensionsVector2d?

Returns the dimensions of the image as a vector. EXIF rotation is applied, so these are the dimensions the image has once normalized.

Returns:

  • (Vector2d, nil)


56
57
58
# File 'lib/dynamic_image/metadata.rb', line 56

def dimensions
  Vector2d.new([:width], [:height]) if valid?
end

#formatString?

Returns the name of the detected format.

Returns:

  • (String, nil)

    the format name, such as "JPEG"



48
49
50
# File 'lib/dynamic_image/metadata.rb', line 48

def format
  reader.format.name if valid?
end

#frame_countInteger?

Returns the number of frames. Animated formats can have more than one; everything else has a single frame.

Returns:

  • (Integer, nil)

    the number of frames



77
78
79
# File 'lib/dynamic_image/metadata.rb', line 77

def frame_count
  [:frame_count] if valid?
end

#heightInteger?

Returns the height of the image.

Returns:

  • (Integer, nil)

    the height in pixels



70
71
72
# File 'lib/dynamic_image/metadata.rb', line 70

def height
  [:height] if valid?
end

#valid?Boolean

Returns true if the data is a readable image in a supported format.

Returns:

  • (Boolean)


94
95
96
# File 'lib/dynamic_image/metadata.rb', line 94

def valid?
  @data && reader.valid_header? &&  != :invalid
end

#widthInteger?

Returns the width of the image.

Returns:

  • (Integer, nil)

    the width in pixels



63
64
65
# File 'lib/dynamic_image/metadata.rb', line 63

def width
  [:width] if valid?
end