Class: MicroMagick::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/micro_magick/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file) ⇒ Image



8
9
10
11
12
13
# File 'lib/micro_magick/image.rb', line 8

def initialize(input_file)
  @input_file = input_file
  @input_options = []
  @output_options = []
  @identify = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



103
104
105
# File 'lib/micro_magick/image.rb', line 103

def method_missing(method, *args, &block)
  add_output_option("-#{method.to_s}", *args)
end

Instance Attribute Details

#input_fileObject (readonly)

Returns the value of attribute input_file.



6
7
8
# File 'lib/micro_magick/image.rb', line 6

def input_file
  @input_file
end

Instance Method Details

#add_input_option(option_name, *args) ⇒ Object

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



17
18
19
20
21
22
23
# 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) ⇒ Object

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.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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?Boolean



41
42
43
# File 'lib/micro_magick/image.rb', line 41

def corrupt?
  identify && @corrupt
end

#heightObject



37
38
39
# File 'lib/micro_magick/image.rb', line 37

def height
  identify.height unless corrupt?
end

#ignore_checksumObject

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.



28
29
30
31
# File 'lib/micro_magick/image.rb', line 28

def ignore_checksum
  # Only PNG for now
  add_input_option("-define", "png:ignore-crc")
end

#overwriteObject



94
95
96
97
98
99
# 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') ⇒ Object

Crop to a square, using the specified gravity.



55
56
57
58
59
# 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

#stripObject

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.)



50
51
52
# File 'lib/micro_magick/image.rb', line 50

def strip
  add_output_option('+profile', '*')
end

#widthObject



33
34
35
# File 'lib/micro_magick/image.rb', line 33

def width
  identify.width unless corrupt?
end

#write(output_file) ⇒ Object



85
86
87
88
89
90
# 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