mini_magick 4.1.0 → 4.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8921e089d8d63d1391e76876bc63b4e3ff953706
4
- data.tar.gz: 38930195578ded9545d86b4225718671a9cd8b6a
3
+ metadata.gz: 21c28d5cb2938a6c126985b362544f64858220aa
4
+ data.tar.gz: 94bdee8c2c92bc7d9e5548202397dff054bd99bc
5
5
  SHA512:
6
- metadata.gz: 39cc0fccd098a964a02be8e2cde7565d492ab9a5831df89a2f1b90d751d67368c13584c05ef3253aab51f8aecf107a52ca489a6849bd32e80a334869500f4628
7
- data.tar.gz: 9c620a81d8b8e601a172d3b8333f4020048e2b7d385ce40fe85caff7f6df7480f1e08c8046d8fa10d66164eb264efe06d9303f111444f55c6193546ca34ab0f2
6
+ metadata.gz: 907e11e72c97a02dcc8eaeb80d8c1bdee96561c8ae0134a14a2c00b4385e2dd6b1d9526dead4f523b4df219fe78c09fc1472609e87983a6d0b3d674aa21f8033
7
+ data.tar.gz: d7f00b1e55cd0e5ab4abfaccd62fa220a34adbc03556a5b92626aa68dc41bd4c3dd61ac1fd0e7a4664a6e6e70971e95227350a11700dccb1ced09914edd782b7
@@ -7,7 +7,7 @@ module MiniMagick
7
7
  # Set whether you want to use [ImageMagick](http://www.imagemagick.org) or
8
8
  # [GraphicsMagick](http://www.graphicsmagick.org).
9
9
  #
10
- # @return [Symbol] `:imagemagick` or `:minimagick`
10
+ # @return [Symbol] `:imagemagick` or `:graphicsmagick`
11
11
  #
12
12
  attr_accessor :cli
13
13
  # @private (for backwards compatibility)
@@ -131,7 +131,14 @@ module MiniMagick
131
131
  def cli=(value)
132
132
  @cli = value
133
133
 
134
- unless [:imagemagick, :graphicsmagick].include?(@cli)
134
+ case @cli
135
+ when :imagemagick
136
+ Utilities.which("mogrify") or
137
+ raise MiniMagick::Error, "ImageMagick is not installed or CLI is not found"
138
+ when :graphicsmagick
139
+ Utilities.which("gm") or
140
+ raise MiniMagick::Error, "GraphicsMagick is not installed or CLI is not found"
141
+ else
135
142
  raise ArgumentError,
136
143
  "CLI has to be set to either :imagemagick or :graphicsmagick" \
137
144
  ", was set to #{@cli.inspect}"
@@ -148,8 +155,6 @@ module MiniMagick
148
155
  @logger || MiniMagick::Logger.new($stdout)
149
156
  end
150
157
 
151
- private
152
-
153
158
  def reload_tools
154
159
  MiniMagick::Tool::OptionMethods.instances.each(&:reload_methods)
155
160
  end
@@ -11,7 +11,7 @@ module MiniMagick
11
11
 
12
12
  def [](value, *args)
13
13
  case value
14
- when "format", "width", "height", "dimensions", "size"
14
+ when "format", "width", "height", "dimensions", "size", "human_size"
15
15
  cheap_info(value)
16
16
  when "colorspace"
17
17
  colorspace
@@ -25,6 +25,8 @@ module MiniMagick
25
25
  raw_exif(value)
26
26
  when "exif"
27
27
  exif
28
+ when "details"
29
+ details
28
30
  else
29
31
  raw(value)
30
32
  end
@@ -34,8 +36,6 @@ module MiniMagick
34
36
  @info.clear
35
37
  end
36
38
 
37
- private
38
-
39
39
  def cheap_info(value)
40
40
  @info.fetch(value) do
41
41
  format, width, height, size = self["%m %w %h %b"].split(" ")
@@ -45,17 +45,18 @@ module MiniMagick
45
45
  "width" => Integer(width),
46
46
  "height" => Integer(height),
47
47
  "dimensions" => [Integer(width), Integer(height)],
48
- "size" => size.to_i,
48
+ "size" => File.size(@path),
49
+ "human_size" => size,
49
50
  )
50
51
 
