Class: MiniMagick::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_magick/tool.rb,
lib/mini_magick/tool/import.rb,
lib/mini_magick/tool/stream.rb,
lib/mini_magick/tool/animate.rb,
lib/mini_magick/tool/compare.rb,
lib/mini_magick/tool/conjure.rb,
lib/mini_magick/tool/convert.rb,
lib/mini_magick/tool/display.rb,
lib/mini_magick/tool/mogrify.rb,
lib/mini_magick/tool/montage.rb,
lib/mini_magick/tool/identify.rb,
lib/mini_magick/tool/composite.rb

Overview

Abstract class that wraps command-line tools. It shouldn’t be used directly, but through one of its subclasses (e.g. Mogrify). Use this class if you want to be closer to the metal and execute ImageMagick commands directly, but still with a nice Ruby interface.

Examples:

MiniMagick::Tool::Mogrify.new do |builder|
  builder.resize "500x500"
  builder << "path/to/image.jpg"
end

Defined Under Namespace

Classes: Animate, Compare, Composite, Conjure, Convert, Display, Identify, Import, Mogrify, Montage, OptionMethods, Stream

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, whiny = MiniMagick.whiny) ⇒ Tool

Returns a new instance of Tool.

Examples:

MiniMagick::Tool::Identify.new(false) do |identify|
  identify.help # returns exit status 1, which would otherwise throw an error
end

Parameters:

  • whiny (Boolean) (defaults to: MiniMagick.whiny)

    Whether to raise errors on exit codes different than 0.



68
69
70
71
72
# File 'lib/mini_magick/tool.rb', line 68

def initialize(name, whiny = MiniMagick.whiny)
  @name  = name
  @whiny = whiny
  @args  = []
end

Instance Attribute Details

#argsObject (readonly)



60
61
62
# File 'lib/mini_magick/tool.rb', line 60

def args
  @args
end

#nameObject (readonly)



60
61
62
# File 'lib/mini_magick/tool.rb', line 60

def name
  @name
end

Class Method Details

.inherited(child) ⇒ Object



31
32
33
34
# File 'lib/mini_magick/tool.rb', line 31

def self.inherited(child)
  child_name = child.name.split("::").last.downcase
  child.send :include, MiniMagick::Tool::OptionMethods.new(child_name)
end

.new(*args) ⇒ MiniMagick::Tool, String

Aside from classic instantiation, it also accepts a block, and then executes the command in the end.

Examples:

version = MiniMagick::Tool::Identify.new { |b| b.version }
puts version

Returns:

  • (MiniMagick::Tool, String)

    If no block is given, returns an instance of the tool, if block is given, returns the output of the command.



48
49
50
51
52
53
54
55
56
57
# File 'lib/mini_magick/tool.rb', line 48

def self.new(*args)
  instance = super(*args)

  if block_given?
    yield instance
    instance.call
  else
    instance
  end
end

Instance Method Details

#+(*values) ⇒ self

Changes the last operator to its “plus” form.

Examples:

MiniMagick::Tool::Mogrify.new 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'`

Returns:

  • (self)


160
161
162
163
164
# File 'lib/mini_magick/tool.rb', line 160

def +(*values)
  args[-1] = args[-1].sub(/^-/, '+')
  self.merge!(values)
  self
end

#<<(arg) ⇒ self

Appends raw options, useful for appending image paths.

Returns:

  • (self)


133
134
135
136
# File 'lib/mini_magick/tool.rb', line 133

def <<(arg)
  args << arg.to_s
  self
end

#call(whiny = @whiny) ⇒ String

Executes the command that has been built up.

Examples:

mogrify = MiniMagick::Tool::Mogrify.new
mogrify.resize("500x500")
mogrify << "path/to/image.jpg"
mogirfy.call # executes `mogrify -resize 500x500 path/to/image.jpg`

Parameters:

  • whiny (Boolean) (defaults to: @whiny)

    Whether you want an error to be raised when ImageMagick returns an exit code of 1. You may want this because some ImageMagick’s commands (‘identify -help`) return exit code 1, even though no error happened.

Returns:

  • (String)

    Output of the command



90
91
92
93
# File 'lib/mini_magick/tool.rb', line 90

def call(whiny = @whiny)
  shell = MiniMagick::Shell.new(whiny)
  shell.run(command).strip
end

#commandArray<String>

The currently built-up command.

Examples:

mogrify = MiniMagick::Tool::Mogrify.new
mogrify.resize "500x500"
mogrify.contrast
mogrify.command #=> ["mogrify", "-resize", "500x500", "-contrast"]

Returns:

  • (Array<String>)


106
107
108
# File 'lib/mini_magick/tool.rb', line 106

def command
  [*executable, *args]
end

#executableArray<String>

The executable used for this tool. Respects Configuration#cli and Configuration#cli_path.

Examples:

MiniMagick.configure { |config| config.cli = :graphicsmagick }
identify = MiniMagick::Tool::Identify.new
identify.executable #=> ["gm", "identify"]

Returns:

  • (Array<String>)


121
122
123
124
125
126
# File 'lib/mini_magick/tool.rb', line 121

def executable
  exe = [name]
  exe.unshift "gm" if MiniMagick.graphicsmagick?
  exe.unshift File.join(MiniMagick.cli_path, exe.shift) if MiniMagick.cli_path
  exe
end

#merge!(new_args) ⇒ self

Merges a list of raw options.

Returns:

  • (self)


143
144
145
146
# File 'lib/mini_magick/tool.rb', line 143

def merge!(new_args)
  new_args.each { |arg| self << arg }
  self
end

#stack {|_self| ... } ⇒ Object

Create an ImageMagick stack in the command (surround.

Examples:

MiniMagick::Tool::Convert.new 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`

Yields:

  • (_self)

Yield Parameters:



181
182
183
184
185
# File 'lib/mini_magick/tool.rb', line 181

def stack
  self << "("
  yield self
  self << ")"
end