Class: Udongo::ImageManipulation::ResizeAndPad

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/udongo/image_manipulation/resize_and_pad.rb

Instance Method Summary collapse

Methods included from Base

#initialize

Instance Method Details

#resize(path) ⇒ Object

Resize the image to fit within the specified dimensions while retaining the original aspect ratio. If necessary, will pad the remaining area with the given color, which defaults to transparent (for gif and png, white for jpeg).

Possible values for options are: NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/udongo/image_manipulation/resize_and_pad.rb', line 16

def resize(path)
  gravity = @options.key?(:gravity) ? @options[:gravity] : 'Center'
  background = @options.key?(:background) ? @options[:background] : :transparant

  img = MiniMagick::Image.open(@file)
  img.combine_options do |cmd|
    cmd.thumbnail "#{@width}x#{@height}>"

    if background.to_sym == :transparent
      cmd.background 'rgba(255, 255, 255, 0.0)'
    else
      cmd.background background
    end

    cmd.gravity gravity
    cmd.extent "#{@width}x#{@height}"
  end

  img.write(path)
end