jekyll-images 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -3
- data/lib/jekyll/filters/thumbnail.rb +11 -14
- data/lib/jekyll/images/image.rb +29 -0
- data/lib/jekyll/images/thumbnail.rb +18 -23
- 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: 85114fbf8d07e0b91732c4c2ced348e28d7462a01cf1bafcd049d8a940d0c6bc
|
4
|
+
data.tar.gz: c49b4ed4846e8ba47817c320da37eb95f6bbebfe3496658e1d0c6618c71863ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3584ac278b582ffe5355cacd68d8a34b18fa928ba6275a90d457c77c0cbd0de87e824450b9987aa61328e146b9cbd8a210a77d3aba9ba285c438ba12d856454c
|
7
|
+
data.tar.gz: aa1b7a026a5dbafe44b4ba9b34c2f466b6c804ad62b00bcabbf9d3d1cdb666e0885e30271f9c38f54c73b5b7c1384fe89851ce055c671ef9a7b6a299a67985da
|
data/README.md
CHANGED
@@ -53,10 +53,12 @@ In your templates, you can use the `thumbnail` filter:
|
|
53
53
|
|
54
54
|
Options for this filter are in the following order:
|
55
55
|
|
56
|
-
* `width
|
56
|
+
* `width`, the desired width for the image, if `nil`, `height` is
|
57
|
+
required.
|
57
58
|
|
58
59
|
* `height`, if provided, the thumbnail will crop to this size. If not,
|
59
|
-
the image is scaled down proportionally to the width.
|
60
|
+
the image is scaled down proportionally to the width. It becomes
|
61
|
+
required if the width is `nil`.
|
60
62
|
|
61
63
|
* `crop` the smart cropping algorithm. One of `none`, `centre`,
|
62
64
|
`entropy` or `attention` (default). See
|
@@ -67,7 +69,7 @@ Options for this filter are in the following order:
|
|
67
69
|
orientation metadata, this controls if it's automatically rotated.
|
68
70
|
|
69
71
|
If you want to pass `crop` and `auto_rotate` but not `height`, just set
|
70
|
-
`height` to `
|
72
|
+
`height` to `nil`.
|
71
73
|
|
72
74
|
## TODO
|
73
75
|
|
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'pry'
|
4
3
|
module Jekyll
|
5
4
|
module Filters
|
6
5
|
# Liquid filter for use in templates
|
@@ -8,15 +7,18 @@ module Jekyll
|
|
8
7
|
@@cached_images = {}
|
9
8
|
|
10
9
|
# Generates a thumbnail and returns its alternate destination
|
11
|
-
def thumbnail(input, width, height = nil, crop =
|
10
|
+
def thumbnail(input, width = nil, height = nil, crop = nil, auto_rotate = nil)
|
12
11
|
return unless input
|
13
|
-
return cached_image(input).dest if cached_image? input
|
14
12
|
|
15
|
-
|
16
|
-
|
13
|
+
image = cached_image input
|
14
|
+
thumb = image.thumbnail(width: width,
|
15
|
+
height: height,
|
16
|
+
crop: crop,
|
17
|
+
auto_rotate: auto_rotate)
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
thumb.write && thumb.optimize
|
20
|
+
|
21
|
+
thumb.dest
|
20
22
|
rescue Vips::Error => e
|
21
23
|
Jekyll.logger.warn "Failed to process #{input}: #{e.message}"
|
22
24
|
input
|
@@ -28,13 +30,8 @@ module Jekyll
|
|
28
30
|
@@cached_images.key? input
|
29
31
|
end
|
30
32
|
|
31
|
-
def cached_image(input
|
32
|
-
@@cached_images[input] ||= Jekyll::Images::
|
33
|
-
input,
|
34
|
-
width: width,
|
35
|
-
height: height,
|
36
|
-
crop: crop,
|
37
|
-
auto_rotate: auto_rotate)
|
33
|
+
def cached_image(input)
|
34
|
+
@@cached_images[input] ||= Jekyll::Images::Image.new(@context.registers[:site], input)
|
38
35
|
end
|
39
36
|
end
|
40
37
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'vips'
|
4
|
+
|
5
|
+
module Jekyll
|
6
|
+
module Images
|
7
|
+
class Image
|
8
|
+
attr_reader :site, :filename
|
9
|
+
|
10
|
+
def initialize(site, filename)
|
11
|
+
unless File.exist? filename
|
12
|
+
raise ArgumentError, "File not found: #{filename}"
|
13
|
+
end
|
14
|
+
|
15
|
+
@cached_thumbnails = {}
|
16
|
+
@site = site
|
17
|
+
@filename = filename
|
18
|
+
end
|
19
|
+
|
20
|
+
def image
|
21
|
+
@image ||= Vips::Image.new_from_file filename, access: :sequential
|
22
|
+
end
|
23
|
+
|
24
|
+
def thumbnail(**args)
|
25
|
+
@cached_thumbnails[args.hash.to_s] ||= Thumbnail.new site: site, filename: filename, image: image, **args
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -4,45 +4,39 @@ require 'vips'
|
|
4
4
|
|
5
5
|
module Jekyll
|
6
6
|
module Images
|
7
|
-
# Use LibVIPS to generate images in different sizes
|
8
|
-
#
|
9
|
-
# We're assuming every image is going to be thumbnailed
|
10
7
|
class Thumbnail
|
11
|
-
attr_reader :site, :filename, :width, :height, :crop, :auto_rotate
|
8
|
+
attr_reader :site, :filename, :image, :width, :height, :crop, :auto_rotate
|
12
9
|
|
13
|
-
def initialize(site
|
14
|
-
|
15
|
-
raise ArgumentError, "
|
10
|
+
def initialize(site:, filename:, image:, **args)
|
11
|
+
if args.slice(:width, :height).values.none?
|
12
|
+
raise ArgumentError, "#{filename} thumbnail needs width or height"
|
16
13
|
end
|
17
|
-
raise ArgumentError, 'Missing width' unless args[:width]
|
18
14
|
|
19
15
|
@site = site
|
20
16
|
@filename = filename
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@
|
25
|
-
|
26
|
-
|
27
|
-
def image
|
28
|
-
@image ||= Vips::Image.new_from_file filename,
|
29
|
-
access: :sequential
|
17
|
+
@image = image
|
18
|
+
@width = args[:width] || proportional_width(args[:height])
|
19
|
+
@height = args[:height] || proportional_height(args[:width])
|
20
|
+
@crop = args[:crop]&.to_sym || :attention
|
21
|
+
@auto_rotate = args[:auto_rotate].nil? ? true : args[:auto_rotate]
|
30
22
|
end
|
31
23
|
|
32
24
|
def thumbnail
|
33
|
-
@thumbnail ||=
|
25
|
+
@thumbnail ||= Vips::Image.thumbnail filename,
|
26
|
+
width,
|
34
27
|
height: height,
|
35
28
|
auto_rotate: auto_rotate,
|
36
29
|
crop: crop
|
37
30
|
end
|
38
31
|
|
39
|
-
|
40
|
-
|
32
|
+
# Finds a height that's proportional to the width
|
33
|
+
def proportional_height(width)
|
34
|
+
@proportional_height ||= (image.height * (width / image.width.to_f)).round
|
41
35
|
end
|
42
36
|
|
43
|
-
#
|
44
|
-
def
|
45
|
-
@
|
37
|
+
# Find a width that's proportional to height
|
38
|
+
def proportional_width(height)
|
39
|
+
@proportional_width ||= (image.width * (height / image.height.to_f)).round
|
46
40
|
end
|
47
41
|
|
48
42
|
# Generates a destination from filename only if we're downsizing
|
@@ -76,6 +70,7 @@ module Jekyll
|
|
76
70
|
Jekyll.logger.info "Thumbnailing #{filename} => #{dest}"
|
77
71
|
thumbnail.write_to_file(dest)
|
78
72
|
else
|
73
|
+
Jekyll.logger.info "Copying #{filename} => #{dest}"
|
79
74
|
FileUtils.cp(filename, dest)
|
80
75
|
end
|
81
76
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-images
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- f
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- README.md
|
66
66
|
- lib/jekyll-images.rb
|
67
67
|
- lib/jekyll/filters/thumbnail.rb
|
68
|
+
- lib/jekyll/images/image.rb
|
68
69
|
- lib/jekyll/images/jpeg_optim.rb
|
69
70
|
- lib/jekyll/images/oxipng.rb
|
70
71
|
- lib/jekyll/images/runner.rb
|