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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ade8806597db4aad25430a39bb1c7a8db08e4e474015344e7afe2e8bbd717038
4
- data.tar.gz: c97d3622656034a395f745aaeafe47fc994cb1c1a18ba9853251ef77f924ebcc
3
+ metadata.gz: 85114fbf8d07e0b91732c4c2ced348e28d7462a01cf1bafcd049d8a940d0c6bc
4
+ data.tar.gz: c49b4ed4846e8ba47817c320da37eb95f6bbebfe3496658e1d0c6618c71863ef
5
5
  SHA512:
6
- metadata.gz: 3398cc500da4c3ee7ea3be463247422636e37d9a363ab5b2aa94a42acd755f90341995c35b51055fe6bb446b90526a383d3bffbb507cbde64750b98cb85ac94c
7
- data.tar.gz: b76bdd36d899533d031990eb152bb93032e609877b32806b9a629c6c4590ddef5baae6e63e8f98a236574d3a21d0fb10538c64ac8d7fee7b64e92af55418c1a6
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` (required), the desired width for the image
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 `0` or `''`.
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 = :attention, auto_rotate = true)
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
- height = height if height.to_i > 1
16
- image = cached_image input, width, height, crop, auto_rotate
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
- image.write && image.optimize
19
- image.dest
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, width = nil, height = nil, crop = nil, auto_rotate = nil)
32
- @@cached_images[input] ||= Jekyll::Images::Thumbnail.new(@context.registers[:site],
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, filename, **args)
14
- unless File.exist? filename
15
- raise ArgumentError, "File not found: #{filename}"
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
- @width = args[:width]
22
- @height = args[:height]
23
- @crop = args[:crop].to_sym
24
- @auto_rotate = args[:auto_rotate]
25
- end
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 ||= image.thumbnail_image width,
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
- def height
40
- @height || proportional_height
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
- # Finds a heigth that's proportional to the width
44
- def proportional_height
45
- @proportional_height ||= (image.height * (width / image.width.to_f)).round
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.5
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