Class: Jekyll::Gensocial::ImageCreator::Composer

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/jekyll-gensocial/image_creator/composer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_size:) ⇒ Composer

Returns a new instance of Composer.



12
13
14
15
# File 'lib/jekyll-gensocial/image_creator/composer.rb', line 12

def initialize(image_size:)
  @image_size = image_size
  @image = Image.new(image_size.width, image_size.height)
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



10
11
12
# File 'lib/jekyll-gensocial/image_creator/composer.rb', line 10

def image
  @image
end

Instance Method Details

#add_bg_layer(config:) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/jekyll-gensocial/image_creator/composer.rb', line 17

def add_bg_layer(config:)
  return if config.path.nil?

  bg_layer = Image.read(config.path)
    .first
    .resize_to_fill(@image_size.width, @image_size.height)

  @image.composite!(bg_layer, CenterGravity, OverCompositeOp)
end

#add_text_layer(text, config:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll-gensocial/image_creator/composer.rb', line 27

def add_text_layer(text, config:)
  origin = config.rect.origin
  size = config.rect.size

  text_layer = Magick::Image.read("caption:#{text}") do
    self.fill = config.fill
    self.font = config.font_path unless config.font_path.nil?
    self.pointsize = config.pointsize
    self.size = "#{size.width}x#{size.height}"
    self.background_color = "none"
  end.first

  @image.composite!(text_layer, NorthWestGravity, origin.x, origin.y, OverCompositeOp)
end