Class: JekyllOgImage::Element::Canvas

Inherits:
Base
  • Object
show all
Defined in:
lib/jekyll_og_image/element/canvas.rb

Instance Method Summary collapse

Constructor Details

#initialize(width, height, background_color: "#ffffff", background_image: nil) ⇒ Canvas

Returns a new instance of Canvas.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jekyll_og_image/element/canvas.rb', line 6

def initialize(width, height, background_color: "#ffffff", background_image: nil)
  @canvas = Vips::Image.black(width, height).ifthenelse([ 0, 0, 0 ], hex_to_rgb(background_color))

  if background_image
    overlay = Vips::Image.new_from_buffer(background_image, "")

    ratio = calculate_ratio(overlay, width, height, :max)
    overlay = overlay.resize(ratio)

    @canvas = @canvas.composite(overlay, :over, x: [ 0 ], y: [ 0 ]).flatten
  end

  @canvas
end

Instance Method Details

#border(width, **opts, &block) ⇒ Object



35
36
37
38
39
40
# File 'lib/jekyll_og_image/element/canvas.rb', line 35

def border(width, **opts, &block)
  border = JekyllOgImage::Element::Border.new(width, **opts)
  @canvas = border.apply_to(@canvas, &block)

  self
end

#image(source, **opts, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/jekyll_og_image/element/canvas.rb', line 21

def image(source, **opts, &block)
  image = JekyllOgImage::Element::Image.new(source, **opts)
  @canvas = image.apply_to(@canvas, &block)

  self
end

#save(filename) ⇒ Object



42
43
44
# File 'lib/jekyll_og_image/element/canvas.rb', line 42

def save(filename)
  @canvas.write_to_file(filename)
end

#text(message, **opts, &block) ⇒ Object



28
29
30
31
32
33
# File 'lib/jekyll_og_image/element/canvas.rb', line 28

def text(message, **opts, &block)
  text = JekyllOgImage::Element::Text.new(message, **opts)
  @canvas = text.apply_to(@canvas, &block)

  self
end