Class: Udongo::ImageManipulation::ResizeToFill

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/udongo/image_manipulation/resize_to_fill.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 aspect ratio of the original image. If necessary, crop the image in the larger dimension.

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/udongo/image_manipulation/resize_to_fill.rb', line 15

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

  img = MiniMagick::Image.open(@file)
  cols, rows = img[:dimensions]

  img.combine_options do |cmd|
    if @width != cols || @height != rows
      scale_x = @width/cols.to_f
      scale_y = @height/rows.to_f

      if scale_x >= scale_y
        cols = (scale_x * (cols + 0.5)).round
        rows = (scale_x * (rows + 0.5)).round
        cmd.resize "#{cols}"
      else
        cols = (scale_y * (cols + 0.5)).round
        rows = (scale_y * (rows + 0.5)).round
        cmd.resize "x#{rows}"
      end
    end

    cmd.quality @options[:quality] if @options.key?(:quality)
    cmd.gravity gravity
    cmd.background 'rgba(255,255,255,0.0)'
    cmd.extent "#{@width}x#{@height}" if cols != @width || rows != @height
  end

  img.write(path)
end