class MiniMagick::Tool
Class that wraps command-line tools directly, as opposed MiniMagick::Image
which is more high-level.
@example
MiniMagick.mogrify do |mogrify| mogrify.resize "500x500" mogrify << "path/to/image.jpg" end
Constants
- CREATION_OPERATORS
Attributes
@private
@private
Public Class Methods
Aside from classic instantiation, it also accepts a block, and then executes the command in the end.
@example
puts MiniMagick.identify(&:version)
@return [MiniMagick::Tool, String] If no block is given, returns an
instance of the tool, if block is given, returns the output of the command.
# File lib/mini_magick/tool.rb, line 30 def self.new(*args) instance = super(*args) if block_given? yield instance instance.call else instance end end
@param name [String] @param options [Hash] @option options [Boolean] :errors Whether to raise errors on non-zero
exit codes.
@option options [Boolean] :warnings Whether to print warnings to stderrr. @option options [String] :stdin Content to send to standard input stream. @example
MiniMagick.identify(errors: false) do |identify| identify.help # returns exit status 1, which would otherwise throw an error end
# File lib/mini_magick/tool.rb, line 54 def initialize(name, **options) @name = name @args = [] @options = options end
Public Instance Methods
Changes the last operator to its “plus” form.
@example
MiniMagick.mogrify do |mogrify| mogrify.antialias.+ mogrify.distort.+("Perspective", "0,0,4,5 89,0,45,46") end # executes `mogrify +antialias +distort Perspective '0,0,4,5 89,0,45,46'`
@return [self]
# File lib/mini_magick/tool.rb, line 162 def +(*values) args[-1] = args[-1].sub(/^-/, '+') self.merge!(values) self end
Appends raw options, useful for appending image paths.
@return [self]
# File lib/mini_magick/tool.rb, line 135 def <<(arg) args << arg.to_s self end
Executes the command that has been built up.
@example
mogrify = MiniMagick.mogrify mogrify.resize("500x500") mogrify << "path/to/image.jpg" mogrify.call # executes `mogrify -resize 500x500 path/to/image.jpg`
@example
mogrify = MiniMagick.mogrify # build the command mogrify.call do |stdout, stderr, status| # ... end
@yield [Array] Optionally yields stdout, stderr, and exit status
@return [String] Returns the output of the command
# File lib/mini_magick/tool.rb, line 80 def call(**options) options = @options.merge(options) options[:warnings] = false if block_given? shell = MiniMagick::Shell.new stdout, stderr, status = shell.run(command, **options) yield stdout, stderr, status if block_given? stdout.chomp("\n") end
This option is a valid ImageMagick option, but it’s also a Ruby method, so we need to override it so that it correctly acts as an option method.
# File lib/mini_magick/tool.rb, line 242 def clone(*args) self << '-clone' self.merge!(args) self end
The currently built-up command.
@return [Array<String>]
@example
mogrify = MiniMagick.mogrify mogrify.resize "500x500" mogrify.contrast mogrify.command #=> ["mogrify", "-resize", "500x500", "-contrast"]
# File lib/mini_magick/tool.rb, line 102 def command [*executable, *args] end
The executable used for this tool. Respects {MiniMagick::Configuration#cli_prefix}.
@return [Array<String>]
@example
identify = MiniMagick.identify identify.executable #=> ["magick", "identify"]
@example
MiniMagick.configure do |config| config.cli_prefix = ['firejail', '--force'] end identify = MiniMagick.identify identify.executable #=> ["firejail", "--force", "magick", "identify"]
# File lib/mini_magick/tool.rb, line 123 def executable exe = [name] exe.unshift "magick" if MiniMagick.imagemagick7? && name != "magick" Array(MiniMagick.cli_prefix).reverse_each { |p| exe.unshift p } if MiniMagick.cli_prefix exe end
Merges a list of raw options.
@return [self]
# File lib/mini_magick/tool.rb, line 145 def merge!(new_args) new_args.each { |arg| self << arg } self end
Any undefined method will be transformed into a CLI option
@example
mogrify = MiniMagick::Tool.new("mogrify") mogrify.adaptive_blur("...") mogrify.foo_bar mogrify.command.join(" ") # => "mogrify -adaptive-blur ... -foo-bar"
# File lib/mini_magick/tool.rb, line 257 def method_missing(name, *args) option = "-#{name.to_s.tr('_', '-')}" self << option self.merge!(args) self end
Create an ImageMagick stack in the command (surround.
@example
MiniMagick.convert do |convert| convert << "wand.gif" convert.stack do |stack| stack << "wand.gif" stack.rotate(30) end convert.append.+ convert << "images.gif" end # executes `convert wand.gif \( wizard.gif -rotate 30 \) +append images.gif`
# File lib/mini_magick/tool.rb, line 183 def stack(*args) self << "(" args.each do |value| case value when Hash then value.each { |key, value| send(key, *value) } when String then self << value end end yield self if block_given? self << ")" end
Adds ImageMagick’s pseudo-filename ‘-` for standard input.
@example
identify = MiniMagick.identify identify.stdin identify.call(stdin: image_content) # executes `identify -` with the given standard input
# File lib/mini_magick/tool.rb, line 204 def stdin self << "-" end
Adds ImageMagick’s pseudo-filename ‘-` for standard output.
@example
content = MiniMagick.convert do |convert| convert << "input.jpg" convert.auto_orient convert.stdout end # executes `convert input.jpg -auto-orient -` which returns file contents
# File lib/mini_magick/tool.rb, line 219 def stdout self << "-" end