51
52
  @info.fetch(value)
52
53
  end
54
+ rescue ArgumentError, TypeError
55
+ raise MiniMagick::Invalid, "image data can't be read"
53
56
  end
54
57
 
55
58
  def colorspace
56
- @info.fetch("colorspace") do
57
- @info["colorspace"] = self["%r"]
58
- end
59
+ @info["colorspace"] ||= self["%r"]
59
60
  end
60
61
 
61
62
  def mime_type
@@ -75,10 +76,10 @@ module MiniMagick
75
76
  end
76
77
 
77
78
  def exif
78
- @info.fetch("exif") do
79
+ @info["exif"] ||= (
79
80
  output = self["%[EXIF:*]"]
80
81
  pairs = output.gsub(/^exif:/, "").split("\n").map { |line| line.split("=") }
81
- exif = Hash[pairs].tap do |hash|
82
+ Hash[pairs].tap do |hash|
82
83
  ASCII_ENCODED_EXIF_KEYS.each do |key|
83
84
  next unless hash.has_key?(key)
84
85
 
@@ -86,22 +87,35 @@ module MiniMagick
86
87
  hash[key] = decode_comma_separated_ascii_characters(value)
87
88
  end
88
89
  end
89
-
90
- @info["exif"] = exif
91
- end
90
+ )
92
91
  end
93
92
 
94
93
  def raw(value)
95
- key = "raw:#{value}"
96
- @info.fetch(key) do
97
- @info[key] = identify { |b| b.format(value) }
98
- end
94
+ @info["raw:#{value}"] ||= identify { |b| b.format(value) }
99
95
  end
100
96
 
101
97
  def signature
102
- @info.fetch("signature") do
103
- @info["signature"] = self["%#"]
104
- end
98
+ @info["signature"] ||= self["%#"]
99
+ end
100
+
101
+ def details
102
+ @info["details"] ||= (
103
+ details_string = identify(&:verbose)
104
+ key_stack = []
105
+ details_string.lines.to_a[1..-1].each_with_object({}) do |line, details_hash|
106
+ level = line[/^\s*/].length / 2 - 1
107
+ key_stack.pop until key_stack.size <= level
108
+
109
+ key, _, value = line.partition(/:[\s\n]/).map(&:strip)
110
+ hash = key_stack.inject(details_hash) { |hash, key| hash.fetch(key) }
111
+ if value.empty?
112
+ hash[key] = {}
113
+ key_stack.push key
114
+ else
115
+ hash[key] = value
116
+ end
117
+ end
118
+ )
105
119
  end
106
120
 
107
121
  def identify
@@ -113,7 +127,6 @@ module MiniMagick
113
127
 
114
128
  def decode_comma_separated_ascii_characters(encoded_value)
115
129
  return encoded_value unless encoded_value.include?(',')
116
-
117
130
  encoded_value.scan(/\d+/).map(&:to_i).map(&:chr).join
118
131
  end
119
132
 
@@ -223,12 +223,18 @@ module MiniMagick
223
223
  #
224
224
  attribute :dimensions
225
225
  ##
226
- # Returns the file size of the image.
226
+ # Returns the file size of the image (in bytes).
227
227
  #
228
228
  # @return [Integer]
229
229
  #
230
230
  attribute :size
231
231
  ##
232
+ # Returns the file size in a human readable format.
233
+ #
234
+ # @return [String]
235
+ #
236
+ attribute :human_size
237
+ ##
232
238
  # @return [String]
233
239
  #
234
240
  attribute :colorspace
@@ -258,6 +264,11 @@ module MiniMagick
258
264
  # @return [String]
259
265
  #
260
266
  attribute :signature
267
+ ##
268
+ # Returns the information from `identify -verbose` in a Hash format.
269
+ #
270
+ # @return [Hash]
271
+ attribute :details
261
272
 
262
273
  ##
263
274
  # Use this method if you want to access raw Identify's format API.
@@ -424,7 +435,7 @@ module MiniMagick
424
435
  #
425
436
  # @see http://www.imagemagick.org/script/composite.php
426
437
  #
427
- def composite(other_image, output_extension = 'jpg', mask = nil)
438
+ def composite(other_image, output_extension = type.downcase, mask = nil)
428
439
  output_tempfile = MiniMagick::Utilities.tempfile(".#{output_extension}")
