class MiniMagick::Image::Info

@private

Constants

ASCII_ENCODED_EXIF_KEYS

Public Class Methods

new(path) click to toggle source
# File lib/mini_magick/image/info.rb, line 9
def initialize(path)
  @path = path
  @info = {}
end

Public Instance Methods

[](value, *args) click to toggle source
# File lib/mini_magick/image/info.rb, line 14
def [](value, *args)
  case value
  when "format", "width", "height", "dimensions", "size", "human_size"
    cheap_info(value)
  when "colorspace"
    colorspace
  when "resolution"
    resolution(*args)
  when "signature"
    signature
  when /^EXIF\:/i
    raw_exif(value)
  when "exif"
    exif
  when "data"
    data
  else
    raw(value)
  end
end
cheap_info(value) click to toggle source
# File lib/mini_magick/image/info.rb, line 39
def cheap_info(value)
  @info.fetch(value) do
    format, width, height, size = parse_warnings(self["%m %w %h %b"]).split(" ")

    path = @path
    path = path.match(/\[\d+\]$/).pre_match if path =~ /\[\d+\]$/

    @info.update(
      "format"     => format,
      "width"      => Integer(width),
      "height"     => Integer(height),
      "dimensions" => [Integer(width), Integer(height)],
      "size"       => File.size(path),
      "human_size" => size,
    )

    @info.fetch(value)
  end
rescue ArgumentError, TypeError
  raise MiniMagick::Invalid, "image data can't be read"
end
clear() click to toggle source
# File lib/mini_magick/image/info.rb, line 35
def clear
  @info.clear
end
colorspace() click to toggle source
# File lib/mini_magick/image/info.rb, line 71
def colorspace
  @info["colorspace"] ||= self["%r"]
end
data() click to toggle source
# File lib/mini_magick/image/info.rb, line 116
def data
  @info["data"] ||= (
    json = MiniMagick.convert do |convert|
      convert << path
      convert << "json:"
    end

    data = JSON.parse(json)
    data = data.fetch(0) if data.is_a?(Array)
    data.fetch("image")
  )
end
exif() click to toggle source
# File lib/mini_magick/image/info.rb, line 87
def exif
  @info["exif"] ||= (
    hash = {}
    output = self["%[EXIF:*]"]

    output.each_line do |line|
      line = line.chomp("\n")

      if match = line.match(/^exif:/)
        key, value = match.post_match.split("=", 2)
        value = decode_comma_separated_ascii_characters(value) if ASCII_ENCODED_EXIF_KEYS.include?(key)
        hash[key] = value
      else
        hash[hash.keys.last] << "\n#{line}"
      end
    end

    hash
  )
end
identify() { |builder| ... } click to toggle source
# File lib/mini_magick/image/info.rb, line 129
def identify
  MiniMagick.identify do |builder|
    yield builder if block_given?
    builder << path
  end
end
parse_warnings(raw_info) click to toggle source
# File lib/mini_magick/image/info.rb, line 61
def parse_warnings(raw_info)
  return raw_info unless raw_info.split("\n").size > 1

  raw_info.split("\n").each do |line|
    # must match "%m %w %h %b"
    return line if line.match?(/^[A-Z]+ \d+ \d+ \d+(|\.\d+)([KMGTPEZY]{0,1})B$/)
  end
  raise TypeError
end
raw(value) click to toggle source
# File lib/mini_magick/image/info.rb, line 108
def raw(value)
  @info["raw:#{value}"] ||= identify { |b| b.format(value) }
end
raw_exif(value) click to toggle source
# File lib/mini_magick/image/info.rb, line 83
def raw_exif(value)
  self["%[#{value}]"]
end
resolution(unit = nil) click to toggle source
# File lib/mini_magick/image/info.rb, line 75
def resolution(unit = nil)
  output = identify do |b|
    b.units unit if unit
    b.format "%x %y"
  end
  output.split(" ").map(&:to_i)
end
signature() click to toggle source
# File lib/mini_magick/image/info.rb, line 112
def signature
  @info["signature"] ||= self["%#"]
end

Private Instance Methods

decode_comma_separated_ascii_characters(encoded_value) click to toggle source
# File lib/mini_magick/image/info.rb, line 138
def decode_comma_separated_ascii_characters(encoded_value)
  return encoded_value unless encoded_value.include?(',')
  encoded_value.scan(/\d+/).map(&:to_i).map(&:chr).join
end
path() click to toggle source
# File lib/mini_magick/image/info.rb, line 143
def path
  value = @path
  value += "[0]" unless value =~ /\[\d+\]$/
  value
end