Class: Acbaker::Processors::Center

Inherits:
Base
  • Object
show all
Defined in:
lib/acbaker/processors/center.rb

Constant Summary collapse

@@defaults =
{"max-width" => "50%", "background-color" => "#FFFFFF", "gravity" => "Center"}

Instance Attribute Summary

Attributes inherited from Base

#asset_pack, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Acbaker::Processors::Base

Instance Method Details

#run(image, image_spec, width = nil, height = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/acbaker/processors/center.rb', line 6

def run(image, image_spec, width=nil, height=nil)
  transform_width = width.to_f
  transform_height = height.to_f
  target_ratio = transform_width / transform_height
  image_ratio = image.columns / image.rows
  
  # calculate dimensions
  if @options['max-width']
    if @options['max-width'][-1] == "%"
      rel_width = (@options['max-width'][0..-2].to_f / 100)
      transform_width = transform_width * rel_width
    else
      transform_width = @options['max-width'].to_f
    end
    transform_height = transform_width * image_ratio
  end
  
  # resize image
  image.resize!(transform_width.to_i, transform_height.to_i)
  
  # create canvas
  canvas = Magick::Image.new(width, height)
  if @options['background-color'] == :transparent
    canvas = canvas.matte_floodfill(1, 1)
  else
    canvas = canvas.color_floodfill(1, 1, Magick::Pixel.from_color(@options['background-color']))
  end
  
  # place image
  image = canvas.composite(image, Magick::CenterGravity, Magick::OverCompositeOp)
  
  image
end