Class: MiniMagick::Image::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_magick/image/info.rb

Constant Summary collapse

ASCII_ENCODED_EXIF_KEYS =
%w[ExifVersion FlashPixVersion]

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Info

Returns a new instance of Info.



9
10
11
12
# File 'lib/mini_magick/image/info.rb', line 9

def initialize(path)
  @path = path
  @info = {}
end

Instance Method Details

#[](value, *args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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 "mime_type"
    mime_type
  when "resolution"
    resolution(*args)
  when "signature"
    signature
  when /^EXIF\:/i
    raw_exif(value)
  when "exif"
    exif
  when "details"
    details
  when "data"
    data
  else
    raw(value)
  end
end

#cheap_info(value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mini_magick/image/info.rb', line 43

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

#clearObject



39
40
41
# File 'lib/mini_magick/image/info.rb', line 39

def clear
  @info.clear
end

#colorspaceObject



75
76
77
# File 'lib/mini_magick/image/info.rb', line 75

def colorspace
  @info["colorspace"] ||= self["%r"]
end

#dataObject

Raises:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mini_magick/image/info.rb', line 165

def data
  raise Error, "MiniMagick::Image#data isn't supported on GraphicsMagick. Use MiniMagick::Image#details instead." if MiniMagick.graphicsmagick?

  @info["data"] ||= (
    json = MiniMagick::Tool::Convert.new do |convert|
      convert << path
      convert << "json:"
    end

    data = JSON.parse(json)
    data = data.fetch(0) if data.is_a?(Array)
    data.fetch("image")
  )
end

#detailsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mini_magick/image/info.rb', line 132

def details
  warn "[MiniMagick] MiniMagick::Image#details has been deprecated, as it was causing too many parsing errors. You should use MiniMagick::Image#data instead, which differs in a way that the keys are in camelcase." if MiniMagick.imagemagick? || MiniMagick.imagemagick7?

  @info["details"] ||= (
    details_string = identify(&:verbose)
    key_stack = []
    details_string.lines.to_a[1..-1].each_with_object({}) do |line, details_hash|
      next if !line.valid_encoding? || line.strip.length.zero?

      level = line[/^\s*/].length / 2 - 1
      if level >= 0
        key_stack.pop until key_stack.size <= level
      else
        # Some metadata, such as SVG clipping paths, will be saved without
        # indentation, resulting in a level of -1
        last_key = details_hash.keys.last
        details_hash[last_key] = '' if details_hash[last_key].empty?
        details_hash[last_key] << line
        next
      end

      key, _, value = line.partition(/:[\s]/).map(&:strip)
      hash = key_stack.inject(details_hash) { |_hash, _key| _hash.fetch(_key) }
      if value.empty?
        hash[key] = {}
        key_stack.push key
      else
        hash[key] = value
      end
    end
  )
end

#exifObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mini_magick/image/info.rb', line 95

def exif
  @info["exif"] ||= (
    hash = {}
    output = self["%[EXIF:*]"]

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

      case MiniMagick.cli
      when :imagemagick, :imagemagick7
        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
      when :graphicsmagick
        next if line == "unknown"
        key, value = line.split("=", 2)
        value.gsub!("\\012", "\n") # convert "\012" characters to newlines
        hash[key] = value
      end
    end

    hash
  )
end

#identifyObject



180
181
182
183
184
185
# File 'lib/mini_magick/image/info.rb', line 180

def identify
  MiniMagick::Tool::Identify.new do |builder|
    yield builder if block_given?
    builder << path
  end
end

#mime_typeObject



79
80
81
# File 'lib/mini_magick/image/info.rb', line 79

def mime_type
  "image/#{self["format"].downcase}"
end

#parse_warnings(raw_info) ⇒ Object

Raises:

  • (TypeError)


65
66
67
68
69
70
71
72
73
# File 'lib/mini_magick/image/info.rb', line 65

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) ⇒ Object



124
125
126
# File 'lib/mini_magick/image/info.rb', line 124

def raw(value)
  @info["raw:#{value}"] ||= identify { |b| b.format(value) }
end

#raw_exif(value) ⇒ Object



91
92
93
# File 'lib/mini_magick/image/info.rb', line 91

def raw_exif(value)
  self["%[#{value}]"]
end

#resolution(unit = nil) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/mini_magick/image/info.rb', line 83

def resolution(unit = nil)
  output = identify do |b|
    b.units unit if unit
    b.format "%x %y"
  end
  output.split(" ").map(&:to_i)
end

#signatureObject



128
129
130
# File 'lib/mini_magick/image/info.rb', line 128

def signature
  @info["signature"] ||= self["%#"]
end