Class: JekyllOgImage::Element::Image

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

Instance Method Summary collapse

Constructor Details

#initialize(source, gravity: :nw, width:, height:, radius:) ⇒ Image

Returns a new instance of Image.



4
5
6
7
8
9
10
11
12
# File 'lib/jekyll_og_image/element/image.rb', line 4

def initialize(source, gravity: :nw, width:, height:, radius:)
  @gravity = gravity
  @source = source
  @width = width
  @height = height
  @radius = radius

  validate_gravity!
end

Instance Method Details

#apply_to(canvas, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jekyll_og_image/element/image.rb', line 14

def apply_to(canvas, &block)
  image = Vips::Image.new_from_buffer(@source, "")
  image = round_corners(image) if @radius

  if @width && @height
    ratio = calculate_ratio(image, @width, @height, :min)
    image = image.resize(ratio)
  end

  result = block.call(canvas, image) if block_given?

  x, y = result ? [ result.fetch(:x, 0), result.fetch(:y, 0) ] : [ 0, 0 ]

  composite_with_gravity(canvas, image, x, y)
end