429
440
 
430
441
  MiniMagick::Tool::Composite.new do |composite|
@@ -484,14 +495,12 @@ module MiniMagick
484
495
  end
485
496
  end
486
497
 
487
- private
488
-
489
498
  def mogrify(page = nil)
490
499
  @info.clear
491
500
 
492
501
  MiniMagick::Tool::Mogrify.new do |builder|
493
502
  builder.instance_eval do
494
- def format(*)
503
+ def format(*args)
495
504
  fail NoMethodError,
496
505
  "you must call #format on a MiniMagick::Image directly"
497
506
  end
@@ -23,8 +23,6 @@ module MiniMagick
23
23
  end
24
24
  end
25
25
 
26
- private
27
-
28
26
  def output(data)
29
27
  printf @io, "#{format}\n", data
30
28
  end
@@ -10,11 +10,7 @@ module MiniMagick
10
10
  #
11
11
  class Shell
12
12
 
13
- def initialize(whiny = true)
14
- @whiny = whiny
15
- end
16
-
17
- def run(command)
13
+ def run(command, options = {})
18
14
  stdout, stderr, code = execute(command)
19
15
 
20
16
  case code
@@ -22,9 +18,9 @@ module MiniMagick
22
18
  fail MiniMagick::Error, "`#{command.join(" ")}` failed with error:\n#{stderr}"
23
19
  when 127
24
20
  fail MiniMagick::Error, stderr
25
- end if @whiny
21
+ end if options.fetch(:whiny, true)
26
22
 
27
- $stderr.print(stderr)
23
+ $stderr.print(stderr) unless options[:stderr] == false
28
24
 
29
25
  stdout
30
26
  end
@@ -42,8 +38,6 @@ module MiniMagick
42
38
  ["", "executable not found: \"#{command.first}\"", 127]
43
39
  end
44
40
 
45
- private
46
-
47
41
  def execute_open3(*command)
48
42
  require "open3"
49
43
  Open3.capture3(*command)
@@ -15,18 +15,6 @@ module MiniMagick
15
15
  #
16
16
  class Tool
17
17
 
18
- autoload :Animate, "mini_magick/tool/animate"
19
- autoload :Compare, "mini_magick/tool/compare"
20
- autoload :Composite, "mini_magick/tool/composite"
21
- autoload :Conjure, "mini_magick/tool/conjure"
22
- autoload :Convert, "mini_magick/tool/convert"
23
- autoload :Display, "mini_magick/tool/display"
24
- autoload :Identify, "mini_magick/tool/identify"
25
- autoload :Import, "mini_magick/tool/import"
26
- autoload :Mogrify, "mini_magick/tool/mogrify"
27
- autoload :Montage, "mini_magick/tool/montage"
28
- autoload :Stream, "mini_magick/tool/stream"
29
-
30
18
  # @private
31
19
  def self.inherited(child)
32
20
  child_name = child.name.split("::").last.downcase
@@ -78,7 +66,7 @@ module MiniMagick
78
66
  # mogrify = MiniMagick::Tool::Mogrify.new
79
67
  # mogrify.resize("500x500")
80
68
  # mogrify << "path/to/image.jpg"
81
- # mogirfy.call # executes `mogrify -resize 500x500 path/to/image.jpg`
69
+ # mogrify.call # executes `mogrify -resize 500x500 path/to/image.jpg`
82
70
  #
83
71
  # @param whiny [Boolean] Whether you want an error to be raised when
84
72
  # ImageMagick returns an exit code of 1. You may want this because
@@ -87,9 +75,9 @@ module MiniMagick
87
75
  #
88
76
  # @return [String] Output of the command
89
77
  #
90
- def call(whiny = @whiny)
91
- shell = MiniMagick::Shell.new(whiny)
92
- shell.run(command).strip
78
+ def call(whiny = @whiny, options = {})
79
+ shell = MiniMagick::Shell.new
80
+ shell.run(command, options.merge(whiny: whiny)).strip
93
81
  end
94
82
 
95
83
  ##
@@ -184,8 +172,6 @@ module MiniMagick
184
172
  self << ")"
185
173
  end
186
174
 
187
- private
188
-
189
175
  ##
