class SimpleMagick::Image

Convert Image Class. @example Usage

image = SimpleMagick::Image.new('/path/to/org.jpg')
image.resize '150x'
image.convert!('/path/to/thumb.jpg')

Public Class Methods

new(source_path, verbose = false) click to toggle source

@param [String] source_path Target Image File Path @param [Boolean] verbose print detailed information

# File lib/simple_magick.rb, line 27
def initialize(source_path, verbose = false)
  @source_path = source_path
  @verbose     = verbose
  @command     = [ImageMagick::EXEC]
end

Public Instance Methods

additional_option(option, value = '') click to toggle source

use options. @param [String] option option name @param [String] value option value. default = ''

# File lib/simple_magick.rb, line 42
def additional_option(option, value = '')
  value = value.strip
  @command << %Q(-#{option}).strip
  @command.last << %Q( "#{value}") unless value.empty?
  @command
end
convert!(destination_path) click to toggle source

run ImageMagick. @param [String] destination_path output image path @return [String] execute command @raise [SimpleMagick::ConvertError] mogrify command fail.

# File lib/simple_magick.rb, line 53
def convert!(destination_path)
  file_copy(destination_path)
  command = create_command(@command, destination_path)

  # ex. % mogrify -resize 90 /path/to/resize_image.jpg
  begin
    puts "[INFO] #{command}" if @verbose
    stdout, stderr, status = Open3.capture3(command)
  rescue SystemCallError => e
    raise ConvertError.new("#{ImageMagick::EXEC} error. exec command => [#{command}], Error Detail => [#{e.message}]")
  end

  unless status.success?
    raise ConvertError.new("#{ImageMagick::EXEC} error. exec command => [#{command}], stdout => [#{stdout}], stderr => [#{stderr}]")
  end

  command
end
method_missing(method, *args) click to toggle source

set option

Calls superclass method
# File lib/simple_magick.rb, line 34
def method_missing(method, *args)
  super unless ImageMagick::OPTIONS.include? method.to_s
  __send__(:additional_option, method.to_s, args[0])
end

Private Instance Methods

create_command(command, destination_path) click to toggle source

create exec command line @param [Array] command mogrify command @param [String] destination_path create image path @return [String] command String.

# File lib/simple_magick.rb, line 93
def create_command(command, destination_path)
  if Utility.windows?
    command << destination_path
  else
    command << Shellwords.escape(destination_path)
  end
  command.join(' ')
end
file_copy(destination_path) click to toggle source

copy source_path to destination_path @param [String] destination_path @return [Void] @raise [SimpleMagick::ConvertError] @source_path not found.

# File lib/simple_magick.rb, line 78
def file_copy(destination_path)
  unless File.file? @source_path
    raise ConvertError.new("Not Found input file. input file => [#{@source_path}]")
  end

  dest_dir = File.dirname(destination_path)
  FileUtils.mkdir_p(dest_dir)
  puts "[INFO] cp #{@source_path} #{destination_path}" if @verbose
  FileUtils.cp(@source_path, destination_path)
end