class MicroMagick::Image

Attributes

input_file[R]

Public Class Methods

new(input_file) click to toggle source
# File lib/micro_magick/image.rb, line 8
def initialize(input_file)
  @input_file = input_file
  @input_options = []
  @output_options = []
  @identify = nil
end

Public Instance Methods

add_input_option(option_name, *args) click to toggle source

If you need to add an option that affects processing of input files, you can use this method.

# File lib/micro_magick/image.rb, line 17
def add_input_option(option_name, *args)
  (@input_options ||= []).push(option_name)
  args.each { |ea| @input_options.push(Shellwords.escape(ea.to_s)) }

  # Support call chaining:
  self
end
add_output_option(option_name, *args) click to toggle source

For normal options, like -resize or -flip, you can call .resize(“32x32”) or .flip(). If you need to add an output option that starts with a '+', you can use this method.

# File lib/micro_magick/image.rb, line 63
def add_output_option(option_name, *args)
  (@output_options ||= []).push(option_name)
  args.each { |ea| @output_options.push(Shellwords.escape(ea.to_s)) }

  # if we're a resize call, let's give the -size render hint to gm, but only when it's safe:
  # * we don't have input options yet,
  # * we're not cropping (because the -size will prevent the crop from working),
  # * and we have dimensions in the form of NNNxNNN
  if %w{-geometry -resize -sample -scale}.include?(option_name) &&
    @input_options.empty? &&
    !@output_options.include?('-crop')
    dimensions = args.first
    if dimensions.to_s =~ /\A(\d+x\d+)\z/
      @input_options.push('-size', dimensions)
    end
  end
  # Support call chaining:
  self
end
corrupt?() click to toggle source
# File lib/micro_magick/image.rb, line 41
def corrupt?
  identify && @corrupt
end
height() click to toggle source
# File lib/micro_magick/image.rb, line 37
def height
  identify.height unless corrupt?
end
ignore_checksum() click to toggle source

Ignore the checksum embedded in the image. Useful if the image is known to not be corrupted but has an invalid checksum; some devices export such broken images.

# File lib/micro_magick/image.rb, line 28
def ignore_checksum
  # Only PNG for now
  add_input_option("-define", "png:ignore-crc")
end
overwrite() click to toggle source

Runs “mogrify” See www.imagemagick.org/script/mogrify.php

# File lib/micro_magick/image.rb, line 94
def overwrite
  MicroMagick.exec(command('mogrify'))
ensure
  @input_options.clear
  @output_options.clear
end
square_crop(gravity = 'Center') click to toggle source

Crop to a square, using the specified gravity.

# File lib/micro_magick/image.rb, line 55
def square_crop(gravity = 'Center')
  gravity(gravity) unless gravity.nil?
  d = [width, height].min
  crop("#{d}x#{d}+0+0!")
end
strip() click to toggle source

Strip the image of any profiles or comments. Note that this re-encodes the image, so it should only be used when downsampling (say, for a thumbnail) (ImageMagick has the -strip command, but GraphicsMagick doesn't. It turns out that “`+profile *“` does the same thing.)

# File lib/micro_magick/image.rb, line 50
def strip
  add_output_option('+profile', '*')
end
width() click to toggle source
# File lib/micro_magick/image.rb, line 33
def width
  identify.width unless corrupt?
end
write(output_file) click to toggle source

Runs “convert” See www.imagemagick.org/script/convert.php

# File lib/micro_magick/image.rb, line 85
def write(output_file)
  MicroMagick.exec(command('convert', output_file))
ensure
  @input_options.clear
  @output_options.clear
end

Private Instance Methods

command(command_name, output_file = nil) click to toggle source
# File lib/micro_magick/image.rb, line 107
def command(command_name, output_file = nil)
  cmd = [command_name]
  cmd.push(*@input_options)
  cmd.push(Shellwords.escape(@input_file))
  cmd.push(*@output_options)
  cmd.push(Shellwords.escape(output_file)) if output_file
  cmd
end
identify() click to toggle source
# File lib/micro_magick/image.rb, line 116
def identify
  @identify || begin
    cmd = ['identify', '-verbose', '-format', '%wx%h', Shellwords.escape(input_file)]
    @identify = IdentifyParser.new(MicroMagick.exec(cmd, true))
    @corrupt = false
  rescue CorruptImageError
    @identify = {}
    @corrupt = true
  end
  @identify
end
method_missing(method, *args, &block) click to toggle source
# File lib/micro_magick/image.rb, line 103
def method_missing(method, *args, &block)
  add_output_option("-#{method.to_s}", *args)
end