Method: CommandWrap::Image.scale

Defined in:
lib/command_wrap/image.rb

.scale(source, target, width, height = nil) ⇒ Object



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/command_wrap/image.rb', line 12

def self.scale (source, target, width, height = nil)
    # if no height, we calculate it based on the width
    unless height
        dim = self.dimensions(source)
        height = (1.0 * width * dim[:height] / dim[:width]).floor
    end

    # Scale source
    simg = Magick::Image.read(source)[0]

    if simg.columns > width && simg.rows > height
        simg.resize_to_fit!(width, height)
    end
                
    # Create transparent image
    timg = Magick::Image.new(width, height)
    d = Magick::Draw.new
    d.fill('white')
    d.draw(timg)
    timg = timg.transparent('white')

    # Insert thumb
    timg.composite!(simg, Magick::CenterGravity, Magick::OverCompositeOp)

    # Save result
    timg.write(target)
end