jekyll-og-image 1.0.2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/examples/1.png +0 -0
- data/lib/jekyll_og_image/config.rb +3 -0
- data/lib/jekyll_og_image/element/base.rb +8 -0
- data/lib/jekyll_og_image/element/canvas.rb +43 -0
- data/lib/jekyll_og_image/element/image.rb +43 -21
- data/lib/jekyll_og_image/element/text.rb +0 -8
- data/lib/jekyll_og_image/generator.rb +34 -22
- data/lib/jekyll_og_image/version.rb +1 -1
- data/lib/jekyll_og_image.rb +0 -2
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eeccdc0ae99d37ef260733506fbad4ebf5c60f925cdb2514acb91dcb346d1673
|
4
|
+
data.tar.gz: b8b291f97eddc6a0be9dc080d2f999be0bb027224bf5bf2953f941a89491b63a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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(
|
7
|
-
@canvas =
|
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
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
21
|
-
@canvas = JekyllOgImage::Element::Border.new(
|
22
|
-
@canvas, width,
|
23
|
-
position: position,
|
24
|
-
fill: fill
|
25
|
-
).apply
|
40
|
+
private
|
26
41
|
|
27
|
-
|
42
|
+
VALID_GRAVITY.each do |gravity|
|
43
|
+
define_method("gravity_#{gravity}?") do
|
44
|
+
@gravity == gravity
|
45
|
+
end
|
28
46
|
end
|
29
47
|
|
30
|
-
def
|
31
|
-
|
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
|
@@ -15,9 +15,10 @@ class JekyllOgImage::Generator < Jekyll::Generator
|
|
15
15
|
path = File.join(site.config["source"], base_path, "#{post.data['slug']}.png")
|
16
16
|
|
17
17
|
if !File.exist?(path) || JekyllOgImage.config.force?
|
18
|
+
Jekyll.logger.info "Jekyll Og Image:", "Generating image #{path}"
|
18
19
|
generate_image_for_post(site, post, path)
|
19
20
|
else
|
20
|
-
Jekyll.logger.info "Jekyll Og Image:", "Skipping image generation
|
21
|
+
Jekyll.logger.info "Jekyll Og Image:", "Skipping image generation #{path} as it already exists."
|
21
22
|
end
|
22
23
|
|
23
24
|
post.data["image"] ||= {
|
@@ -34,37 +35,48 @@ class JekyllOgImage::Generator < Jekyll::Generator
|
|
34
35
|
def generate_image_for_post(site, post, path)
|
35
36
|
date = post.date.strftime("%B %d, %Y")
|
36
37
|
|
37
|
-
|
38
|
+
canvas = JekyllOgImage::Element::Canvas.new(1200, 600)
|
38
39
|
.border(JekyllOgImage.config.border_bottom["width"],
|
39
40
|
position: :bottom,
|
40
41
|
fill: JekyllOgImage.config.border_bottom["fill"]
|
41
42
|
)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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 } }
|
54
66
|
|
55
67
|
if post.data["tags"].any?
|
56
68
|
tags = post.data["tags"].map { |tag| "##{tag}" }.join(" ")
|
57
69
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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 } }
|
64
76
|
end
|
65
77
|
|
66
78
|
if JekyllOgImage.config.domain
|
67
|
-
|
79
|
+
canvas = canvas.text(JekyllOgImage.config.domain, gravity: :se, color: "#535358", dpi: 150, font: "Helvetica, Regular") do |_canvas, _text|
|
68
80
|
{
|
69
81
|
x: 80,
|
70
82
|
y: post.data["tags"].any? ? 150 : 100
|
@@ -72,6 +84,6 @@ class JekyllOgImage::Generator < Jekyll::Generator
|
|
72
84
|
end
|
73
85
|
end
|
74
86
|
|
75
|
-
|
87
|
+
canvas.save(path)
|
76
88
|
end
|
77
89
|
end
|
data/lib/jekyll_og_image.rb
CHANGED
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.
|
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
|