class GraphicsMagick::Image

Attributes

file[R]

Public Class Methods

new(input) click to toggle source
# File lib/graphicsmagick/image.rb, line 14
def initialize(input)
  @command_options = []
  @utility = nil

  @file = parse_input(input)
end

Public Instance Methods

path() click to toggle source

Returns the path of the associated file

# File lib/graphicsmagick/image.rb, line 22
def path
  @file.path
end
write(output, opts = {}) click to toggle source

Writes the changes to the file specified in output. Set +:timeout => 30.seconds+ to change the timeout value. Default is one minute.

# File lib/graphicsmagick/image.rb, line 29
def write(output, opts = {})
  output_path = parse_input(output, false)

  FileUtils.copy_file(path, output_path) unless requires_output_file?

  command = build_command(output_path)
  run(command, opts)
  GraphicsMagick::Image.new(output_path)
end
write!(opts = {}) click to toggle source

Writes the changes to the current file. Set +:timeout => 30.seconds+ to change the timeout value. Default is one minute.

# File lib/graphicsmagick/image.rb, line 42
def write!(opts = {})
  if requires_output_file?
    raise NoMethodError, "You cannot use Image#write(output) with "\
                         "the #{current_utility} command"
  end

  command = build_command(path)
  run(command, opts)
  self
end

Protected Instance Methods

method_missing(method, *args, &block) click to toggle source
# File lib/graphicsmagick/image.rb, line 55
def method_missing(method, *args, &block)
  add_option("-#{method.to_s.gsub(/_/, '-')}", *args)
end

Private Instance Methods

add_option(option_name, *args) click to toggle source
# File lib/graphicsmagick/image.rb, line 61
def add_option(option_name, *args)
  @command_options << {
    name: option_name,
    args: args.collect { |a| Shellwords.escape(a.to_s) }.join(' ')
  }
  self
end
parse_input(input, output_as_file = true) click to toggle source
# File lib/graphicsmagick/image.rb, line 82
def parse_input(input, output_as_file = true)
  if input.is_a?(String)
    output_as_file ? File.new(input) : input
  elsif input.is_a?(File) || input.is_a?(Tempfile)
    output_as_file ? input : input.path
  else
    raise TypeError,
          'You must specify a file as a String, File, or Tempfile object'
  end
end
run(command, opts = {}) click to toggle source
# File lib/graphicsmagick/image.rb, line 69
def run(command, opts = {})
  opts = { timeout: 60 }.merge(opts)
  command = "gm #{command}"
  cmd = Subexec.run(command, opts)

  if cmd.exitstatus != 0
    raise UnknownOptionError, "#{command} failed: #{cmd.output}"
  end
ensure
  @command_options = []
  @utility = nil
end