class MicroMagick::Image
Attributes
Public Class Methods
# 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
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
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
# File lib/micro_magick/image.rb, line 41 def corrupt? identify && @corrupt end
# File lib/micro_magick/image.rb, line 37 def height identify.height unless corrupt? end
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
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
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 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
# File lib/micro_magick/image.rb, line 33 def width identify.width unless corrupt? end
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
# 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
# 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
# File lib/micro_magick/image.rb, line 103 def method_missing(method, *args, &block) add_output_option("-#{method.to_s}", *args) end