Module: MiniMagick::Configuration

Included in:
MiniMagick
Defined in:
lib/mini_magick/configuration.rb

Constant Summary collapse

CLI_DETECTION =
{
  imagemagick:    "mogrify",
  graphicsmagick: "gm",
  imagemagick7:   "magick",
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cliSymbol

Get [ImageMagick](www.imagemagick.org) or [GraphicsMagick](www.graphicsmagick.org).

Returns:

  • (Symbol)

    ‘:imagemagick` or `:graphicsmagick`



13
14
15
# File 'lib/mini_magick/configuration.rb', line 13

def cli
  @cli
end

#cli_pathString

If you set the path of CLI tools, you can get the path of the executables.

Returns:

  • (String)


173
174
175
176
177
178
179
180
181
# File 'lib/mini_magick/configuration.rb', line 173

def cli_path
  if instance_variable_defined?("@cli_path")
    instance_variable_get("@cli_path")
  else
    processor_path = instance_variable_get("@processor_path") if instance_variable_defined?("@processor_path")

    instance_variable_set("@cli_path", processor_path)
  end
end

#cli_prefixString+

Adds a prefix to the CLI command. For example, you could use ‘firejail` to run all commands in a sandbox. Can be a string, or an array of strings. e.g. ’firejail’, or [‘firejail’, ‘–force’]

Returns:

  • (String)
  • (Array<String>)


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

def cli_prefix
  @cli_prefix
end

#debugBoolean

When get to ‘true`, it outputs each command to STDOUT in their shell version.

Returns:

  • (Boolean)


47
48
49
# File 'lib/mini_magick/configuration.rb', line 47

def debug
  @debug
end

#loggerLogger

Logger for #debug, default is ‘MiniMagick::Logger.new(STDOUT)`, but you can override it, for example if you want the logs to be written to a file.

Returns:

  • (Logger)


55
56
57
# File 'lib/mini_magick/configuration.rb', line 55

def logger
  @logger
end

#processor_pathObject



21
22
23
# File 'lib/mini_magick/configuration.rb', line 21

def processor_path
  @processor_path
end

#shell_apiString

Instructs MiniMagick how to execute the shell commands. Available APIs are “open3” (default) and “posix-spawn” (requires the “posix-spawn” gem).

Returns:

  • (String)


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

def shell_api
  @shell_api
end

#timeoutInteger

If you don’t want commands to take too long, you can set a timeout (in seconds).

Returns:

  • (Integer)


40
41
42
# File 'lib/mini_magick/configuration.rb', line 40

def timeout
  @timeout
end

#validate_on_createBoolean

If set to ‘true`, it will `identify` every newly created image, and raise `MiniMagick::Invalid` if the image is not valid. Useful for validating user input, although it adds a bit of overhead. Defaults to `true`.

Returns:

  • (Boolean)


64
65
66
# File 'lib/mini_magick/configuration.rb', line 64

def validate_on_create
  @validate_on_create
end

#validate_on_writeBoolean

If set to ‘true`, it will `identify` every image that gets written (with Image#write), and raise `MiniMagick::Invalid` if the image is not valid. Useful for validating that processing was sucessful, although it adds a bit of overhead. Defaults to `true`.

Returns:

  • (Boolean)


73
74
75
# File 'lib/mini_magick/configuration.rb', line 73

def validate_on_write
  @validate_on_write
end

#whinyBoolean

If set to ‘false`, it will not raise errors when ImageMagick returns status code different than 0. Defaults to `true`.

Returns:

  • (Boolean)


81
82
83
# File 'lib/mini_magick/configuration.rb', line 81

def whiny
  @whiny
end

Class Method Details

.extended(base) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/mini_magick/configuration.rb', line 92

def self.extended(base)
  base.validate_on_create = true
  base.validate_on_write = true
  base.whiny = true
  base.shell_api = "open3"
  base.logger = Logger.new($stdout).tap { |l| l.level = Logger::INFO }
end

Instance Method Details

#configure {|self| ... } ⇒ Object

Examples:

MiniMagick.configure do |config|
  config.cli = :graphicsmagick
  config.timeout = 5
end

Yields:

  • (self)


108
109
110
# File 'lib/mini_magick/configuration.rb', line 108

def configure
  yield self
end

#processorObject



119
120
121
122
123
# File 'lib/mini_magick/configuration.rb', line 119

def processor
  @processor ||= CLI_DETECTION.values.detect do |processor|
    MiniMagick::Utilities.which(processor)
  end
end

#processor=(processor) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/mini_magick/configuration.rb', line 126

def processor=(processor)
  @processor = processor.to_s

  unless CLI_DETECTION.value?(@processor)
    raise ArgumentError,
      "processor has to be set to either \"magick\", \"mogrify\" or \"gm\"" \
      ", was set to #{@processor.inspect}"
  end
end

#reload_toolsObject

Backwards compatibility



193
194
195
# File 'lib/mini_magick/configuration.rb', line 193

def reload_tools
  warn "MiniMagick.reload_tools is deprecated because it is no longer necessary"
end