190
176
  # Dynamically generates modules with dynamically generated option methods
191
177
  # for each command-line tool. It uses the `-help` page of a command-line
@@ -219,8 +205,6 @@ module MiniMagick
219
205
  option *cli_options
220
206
  end
221
207
 
222
- private
223
-
224
208
  ##
225
209
  # Creates method based on command-line option's name.
226
210
  #
@@ -228,12 +212,12 @@ module MiniMagick
228
212
  # mogrify.antialias
229
213
  # mogrify.depth(8)
230
214
  # mogrify.resize("500x500")
231
- # mogirfy.command.join(" ")
215
+ # mogrify.command.join(" ")
232
216
  # #=> "mogrify -antialias -depth 8 -resize 500x500"
233
217
  #
234
218
  def option(*options)
235
219
  options.each do |option|
236
- define_method(option[1..-1].gsub('-', '_')) do |*values|
220
+ define_method(option[1..-1].tr('-', '_')) do |*values|
237
221
  self << option
238
222
  self.merge!(values)
239
223
  self
@@ -263,11 +247,28 @@ module MiniMagick
263
247
  end
264
248
 
265
249
  def cli_options
266
- help = MiniMagick::Tool.new(@tool_name, false) { |b| b << "-help" }
267
- cli_options = help.scan(/^\s+-[a-z\-]+/).map(&:strip)
250
+ tool = MiniMagick::Tool.new(@tool_name)
251
+ tool << "-help"
252
+ help_page = tool.call(false, stderr: false)
253
+
254
+ cli_options = help_page.scan(/^\s+-[a-z\-]+/).map(&:strip)
255
+ cli_options << "-gravity" if @tool_name == "mogrify" && MiniMagick.graphicsmagick?
256
+ cli_options
268
257
  end
269
258
 
270
259
  end
271
260
 
272
261
  end
273
262
  end
263
+
264
+ require "mini_magick/tool/animate"
265
+ require "mini_magick/tool/compare"
266
+ require "mini_magick/tool/composite"
267
+ require "mini_magick/tool/conjure"
268
+ require "mini_magick/tool/convert"
269
+ require "mini_magick/tool/display"
270
+ require "mini_magick/tool/identify"
271
+ require "mini_magick/tool/import"
272
+ require "mini_magick/tool/mogrify"
273
+ require "mini_magick/tool/montage"
274
+ require "mini_magick/tool/stream"
@@ -8,8 +8,8 @@ module MiniMagick
8
8
 
9
9
  module VERSION
10
10
  MAJOR = 4
11
- MINOR = 1
12
- TINY = 0
11
+ MINOR = 2
12
+ TINY = 10
13
13
  PRE = nil
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
data/lib/mini_magick.rb CHANGED
@@ -1,6 +1,4 @@
1
1
  require 'mini_magick/configuration'
2
- require 'mini_magick/tool'
3
- require 'mini_magick/image'
4
2
 
5
3
  module MiniMagick
6
4
 
@@ -19,6 +17,7 @@ module MiniMagick
19
17
  old_cli = self.cli
20
18
  self.cli = cli
21
19
  yield
20
+ ensure
22
21
  self.cli = old_cli
23
22
  end
24
23
 
@@ -51,3 +50,6 @@ module MiniMagick
51
50
  class Invalid < StandardError; end
52
51
 
53
52
  end
53
+
54
+ require 'mini_magick/tool'
55
+ require 'mini_magick/image'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_magick
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Johnson
@@ -9,10 +9,11 @@ authors:
9
9
  - Peter Kieltyka
10
10
  - James Miller
11
11
  - Thiago Fernandes Massa
12
+ - Janko Marohnić
12
13
  autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
- date: 2015-02-17 00:00:00.000000000 Z
16
+ date: 2015-08-08 00:00:00.000000000 Z
16
17
  dependencies:
17
18
  - !ruby/object:Gem::Dependency
18
19
  name: rake
@@ -63,6 +64,7 @@ email:
63
64
  - peter@nulayer.com
64
65
  - bensie@gmail.com
65
66
  - thiagown@gmail.com
67
+ - janko.marohnic@gmail.com
66
68
  executables: []
67
69
  extensions: []
68
70
  extra_rdoc_files: []