mini_magick 3.8.0 → 4.2.0

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.
@@ -0,0 +1,40 @@
1
+ require "benchmark"
2
+
3
+ module MiniMagick
4
+ ##
5
+ # Responsible for logging commands to stdout (activated when
6
+ # `MiniMagick.debug` is set to `true`). Implements a simplified Logger
7
+ # interface.
8
+ #
9
+ # @private
10
+ #
11
+ class Logger
12
+
13
+ attr_accessor :format
14
+
15
+ def initialize(io)
16
+ @io = io
17
+ @format = "[%<duration>.2fs] %<command>s"
18
+ end
19
+
20
+ def debug(command, &action)
21
+ benchmark(action) do |duration|
22
+ output(duration: duration, command: command) if MiniMagick.debug
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def output(data)
29
+ printf @io, "#{format}\n", data
30
+ end
31
+
32
+ def benchmark(action)
33
+ return_value = nil
34
+ duration = Benchmark.realtime { return_value = action.call }
35
+ yield duration
36
+ return_value
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,61 @@
1
+ require "mini_magick/logger"
2
+ require "timeout"
3
+
4
+ module MiniMagick
5
+ ##
6
+ # Sends commands to the shell (more precisely, it sends commands directly to
7
+ # the operating system).
8
+ #
9
+ # @private
10
+ #
11
+ class Shell
12
+
13
+ def initialize(whiny = true)
14
+ @whiny = whiny
15
+ end
16
+
17
+ def run(command)
18
+ stdout, stderr, code = execute(command)
19
+
20
+ case code
21
+ when 1
22
+ fail MiniMagick::Error, "`#{command.join(" ")}` failed with error:\n#{stderr}"
23
+ when 127
24
+ fail MiniMagick::Error, stderr
25
+ end if @whiny
26
+
27
+ $stderr.print(stderr)
28
+
29
+ stdout
30
+ end
31
+
32
+ def execute(command)
33
+ stdout, stderr, status =
34
+ MiniMagick.logger.debug(command.join(" ")) do
35
+ Timeout.timeout(MiniMagick.timeout) do
36
+ send("execute_#{MiniMagick.shell_api.gsub("-", "_")}", *command)
37
+ end
38
+ end
39
+
40
+ [stdout, stderr, status.exitstatus]
41
+ rescue Errno::ENOENT, IOError
42
+ ["", "executable not found: \"#{command.first}\"", 127]
43
+ end
44
+
45
+ private
46
+
47
+ def execute_open3(*command)
48
+ require "open3"
49
+ Open3.capture3(*command)
50
+ end
51
+
52
+ def execute_posix_spawn(*command)
53
+ require "posix-spawn"
54
+ pid, stdin, stdout, stderr = POSIX::Spawn.popen4(*command)
55
+ Process.waitpid(pid)
56
+
57
+ [stdout.read, stderr.read, $?]
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/animate.php
5
+ #
6
+ class Animate < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("animate", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/compare.php
5
+ #
6
+ class Compare < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("compare", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/composite.php
5
+ #
6
+ class Composite < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("composite", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/conjure.php
5
+ #
6
+ class Conjure < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("conjure", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/convert.php
5
+ #
6
+ class Convert < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("convert", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/display.php
5
+ #
6
+ class Display < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("display", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/identify.php
5
+ #
6
+ class Identify < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("identify", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/import.php
5
+ #
6
+ class Import < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("import", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/mogrify.php
5
+ #
6
+ class Mogrify < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("mogrify", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/montage.php
5
+ #
6
+ class Montage < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("montage", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module MiniMagick
2
+ class Tool
3
+ ##
4
+ # @see http://www.imagemagick.org/script/stream.php
5
+ #
6
+ class Stream < MiniMagick::Tool
7
+
8
+ def initialize(*args)
9
+ super("stream", *args)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,273 @@
1
+ require "mini_magick/shell"
2
+
3
+ module MiniMagick
4
+ ##
5
+ # Abstract class that wraps command-line tools. It shouldn't be used directly,
6
+ # but through one of its subclasses (e.g. {MiniMagick::Tool::Mogrify}). Use
7
+ # this class if you want to be closer to the metal and execute ImageMagick
8
+ # commands directly, but still with a nice Ruby interface.
9
+ #
10
+ # @example
11
+ # MiniMagick::Tool::Mogrify.new do |builder|
12
+ # builder.resize "500x500"
13
+ # builder << "path/to/image.jpg"
14
+ # end
15
+ #
16
+ class Tool
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
+ # @private
31
+ def self.inherited(child)
32
+ child_name = child.name.split("::").last.downcase
33
+ child.send :include, MiniMagick::Tool::OptionMethods.new(child_name)
34
+ end
35
+
36
+ ##
37
+ # Aside from classic instantiation, it also accepts a block, and then
38
+ # executes the command in the end.
39
+ #
40
+ # @example
41
+ # version = MiniMagick::Tool::Identify.new { |b| b.version }
42
+ # puts version
43
+ #
44
+ # @return [MiniMagick::Tool, String] If no block is given, returns an
45
+ # instance of the tool, if block is given, returns the output of the
46
+ # command.
47
+ #
48
+ def self.new(*args)
49
+ instance = super(*args)
50
+
51
+ if block_given?
52
+ yield instance
53
+ instance.call
54
+ else
55
+ instance
56
+ end
57
+ end
58
+
59
+ # @private
60
+ attr_reader :name, :args
61
+
62
+ # @param whiny [Boolean] Whether to raise errors on exit codes different
63
+ # than 0.
64
+ # @example
65
+ # MiniMagick::Tool::Identify.new(false) do |identify|
66
+ # identify.help # returns exit status 1, which would otherwise throw an error
67
+ # end
68
+ def initialize(name, whiny = MiniMagick.whiny)
69
+ @name = name
70
+ @whiny = whiny
71
+ @args = []
72
+ end
73
+
74
+ ##
75
+ # Executes the command that has been built up.
76
+ #
77
+ # @example
78
+ # mogrify = MiniMagick::Tool::Mogrify.new
79
+ # mogrify.resize("500x500")
80
+ # mogrify << "path/to/image.jpg"
81
+ # mogirfy.call # executes `mogrify -resize 500x500 path/to/image.jpg`
82
+ #
83
+ # @param whiny [Boolean] Whether you want an error to be raised when
84
+ # ImageMagick returns an exit code of 1. You may want this because
85
+ # some ImageMagick's commands (`identify -help`) return exit code 1,
86
+ # even though no error happened.
87
+ #
88
+ # @return [String] Output of the command
89
+ #
90
+ def call(whiny = @whiny)
91
+ shell = MiniMagick::Shell.new(whiny)
92
+ shell.run(command).strip
93
+ end
94
+
95
+ ##
96
+ # The currently built-up command.
97
+ #
98
+ # @return [Array<String>]
99
+ #
100
+ # @example
101
+ # mogrify = MiniMagick::Tool::Mogrify.new
102
+ # mogrify.resize "500x500"
103
+ # mogrify.contrast
104
+ # mogrify.command #=> ["mogrify", "-resize", "500x500", "-contrast"]
105
+ #
106
+ def command
107
+ [*executable, *args]
108
+ end
109
+
110
+ ##
111
+ # The executable used for this tool. Respects
112
+ # {MiniMagick::Configuration#cli} and {MiniMagick::Configuration#cli_path}.
113
+ #
114
+ # @return [Array<String>]
115
+ #
116
+ # @example
117
+ # MiniMagick.configure { |config| config.cli = :graphicsmagick }
118
+ # identify = MiniMagick::Tool::Identify.new
119
+ # identify.executable #=> ["gm", "identify"]
120
+ #
121
+ def executable
122
+ exe = [name]
123
+ exe.unshift "gm" if MiniMagick.graphicsmagick?
124
+ exe.unshift File.join(MiniMagick.cli_path, exe.shift) if MiniMagick.cli_path
125
+ exe
126
+ end
127
+
128
+ ##
129
+ # Appends raw options, useful for appending image paths.
130
+ #
131
+ # @return [self]
132
+ #
133
+ def <<(arg)
134
+ args << arg.to_s
135
+ self
136
+ end
137
+
138
+ ##
139
+ # Merges a list of raw options.
140
+ #
141
+ # @return [self]
142
+ #
143
+ def merge!(new_args)
144
+ new_args.each { |arg| self << arg }
145
+ self
146
+ end
147
+
148
+ ##
149
+ # Changes the last operator to its "plus" form.
150
+ #
151
+ # @example
152
+ # MiniMagick::Tool::Mogrify.new do |mogrify|
153
+ # mogrify.antialias.+
154
+ # mogrify.distort.+("Perspective", "0,0,4,5 89,0,45,46")
155
+ # end
156
+ # # executes `mogrify +antialias +distort Perspective '0,0,4,5 89,0,45,46'`
157
+ #
158
+ # @return [self]
159
+ #
160
+ def +(*values)
161
+ args[-1] = args[-1].sub(/^-/, '+')
162
+ self.merge!(values)
163
+ self
164
+ end
165
+
166
+ ##
167
+ # Create an ImageMagick stack in the command (surround.
168
+ #
169
+ # @example
170
+ # MiniMagick::Tool::Convert.new do |convert|
171
+ # convert << "wand.gif"
172
+ # convert.stack do |stack|
173
+ # stack << "wand.gif"
174
+ # stack.rotate(30)
175
+ # end
176
+ # convert.append.+
177
+ # convert << "images.gif"
178
+ # end
179
+ # # executes `convert wand.gif \( wizard.gif -rotate 30 \) +append images.gif`
180
+ #
181
+ def stack
182
+ self << "("
183
+ yield self
184
+ self << ")"
185
+ end
186
+
187
+ private
188
+
189
+ ##
190
+ # Dynamically generates modules with dynamically generated option methods
191
+ # for each command-line tool. It uses the `-help` page of a command-line
192
+ # tool and generates methods from it. It then includes the generated
193
+ # module into the tool class.
194
+ #
195
+ # @private
196
+ #
197
+ class OptionMethods < Module # think about it for a minute
198
+
199
+ def self.instances
200
+ @instances ||= []
201
+ end
202
+
203
+ def initialize(tool_name)
204
+ @tool_name = tool_name
205
+ reload_methods
206
+ self.class.instances << self
207
+ end
208
+
209
+ def to_s
210
+ "OptionMethods(#{@tool_name})"
211
+ end
212
+
213
+ ##
214
+ # Dynamically generates operator methods from the "-help" page.
215
+ #
216
+ def reload_methods
217
+ instance_methods(false).each { |method| undef_method(method) }
218
+ creation_operator *creation_operators
219
+ option *cli_options
220
+ end
221
+
222
+ private
223
+
224
+ ##
225
+ # Creates method based on command-line option's name.
226
+ #
227
+ # mogrify = MiniMagick::Tool.new("mogrify")
228
+ # mogrify.antialias
229
+ # mogrify.depth(8)
230
+ # mogrify.resize("500x500")
231
+ # mogirfy.command.join(" ")
232
+ # #=> "mogrify -antialias -depth 8 -resize 500x500"
233
+ #
234
+ def option(*options)
235
+ options.each do |option|
236
+ define_method(option[1..-1].gsub('-', '_')) do |*values|
237
+ self << option
238
+ self.merge!(values)
239
+ self
240
+ end
241
+ end
242
+ end
243
+
244
+ ##
245
+ # Creates method based on creation operator's name.
246
+ #
247
+ # mogrify = MiniMagick::Tool.new("mogrify")
248
+ # mogrify.canvas("khaki")
249
+ # mogrify.command.join(" ") #=> "mogrify canvas:khaki"
250
+ #
251
+ def creation_operator(*operators)
252
+ operators.each do |operator|
253
+ define_method(operator.gsub('-', '_')) do |value = nil|
254
+ self << "#{operator}:#{value}"
255
+ self
256
+ end
257
+ end
258
+ end
259
+
260
+ def creation_operators
261
+ %w[xc canvas logo rose gradient radial-gradient
262
+ plasma tile pattern label caption text]
263
+ end
264
+
265
+ def cli_options
266
+ help = MiniMagick::Tool.new(@tool_name, false) { |b| b << "-help" }
267
+ cli_options = help.scan(/^\s+-[a-z\-]+/).map(&:strip)
268
+ end
269
+
270
+ end
271
+
272
+ end
273
+ end
@@ -1,36 +1,35 @@
1
- require 'rbconfig'
1
+ require "tempfile"
2
2
 
3
3
  module MiniMagick
4
+ # @private
4
5
  module Utilities
5
- class << self
6
- # Cross-platform way of finding an executable in the $PATH.
7
- #
8
- # which('ruby') #=> /usr/bin/ruby
9
- def which(cmd)
10
- exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
11
- ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
12
- exts.each do |ext|
13
- exe = File.join(path, "#{cmd}#{ext}")
14
- return exe if File.executable? exe
15
- end
16
- end
17
- nil
18
- end
19
6
 
20
- # Finds out if the host OS is windows
21
- def windows?
22
- RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
23
- end
7
+ module_function
24
8
 
25
- def windows_escape(value)
26
- # For Windows, ^ is the escape char, equivalent to \ in Unix.
27
- escaped = value.gsub(/\^/, '^^').gsub(/>/, '^>')
28
- if escaped !~ /^".+"$/ && escaped.include?("'")
29
- escaped.inspect
30
- else
31
- escaped
9
+ ##
10
+ # Cross-platform way of finding an executable in the $PATH.
11
+ #
12
+ # @example
13
+ # MiniMagick::Utilities.which('ruby') #=> "/usr/bin/ruby"
14
+ #
15
+ def which(cmd)
16
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
17
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
18
+ exts.each do |ext|
19
+ exe = File.join(path, "#{cmd}#{ext}")
20
+ return exe if File.executable? exe
32
21
  end
33
22
  end
23
+ nil
34
24
  end
25
+
26
+ def tempfile(extension)
27
+ Tempfile.new(["mini_magick", extension]).tap do |tempfile|
28
+ tempfile.binmode
29
+ yield tempfile if block_given?
30
+ tempfile.close
31
+ end
32
+ end
33
+
35
34
  end
36
35
  end
@@ -1,3 +1,17 @@
1
1
  module MiniMagick
2
- VERSION = '3.8.0'
2
+ ##
3
+ # @return [Gem::Version]
4
+ #
5
+ def self.version
6
+ Gem::Version.new VERSION::STRING
7
+ end
8
+
9
+ module VERSION
10
+ MAJOR = 4
11
+ MINOR = 2
12
+ TINY = 0
13
+ PRE = nil
14
+
15
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
16
+ end
3
17
  end