jekyll-og-image 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4abcfee52f43ab5b0a56cbfb1386797c89f183ce031f610ae80c15d9fdb4152a
4
- data.tar.gz: 862a6a8209e19993e5f13c9da57a7d2896a695823b8007628a28741f212796cb
3
+ metadata.gz: eeccdc0ae99d37ef260733506fbad4ebf5c60f925cdb2514acb91dcb346d1673
4
+ data.tar.gz: b8b291f97eddc6a0be9dc080d2f999be0bb027224bf5bf2953f941a89491b63a
5
5
  SHA512:
6
- metadata.gz: 9867e3036591d613972c9fd86aa078aa29b79f938c63d089b017c44cce8e8c0d881852e6456bc93c19e770170eec6b535ac807f5957e55bf57c0512dbebb7554
7
- data.tar.gz: 691710ca4af48c7a032f37adc1a743fff47d943c1131ffd574832d7ffc48e1c46058bd2149cbadb16735a52cb3575f2d2b42af643fa60e7ab7f8cbc84f1c6546
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
@@ -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 for #{post.data['title']} as it already exists."
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
- image = JekyllOgImage::Element::Image.new(1200, 600)
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
- .text(post.data["title"], width: 1040, color: "#2f313d", dpi: 500, font: "Helvetica, Bold") do |_canvas, _text|
43
- {
44
- x: 80,
45
- y: 100
46
- }
47
- end
48
- .text(date, gravity: :sw, color: "#535358", dpi: 150, font: "Helvetica, Regular") do |_canvas, _text|
49
- {
50
- x: 80,
51
- y: post.data["tags"].any? ? 150 : 100
52
- }
53
- 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 } }
54
66
 
55
67
  if post.data["tags"].any?
56
68
  tags = post.data["tags"].map { |tag| "##{tag}" }.join(" ")
57
69
 
58
- image = image.text(tags, gravity: :sw, color: "#535358", dpi: 150, font: "Helvetica, Regular") do |_canvas, _text|
59
- {
60
- x: 80,
61
- y: 100
62
- }
63
- 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 } }
64
76
  end
65
77
 
66
78
  if JekyllOgImage.config.domain
67
- 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|
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
- image.save(path)
87
+ canvas.save(path)
76
88
  end
77
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllOgImage
4
- VERSION = "1.0.2"
4
+ VERSION = "1.0.4"
5
5
  end
@@ -25,6 +25,4 @@ end
25
25
 
26
26
  Jekyll::Hooks.register(:site, :after_init) do |site|
27
27
  JekyllOgImage.config = JekyllOgImage::Config.new(site.config["og_image"])
28
-
29
- site.config["exclude"] << JekyllOgImage.config.output_dir
30
28
  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.2
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