jekyll-og-image 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eeccdc0ae99d37ef260733506fbad4ebf5c60f925cdb2514acb91dcb346d1673
4
- data.tar.gz: b8b291f97eddc6a0be9dc080d2f999be0bb027224bf5bf2953f941a89491b63a
3
+ metadata.gz: 9fc4622681cf9b4953d6cbd7de3b661ebd217de44c08205656e395386b14e4c9
4
+ data.tar.gz: 26b864c6981a1b353de1e8e97de690870e2f2d9c531d56dac1bf97bc2efc0cde
5
5
  SHA512:
6
- metadata.gz: b15a8205247be628cf982c6efb8f9127cd0ba2eb1d2421e7f7f702802e4960a14317612cfffa010d41972e1b2ba2671eae82f19092552f93b71003995096a21a
7
- data.tar.gz: 47b2a2845a7e6bd2a25ef2f249af3fcd1e78adf9179366680e173fc0228aaa5e564be2f5935a0752b3c2c5dd8061b6b17959d7f592fa50ca08d54006baca272c
6
+ metadata.gz: 7285bbd3562d0e2fa67a1f92c639d2ffa505e73d0d6b58c705217e8e7eefbb5930b9c01fec2c5d267f3448cd012e76ed5f2748b639d62bde290d796ab87d31d8
7
+ data.tar.gz: beb92471fe217bc7c105fb3924940fee58031306cf628aac36ff8a4b61841ebaee406c8a85ce6aa903563fe3ca8dd489d68d0b3018f9797e77cb76da209d08ef
data/examples/1.png CHANGED
Binary file
data/examples/2.png CHANGED
Binary file
@@ -21,4 +21,20 @@ class JekyllOgImage::Element::Base
21
21
  raise ArgumentError, "Invalid gravity: #{@gravity.inspect}"
22
22
  end
23
23
  end
24
+
25
+ def composite_with_gravity(canvas, overlay, x, y)
26
+ if gravity_nw?
27
+ canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
28
+ elsif gravity_ne?
29
+ x = canvas.width - overlay.width - x
30
+ canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
31
+ elsif gravity_sw?
32
+ y = canvas.height - overlay.height - y
33
+ canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
34
+ elsif gravity_se?
35
+ x = canvas.width - overlay.width - x
36
+ y = canvas.height - overlay.height - y
37
+ canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
38
+ end
39
+ end
24
40
  end
@@ -4,43 +4,57 @@ class JekyllOgImage::Element::Border < JekyllOgImage::Element::Base
4
4
  class Part < Data.define(:rgb, :width, :height, :offset)
5
5
  end
6
6
 
7
- def initialize(canvas, size, position: :bottom, fill: "#000000")
8
- @canvas = canvas
7
+ def initialize(size, position: :bottom, fill: "#000000")
9
8
  @size = size
10
9
  @position = position
11
10
  @fill = fill
12
11
 
13
12
  validate_position!
13
+ end
14
14
 
15
- @border = Vips::Image.black(*dimensions)
15
+ def apply_to(canvas, &block)
16
+ border = Vips::Image.black(*dimensions(canvas))
16
17
 
17
- parts.each.with_index do |part, index|
18
- border = Vips::Image.black(part.width, part.height).ifthenelse([ 0, 0, 0 ], part.rgb)
18
+ parts(canvas).each.with_index do |part, index|
19
+ image = Vips::Image.black(part.width, part.height).ifthenelse([ 0, 0, 0 ], part.rgb)
19
20
 
20
21
  if vertical?
21
- @border = @border.composite(border, :over, x: [ 0 ], y: [ part.offset ]).flatten
22
+ border = border.composite(image, :over, x: [ 0 ], y: [ part.offset ]).flatten
22
23
  else
23
- @border = @border.composite(border, :over, x: [ part.offset ], y: [ 0 ]).flatten
24
+ border = border.composite(image, :over, x: [ part.offset ], y: [ 0 ]).flatten
24
25
  end
25
26
  end
26
- end
27
27
 
28
- def apply(&block)
29
- result = block.call(@canvas, @border) if block_given?
28
+ result = block.call(canvas, border) if block_given?
29
+
30
30
  x, y = result ? [ result.fetch(:x, 0), result.fetch(:y, 0) ] : [ 0, 0 ]
31
31
 
32
32
  if vertical?
33
- x = @position == :left ? x : @canvas.width - @size - x
34
- @canvas.composite(@border, :over, x: [ x ], y: [ 0 ]).flatten
33
+ x = @position == :left ? x : canvas.width - @size - x
34
+ canvas.composite(border, :over, x: [ x ], y: [ 0 ]).flatten
35
35
  else
