Module: CommandWrap::Image

Defined in:
lib/command_wrap/image.rb

Class Method Summary collapse

Class Method Details

.dimensions(path) ⇒ Object



7
8
9
10
# File 'lib/command_wrap/image.rb', line 7

def self.dimensions (path)
    img = Magick::Image.read(path)[0]
    { :width => img.columns, :height => img.rows }
end

.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