jekyll-og-image 1.0.3 → 1.0.4

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: e885b900b05e7a8b46a221a0850e682815081837e102044dfa02141d5c86b41c
4
- data.tar.gz: 7665d1a4ce49f9a422d1823513f1c6a5cc4a0bbb7e532904288ad3777e95129d
3
+ metadata.gz: eeccdc0ae99d37ef260733506fbad4ebf5c60f925cdb2514acb91dcb346d1673
4
+ data.tar.gz: b8b291f97eddc6a0be9dc080d2f999be0bb027224bf5bf2953f941a89491b63a
5
5
  SHA512:
6
- metadata.gz: abf1c34206e4f72af335710c9c2d3178039057208b8a123d640e2a270bb48e19c1a205c8ce41e18a0bc2b5d58b2c8bc33a37a5052d706d44d9b6035548a0e62e
7
- data.tar.gz: e08fb4973bacb416395934631f63b8d4e293fa73d6e7a360b1126d4403851e73084aa6443ab979b1070a090faebb4919a27d2d61c0461fd156f3e82a28a5179f
6
+ metadata.gz: b15a8205247be628cf982c6efb8f9127cd0ba2eb1d2421e7f7f702802e4960a14317612cfffa010d41972e1b2ba2671eae82f19092552f93b71003995096a21a
7
+ data.tar.gz: 47b2a2845a7e6bd2a25ef2f249af3fcd1e78adf9179366680e173fc0228aaa5e564be2f5935a0752b3c2c5dd8061b6b17959d7f592fa50ca08d54006baca272c
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  A Jekyll plugin to automatically generate open graph images for posts.
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/jekyll-og-image.svg)](https://badge.fury.io/rb/jekyll-og-image)
5
6
  [![Lint](https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/lint.yml/badge.svg?branch=main)](https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/lint.yml)
6
7
  [![Specs](https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/specs.yml/badge.svg?branch=main)](https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/specs.yml)
7
8
 
@@ -46,6 +47,7 @@ og_image:
46
47
  ```yaml
47
48
  og_image:
48
49
  output_dir: "assets/images/og"
50
+ image: "/assets/images/igor.jpeg"
49
51
  domain: "igor.works"
50
52
  border_bottom:
51
53
  width: 20
data/examples/1.png CHANGED
Binary file
@@ -5,12 +5,15 @@ require "anyway_config"
5
5
  class JekyllOgImage::Config < Anyway::Config
6
6
  attr_config output_dir: "assets/images/og"
7
7
  attr_config force: false
8
+ attr_config :image
8
9
  attr_config :domain
9
10
  attr_config border_bottom: {
10
11
  width: 20,
11
12
  fill: [ "#211F1F", "#F4CBB2", "#AD5C51", "#9CDAF1", "#7DBBE6" ]
12
13
  }
13
14
 
15
+ coerce_types image: { type: :string }
16
+
14
17
  coerce_types border_bottom: {
15
18
  width: { type: :integer },
16
19
  fill: { type: :string, array: true }
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class JekyllOgImage::Element::Base
4
+ VALID_GRAVITY = %i[nw ne sw se].freeze
5
+
4
6
  private
5
7
 
6
8
  def hex_to_rgb(input)
@@ -13,4 +15,10 @@ class JekyllOgImage::Element::Base
13
15
  raise ArgumentError, "Unknown input #{input.inspect}"
14
16
  end
15
17
  end
18
+
19
+ def validate_gravity!
20
+ unless VALID_GRAVITY.include?(@gravity)
21
+ raise ArgumentError, "Invalid gravity: #{@gravity.inspect}"
22
+ end
23
+ end
16
24
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "vips"
4
+
5
+ class JekyllOgImage::Element::Canvas < JekyllOgImage::Element::Base
6
+ def initialize(width, height, color: "#ffffff")
7
+ @canvas = Vips::Image.black(width, height).ifthenelse([ 0, 0, 0 ], hex_to_rgb(color))
8
+ end
9
+
10
+ def image(source, **opts, &block)
11
+ image = JekyllOgImage::Element::Image.new(
12
+ @canvas, source, **opts
13
+ )
14
+
15
+ @canvas = image.apply(&block)
16
+
17
+ self
18
+ end
19
+
20
+ def text(message, **opts, &block)
21
+ text = JekyllOgImage::Element::Text.new(
22
+ @canvas, message, **opts
23
+ )
24
+
25
+ @canvas = text.apply(&block)
26
+
27
+ self
28
+ end
29
+
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
36
+
37
+ self
38
+ end
39
+
40
+ def save(filename)
41
+ @canvas.write_to_file(filename)
42
+ end
43
+ end
@@ -1,33 +1,55 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "vips"
4
-
5
3
  class JekyllOgImage::Element::Image < JekyllOgImage::Element::Base
6
- def initialize(width, height, color: "#ffffff")
7
- @canvas = Vips::Image.black(width, height).ifthenelse([ 0, 0, 0 ], hex_to_rgb(color))
4
+ def initialize(canvas, source, gravity: :nw, width:, height:)
5
+ @canvas = canvas
6
+ @gravity = gravity
7
+ @source = source
8
+ @width = width
9
+ @height = height
10
+
11
+ validate_gravity!
8
12
  end
9
13
 
10
- def text(message, **opts, &block)
11
- text = JekyllOgImage::Element::Text.new(
12
- @canvas, message, **opts
13
- )
14
-
15
- @canvas = text.apply(&block)
16
-
17
- self
14
+ def apply(&block)
15
+ image = Vips::Image.new_from_buffer(@source, "")
16
+ result = block.call(@canvas, image) if block_given?
17
+
18
+ if @width && @height
19
+ ratio = calculate_ratio(image, @width, @height, :min)
20
+ image = image.resize(ratio)
21
+ end
22
+
23
+ x, y = result ? [ result.fetch(:x, 0), result.fetch(:y, 0) ] : [ 0, 0 ]
24
+
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
18
38
  end
19
39
 
20
- def border(width, position: :bottom, fill: "#000000")
21
- @canvas = JekyllOgImage::Element::Border.new(
22
- @canvas, width,
23
- position: position,
24
- fill: fill
25
- ).apply
40
+ private
26
41
 
27
- self
42
+ VALID_GRAVITY.each do |gravity|
43
+ define_method("gravity_#{gravity}?") do
44
+ @gravity == gravity
45
+ end
28
46
  end
29
47
 
30
- def save(filename)
31
- @canvas.write_to_file(filename)
48
+ def calculate_ratio(image, width, height, mode)
49
+ if mode == :min
50
+ [ width.to_f / image.width, height.to_f / image.height ].min
51
+ else
52
+ [ width.to_f / image.width, height.to_f / image.height ].max
53
+ end
32
54
  end
33
55
  end
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class JekyllOgImage::Element::Text < JekyllOgImage::Element::Base
4
- VALID_GRAVITY = %i[nw ne sw se].freeze
5
-
6
4
  def initialize(canvas, message, gravity: :nw, width: nil, dpi: nil, color: "#000000", font: nil)
7
5
  @canvas = canvas
8
6
  @gravity = gravity
@@ -49,10 +47,4 @@ class JekyllOgImage::Element::Text < JekyllOgImage::Element::Base
49
47
  @gravity == gravity
50
48
  end
51
49
  end
52
-
53
- def validate_gravity!
54
- unless VALID_GRAVITY.include?(@gravity)
55
- raise ArgumentError, "Invalid gravity: #{@gravity.inspect}"
56
- end
57
- end
58
50
  end
@@ -35,37 +35,48 @@ class JekyllOgImage::Generator < Jekyll::Generator
35
35
  def generate_image_for_post(site, post, path)
36
36
  date = post.date.strftime("%B %d, %Y")
37
37
 
38
- image = JekyllOgImage::Element::Image.new(1200, 600)
38
+ canvas = JekyllOgImage::Element::Canvas.new(1200, 600)
39
39
  .border(JekyllOgImage.config.border_bottom["width"],
40
40
  position: :bottom,
41
41
  fill: JekyllOgImage.config.border_bottom["fill"]
42
42
  )
43
- .text(post.data["title"], width: 1040, color: "#2f313d", dpi: 500, font: "Helvetica, Bold") do |_canvas, _text|
44
- {
45
- x: 80,
46
- y: 100
47
- }
48
- end
49
- .text(date, gravity: :sw, color: "#535358", dpi: 150, font: "Helvetica, Regular") do |_canvas, _text|
50
- {
51
- x: 80,
52
- y: post.data["tags"].any? ? 150 : 100
53
- }
54
- end
43
+
44
+ if JekyllOgImage.config.image
45
+ canvas = canvas.image(
46
+ File.read(File.join(site.config["source"], JekyllOgImage.config.image)),
47
+ gravity: :ne,
48
+ width: 150,
49
+ height: 150
50
+ ) { |_canvas, _text| { x: 80, y: 100 } }
51
+ end
52
+
53
+ canvas = canvas.text(post.data["title"],
54
+ width: JekyllOgImage.config.image ? 870 : 1040,
55
+ color: "#2f313d",
56
+ dpi: 400,
57
+ font: "Helvetica, Bold"
58
+ ) { |_canvas, _text| { x: 80, y: 100 } }
59
+
60
+ canvas = canvas.text(date,
61
+ gravity: :sw,
62
+ color: "#535358",
63
+ dpi: 150,
64
+ font: "Helvetica, Regular"
65
+ ) { |_canvas, _text| { x: 80, y: post.data["tags"].any? ? 150 : 100 } }
55
66
 
56
67
  if post.data["tags"].any?
57
68
  tags = post.data["tags"].map { |tag| "##{tag}" }.join(" ")
58
69
 
59
- image = image.text(tags, gravity: :sw, color: "#535358", dpi: 150, font: "Helvetica, Regular") do |_canvas, _text|
60
- {
61
- x: 80,
62
- y: 100
63
- }
64
- end
70
+ canvas = canvas.text(tags,
71
+ gravity: :sw,
72
+ color: "#535358",
73
+ dpi: 150,
74
+ font: "Helvetica, Regular"
75
+ ) { |_canvas, _text| { x: 80, y: 100 } }
65
76
  end
66
77
 
67
78
  if JekyllOgImage.config.domain
68
- image = image.text(JekyllOgImage.config.domain, gravity: :se, color: "#535358", dpi: 150, font: "Helvetica, Regular") do |_canvas, _text|
79
+ canvas = canvas.text(JekyllOgImage.config.domain, gravity: :se, color: "#535358", dpi: 150, font: "Helvetica, Regular") do |_canvas, _text|
69
80
  {
70
81
  x: 80,
71
82
  y: post.data["tags"].any? ? 150 : 100
@@ -73,6 +84,6 @@ class JekyllOgImage::Generator < Jekyll::Generator
73
84
  end
74
85
  end
75
86
 
76
- image.save(path)
87
+ canvas.save(path)
77
88
  end
78
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllOgImage
4
- VERSION = "1.0.3"
4
+ VERSION = "1.0.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-og-image
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Alexandrov
@@ -108,6 +108,7 @@ files:
108
108
  - lib/jekyll_og_image/config.rb
109
109
  - lib/jekyll_og_image/element/base.rb
110
110
  - lib/jekyll_og_image/element/border.rb
111
+ - lib/jekyll_og_image/element/canvas.rb
111
112
  - lib/jekyll_og_image/element/image.rb
112
113
  - lib/jekyll_og_image/element/text.rb
113
114
  - lib/jekyll_og_image/generator.rb