36
- y = @position == :top ? y : @canvas.height - @size - y
37
- @canvas.composite(@border, :over, x: [ 0 ], y: [ y ]).flatten
36
+ y = @position == :top ? y : canvas.height - @size - y
37
+ canvas.composite(border, :over, x: [ 0 ], y: [ y ]).flatten
38
38
  end
39
39
  end
40
40
 
41
- def parts
41
+ private
42
+
43
+ def hex_to_rgb(hex)
44
+ hex.match(/#(..)(..)(..)/)[1..3].map { |x| x.hex }
45
+ end
46
+
47
+ def dimensions(canvas)
48
+ if vertical?
49
+ [ @size, canvas.height ]
50
+ else
51
+ [ canvas.width, @size ]
52
+ end
53
+ end
54
+
55
+ def parts(canvas)
42
56
  if @fill.is_a?(Array)
43
- width, height = vertical? ? [ @size, (@canvas.height / @fill.size) ] : [ (@canvas.width / @fill.size), @size ]
57
+ width, height = vertical? ? [ @size, (canvas.height / @fill.size) ] : [ (canvas.width / @fill.size), @size ]
44
58
 
45
59
  @fill.map.with_index do |item, index|
46
60
  Part.new(
@@ -51,26 +65,12 @@ class JekyllOgImage::Element::Border < JekyllOgImage::Element::Base
51
65
  )
52
66
  end
53
67
  else
54
- length = vertical? ? @canvas.height : @canvas.width
68
+ length = vertical? ? canvas.height : canvas.width
55
69
 
56
70
  [ Part.new(rgb: hex_to_rgb(@fill), length: length, offset: 0) ]
57
71
  end
58
72
  end
59
73
 
60
- private
61
-
62
- def hex_to_rgb(hex)
63
- hex.match(/#(..)(..)(..)/)[1..3].map { |x| x.hex }
64
- end
65
-
66
- def dimensions
67
- if vertical?
68
- [ @size, @canvas.height ]
69
- else
70
- [ @canvas.width, @size ]
71
- end
72
- end
73
-
74
74
  def vertical?
75
75
  @position == :left || @position == :right
76
76
  end
@@ -8,31 +8,22 @@ class JekyllOgImage::Element::Canvas < JekyllOgImage::Element::Base
8
8
  end
9
9
 
10
10
  def image(source, **opts, &block)
11
- image = JekyllOgImage::Element::Image.new(
12
- @canvas, source, **opts
13
- )
14
-
15
- @canvas = image.apply(&block)
11
+ image = JekyllOgImage::Element::Image.new(source, **opts)
12
+ @canvas = image.apply_to(@canvas, &block)
16
13
 
17
14
  self
18
15
  end
19
16
 
20
17
  def text(message, **opts, &block)
21
- text = JekyllOgImage::Element::Text.new(
22
- @canvas, message, **opts
23
- )
24
-
25
- @canvas = text.apply(&block)
18
+ text = JekyllOgImage::Element::Text.new(message, **opts)
19
+ @canvas = text.apply_to(@canvas, &block)
26
20
 
27
21
  self
28
22
  end
29
23
 
30
- def border(width, position: :bottom, fill: "#000000")
31
- @canvas = JekyllOgImage::Element::Border.new(
32
- @canvas, width,
33
- position: position,
34
- fill: fill
35
- ).apply
24
+ def border(width, **opts, &block)
25
+ border = JekyllOgImage::Element::Border.new(width, **opts)
26
+ @canvas = border.apply_to(@canvas, &block)
36
27
 
37
28
  self
38
29
  end
@@ -1,19 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class JekyllOgImage::Element::Image < JekyllOgImage::Element::Base
4
- def initialize(canvas, source, gravity: :nw, width:, height:)
5
- @canvas = canvas
4
+ def initialize(source, gravity: :nw, width:, height:, radius:)
6
5
  @gravity = gravity
7
6
  @source = source
8
7
  @width = width
9
8
  @height = height
9
+ @radius = radius
10
10
 
11
11
  validate_gravity!
12
12
  end
13
13
 
14
- def apply(&block)
14
+ def apply_to(canvas, &block)
15
15
  image = Vips::Image.new_from_buffer(@source, "")
16
- result = block.call(@canvas, image) if block_given?
16
+ image = round_corners(image) if @radius
17
+
18
+ result = block.call(canvas, image) if block_given?
17
19
 
18
20
  if @width && @height
19
21
  ratio = calculate_ratio(image, @width, @height, :min)
@@ -22,19 +24,7 @@ class JekyllOgImage::Element::Image < JekyllOgImage::Element::Base
22
24
 
23
25
  x, y = result ? [ result.fetch(:x, 0), result.fetch(:y, 0) ] : [ 0, 0 ]
24
26
 
25
- if gravity_nw?
26
- @canvas.composite(image, :over, x: [ x ], y: [ y ]).flatten
27
- elsif gravity_ne?
28
- x = @canvas.width - image.width - x
29
- @canvas.composite(image, :over, x: [ x ], y: [ y ]).flatten
30
- elsif gravity_sw?
31
- y = @canvas.height - image.height - y
32
- @canvas.composite(image, :over, x: [ x ], y: [ y ]).flatten
33
- elsif gravity_se?
34
- x = @canvas.width - image.width - x
35
- y = @canvas.height - image.height - y
36
- @canvas.composite(image, :over, x: [ x ], y: [ y ]).flatten
37
- end
27
+ composite_with_gravity(canvas, image, x, y)
38
28
  end
39
29
 
40
30
  private
@@ -45,6 +35,25 @@ class JekyllOgImage::Element::Image < JekyllOgImage::Element::Base
45
35
  end
46
36
  end
47
37
 
38
+ def round_corners(image)
39
+ mask = %(
40
+ <svg viewBox="0 0 #{image.width} #{image.height}">
41
+ <rect
42
+ rx="#{@radius}"
43
+ ry="#{@radius}"
44
+ x="0"
45
+ y="0"
46
+ width="#{image.width}"
47
+ height="#{image.height}"
48
+ fill="#fff"
49
+ />
50
+ </svg>
51
+ )
52
+
53
+ mask = Vips::Image.new_from_buffer(mask, "")
54
+ image.bandjoin mask[3]
55
+ end
56
+
48
57
  def calculate_ratio(image, width, height, mode)
49
58
  if mode == :min
50
59
  [ width.to_f / image.width, height.to_f / image.height ].min
@@ -1,43 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class JekyllOgImage::Element::Text < JekyllOgImage::Element::Base
4
- def initialize(canvas, message, gravity: :nw, width: nil, dpi: nil, color: "#000000", font: nil)
5
- @canvas = canvas
4
+ def initialize(message, gravity: :nw, width: nil, dpi: nil, color: "#000000", font: nil)
5
+ @message = message
6
6
  @gravity = gravity
7
+ @width = width
8
+ @dpi = dpi
9
+ @color = color
10
+ @font = font
7
11
 
8
12
  validate_gravity!
13
+ end
9
14
 
10
- text = Vips::Image.text(message,
11
- font: font,
12
- width: width,
13
- dpi: dpi,
15
+ def apply_to(canvas, &block)
16
+ text = Vips::Image.text(@message,
17
+ font: @font,
18
+ width: @width,
19
+ dpi: @dpi,
14
20
  wrap: :word,
15
21
  align: :low
16
22
  )
17
23
 
18
- @text = text
19
- .new_from_image(hex_to_rgb(color))
24
+ text = text
25
+ .new_from_image(hex_to_rgb(@color))
20
26
  .copy(interpretation: :srgb)
21
27
  .bandjoin(text)
22
- end
23
28
 
24
- def apply(&block)
25
- result = block.call(@canvas, @text) if block_given?
29
+ result = block.call(canvas, text) if block_given?
30
+
26
31
  x, y = result ? [ result.fetch(:x, 0), result.fetch(:y, 0) ] : [ 0, 0 ]
27
32
 
28
- if gravity_nw?
29
- @canvas.composite(@text, :over, x: [ x ], y: [ y ]).flatten
30
- elsif gravity_ne?
31
- x = @canvas.width - @text.width - x
32
- @canvas.composite(@text, :over, x: [ x ], y: [ y ]).flatten
33
- elsif gravity_sw?
34
- y = @canvas.height - @text.height - y
35
- @canvas.composite(@text, :over, x: [ x ], y: [ y ]).flatten
36
- elsif gravity_se?
37
- x = @canvas.width - @text.width - x
38
- y = @canvas.height - @text.height - y
39
- @canvas.composite(@text, :over, x: [ x ], y: [ y ]).flatten
40
- end
33
+ composite_with_gravity(canvas, text, x, y)
41
34
  end
42
35
 
43
36
  private
@@ -46,7 +46,8 @@ class JekyllOgImage::Generator < Jekyll::Generator
46
46
  File.read(File.join(site.config["source"], JekyllOgImage.config.image)),
47
47
  gravity: :ne,
48
48
  width: 150,
49
- height: 150
49
+ height: 150,
50
+ radius: 50
50
51
  ) { |_canvas, _text| { x: 80, y: 100 } }
51
52
  end
52
53
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllOgImage
4
- VERSION = "1.0.4"
4
+ VERSION = "1.0.5"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-og-image
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Alexandrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-05 00:00:00.000000000 Z
11
+ date: 2024-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll-seo-tag