next_gen_images 1.1.1 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +173 -138
- data/README.md +20 -69
- data/lib/next_gen_images/config.rb +21 -0
- data/lib/next_gen_images/railtie.rb +0 -6
- data/lib/next_gen_images/tasks/assets/webp.rake +23 -6
- data/lib/next_gen_images/version.rb +1 -1
- data/lib/next_gen_images.rb +1 -1
- data/next_gen_images.gemspec +2 -2
- metadata +7 -7
- data/lib/next_gen_images/view_helpers.rb +0 -87
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b668858b2cfb225e8e909828eb0c45bb08016e881059a1c205997ad9d3fda2d3
|
4
|
+
data.tar.gz: 450bc886860b15d583aabc154c9e8bbbecf2cf92c43bb8f5c8f9f4c866e3c489
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53117995e662f8a3320f4313f512bbb14500b5d0a2757576a7240dc6b7419316207e4b3c1ed24008bb5c637f62e0551c4386aeb3a2bea9cf327a1a1643ec9ffa
|
7
|
+
data.tar.gz: cdc234b4fab76feb34c6d4bfed49dbb782452a863404e04e7005f6ab2435b4b8026fe901dcb8fb6cc6facc0764e4203c9f4d6a17dbad1f33e62b9316e05348d6
|
data/Gemfile.lock
CHANGED
@@ -1,108 +1,126 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
next_gen_images (1.
|
5
|
-
rails (>=
|
6
|
-
webp-ffi (~> 0.
|
4
|
+
next_gen_images (1.2.1)
|
5
|
+
rails (>= 7.1)
|
6
|
+
webp-ffi (~> 0.4.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
actioncable (7.
|
12
|
-
actionpack (= 7.
|
13
|
-
activesupport (= 7.
|
11
|
+
actioncable (7.1.4)
|
12
|
+
actionpack (= 7.1.4)
|
13
|
+
activesupport (= 7.1.4)
|
14
14
|
nio4r (~> 2.0)
|
15
15
|
websocket-driver (>= 0.6.1)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
zeitwerk (~> 2.6)
|
17
|
+
actionmailbox (7.1.4)
|
18
|
+
actionpack (= 7.1.4)
|
19
|
+
activejob (= 7.1.4)
|
20
|
+
activerecord (= 7.1.4)
|
21
|
+
activestorage (= 7.1.4)
|
22
|
+
activesupport (= 7.1.4)
|
22
23
|
mail (>= 2.7.1)
|
23
24
|
net-imap
|
24
25
|
net-pop
|
25
26
|
net-smtp
|
26
|
-
actionmailer (7.
|
27
|
-
actionpack (= 7.
|
28
|
-
actionview (= 7.
|
29
|
-
activejob (= 7.
|
30
|
-
activesupport (= 7.
|
27
|
+
actionmailer (7.1.4)
|
28
|
+
actionpack (= 7.1.4)
|
29
|
+
actionview (= 7.1.4)
|
30
|
+
activejob (= 7.1.4)
|
31
|
+
activesupport (= 7.1.4)
|
31
32
|
mail (~> 2.5, >= 2.5.4)
|
32
33
|
net-imap
|
33
34
|
net-pop
|
34
35
|
net-smtp
|
35
|
-
rails-dom-testing (~> 2.
|
36
|
-
actionpack (7.
|
37
|
-
actionview (= 7.
|
38
|
-
activesupport (= 7.
|
39
|
-
|
36
|
+
rails-dom-testing (~> 2.2)
|
37
|
+
actionpack (7.1.4)
|
38
|
+
actionview (= 7.1.4)
|
39
|
+
activesupport (= 7.1.4)
|
40
|
+
nokogiri (>= 1.8.5)
|
41
|
+
racc
|
42
|
+
rack (>= 2.2.4)
|
43
|
+
rack-session (>= 1.0.1)
|
40
44
|
rack-test (>= 0.6.3)
|
41
|
-
rails-dom-testing (~> 2.
|
42
|
-
rails-html-sanitizer (~> 1.
|
43
|
-
actiontext (7.
|
44
|
-
actionpack (= 7.
|
45
|
-
activerecord (= 7.
|
46
|
-
activestorage (= 7.
|
47
|
-
activesupport (= 7.
|
45
|
+
rails-dom-testing (~> 2.2)
|
46
|
+
rails-html-sanitizer (~> 1.6)
|
47
|
+
actiontext (7.1.4)
|
48
|
+
actionpack (= 7.1.4)
|
49
|
+
activerecord (= 7.1.4)
|
50
|
+
activestorage (= 7.1.4)
|
51
|
+
activesupport (= 7.1.4)
|
48
52
|
globalid (>= 0.6.0)
|
49
53
|
nokogiri (>= 1.8.5)
|
50
|
-
actionview (7.
|
51
|
-
activesupport (= 7.
|
54
|
+
actionview (7.1.4)
|
55
|
+
activesupport (= 7.1.4)
|
52
56
|
builder (~> 3.1)
|
53
|
-
erubi (~> 1.
|
54
|
-
rails-dom-testing (~> 2.
|
55
|
-
rails-html-sanitizer (~> 1.
|
56
|
-
activejob (7.
|
57
|
-
activesupport (= 7.
|
57
|
+
erubi (~> 1.11)
|
58
|
+
rails-dom-testing (~> 2.2)
|
59
|
+
rails-html-sanitizer (~> 1.6)
|
60
|
+
activejob (7.1.4)
|
61
|
+
activesupport (= 7.1.4)
|
58
62
|
globalid (>= 0.3.6)
|
59
|
-
activemodel (7.
|
60
|
-
activesupport (= 7.
|
61
|
-
activerecord (7.
|
62
|
-
activemodel (= 7.
|
63
|
-
activesupport (= 7.
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
63
|
+
activemodel (7.1.4)
|
64
|
+
activesupport (= 7.1.4)
|
65
|
+
activerecord (7.1.4)
|
66
|
+
activemodel (= 7.1.4)
|
67
|
+
activesupport (= 7.1.4)
|
68
|
+
timeout (>= 0.4.0)
|
69
|
+
activestorage (7.1.4)
|
70
|
+
actionpack (= 7.1.4)
|
71
|
+
activejob (= 7.1.4)
|
72
|
+
activerecord (= 7.1.4)
|
73
|
+
activesupport (= 7.1.4)
|
69
74
|
marcel (~> 1.0)
|
70
|
-
|
71
|
-
|
75
|
+
activesupport (7.1.4)
|
76
|
+
base64
|
77
|
+
bigdecimal
|
72
78
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
79
|
+
connection_pool (>= 2.2.5)
|
80
|
+
drb
|
73
81
|
i18n (>= 1.6, < 2)
|
74
82
|
minitest (>= 5.1)
|
83
|
+
mutex_m
|
75
84
|
tzinfo (~> 2.0)
|
76
|
-
addressable (2.8.
|
77
|
-
public_suffix (>= 2.0.2, <
|
85
|
+
addressable (2.8.7)
|
86
|
+
public_suffix (>= 2.0.2, < 7.0)
|
78
87
|
ast (2.4.2)
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
88
|
+
base64 (0.2.0)
|
89
|
+
bigdecimal (3.1.8)
|
90
|
+
builder (3.3.0)
|
91
|
+
carrierwave (3.0.7)
|
92
|
+
activemodel (>= 6.0.0)
|
93
|
+
activesupport (>= 6.0.0)
|
83
94
|
addressable (~> 2.6)
|
84
95
|
image_processing (~> 1.1)
|
85
96
|
marcel (~> 1.0.0)
|
86
|
-
mini_mime (>= 0.1.3)
|
87
97
|
ssrf_filter (~> 1.0)
|
88
|
-
concurrent-ruby (1.
|
98
|
+
concurrent-ruby (1.3.4)
|
99
|
+
connection_pool (2.4.1)
|
89
100
|
crass (1.0.6)
|
90
|
-
date (3.3.
|
91
|
-
diff-lcs (1.5.
|
92
|
-
|
93
|
-
|
94
|
-
ffi
|
95
|
-
|
101
|
+
date (3.3.4)
|
102
|
+
diff-lcs (1.5.1)
|
103
|
+
drb (2.2.1)
|
104
|
+
erubi (1.13.0)
|
105
|
+
ffi (1.17.0)
|
106
|
+
ffi-compiler (1.3.2)
|
107
|
+
ffi (>= 1.15.5)
|
96
108
|
rake
|
97
|
-
globalid (1.1
|
98
|
-
activesupport (>=
|
99
|
-
i18n (1.
|
109
|
+
globalid (1.2.1)
|
110
|
+
activesupport (>= 6.1)
|
111
|
+
i18n (1.14.5)
|
100
112
|
concurrent-ruby (~> 1.0)
|
101
|
-
image_processing (1.
|
113
|
+
image_processing (1.13.0)
|
102
114
|
mini_magick (>= 4.9.5, < 5)
|
103
115
|
ruby-vips (>= 2.0.17, < 3)
|
104
|
-
|
105
|
-
|
116
|
+
io-console (0.7.2)
|
117
|
+
irb (1.14.0)
|
118
|
+
rdoc (>= 4.0.0)
|
119
|
+
reline (>= 0.4.2)
|
120
|
+
json (2.7.2)
|
121
|
+
language_server-protocol (3.17.0.3)
|
122
|
+
logger (1.6.1)
|
123
|
+
loofah (2.22.0)
|
106
124
|
crass (~> 1.0.2)
|
107
125
|
nokogiri (>= 1.12.0)
|
108
126
|
mail (2.8.1)
|
@@ -110,104 +128,121 @@ GEM
|
|
110
128
|
net-imap
|
111
129
|
net-pop
|
112
130
|
net-smtp
|
113
|
-
marcel (1.0.
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
net-imap (0.
|
131
|
+
marcel (1.0.4)
|
132
|
+
mini_magick (4.13.2)
|
133
|
+
mini_mime (1.1.5)
|
134
|
+
minitest (5.25.1)
|
135
|
+
mutex_m (0.2.0)
|
136
|
+
net-imap (0.4.16)
|
119
137
|
date
|
120
138
|
net-protocol
|
121
139
|
net-pop (0.1.2)
|
122
140
|
net-protocol
|
123
|
-
net-protocol (0.2.
|
141
|
+
net-protocol (0.2.2)
|
124
142
|
timeout
|
125
|
-
net-smtp (0.
|
143
|
+
net-smtp (0.5.0)
|
126
144
|
net-protocol
|
127
|
-
nio4r (2.
|
128
|
-
nokogiri (1.
|
145
|
+
nio4r (2.7.3)
|
146
|
+
nokogiri (1.16.7-arm64-darwin)
|
129
147
|
racc (~> 1.4)
|
130
|
-
parallel (1.
|
131
|
-
parser (3.
|
148
|
+
parallel (1.26.3)
|
149
|
+
parser (3.3.5.0)
|
132
150
|
ast (~> 2.4.1)
|
133
|
-
|
134
|
-
|
135
|
-
|
151
|
+
racc
|
152
|
+
psych (5.1.2)
|
153
|
+
stringio
|
154
|
+
public_suffix (6.0.1)
|
155
|
+
racc (1.8.1)
|
156
|
+
rack (3.1.7)
|
157
|
+
rack-session (2.0.0)
|
158
|
+
rack (>= 3.0.0)
|
136
159
|
rack-test (2.1.0)
|
137
160
|
rack (>= 1.3)
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
161
|
+
rackup (2.1.0)
|
162
|
+
rack (>= 3)
|
163
|
+
webrick (~> 1.8)
|
164
|
+
rails (7.1.4)
|
165
|
+
actioncable (= 7.1.4)
|
166
|
+
actionmailbox (= 7.1.4)
|
167
|
+
actionmailer (= 7.1.4)
|
168
|
+
actionpack (= 7.1.4)
|
169
|
+
actiontext (= 7.1.4)
|
170
|
+
actionview (= 7.1.4)
|
171
|
+
activejob (= 7.1.4)
|
172
|
+
activemodel (= 7.1.4)
|
173
|
+
activerecord (= 7.1.4)
|
174
|
+
activestorage (= 7.1.4)
|
175
|
+
activesupport (= 7.1.4)
|
150
176
|
bundler (>= 1.15.0)
|
151
|
-
railties (= 7.
|
152
|
-
rails-dom-testing (2.0
|
153
|
-
activesupport (>=
|
177
|
+
railties (= 7.1.4)
|
178
|
+
rails-dom-testing (2.2.0)
|
179
|
+
activesupport (>= 5.0.0)
|
180
|
+
minitest
|
154
181
|
nokogiri (>= 1.6)
|
155
|
-
rails-html-sanitizer (1.
|
156
|
-
loofah (~> 2.
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
182
|
+
rails-html-sanitizer (1.6.0)
|
183
|
+
loofah (~> 2.21)
|
184
|
+
nokogiri (~> 1.14)
|
185
|
+
railties (7.1.4)
|
186
|
+
actionpack (= 7.1.4)
|
187
|
+
activesupport (= 7.1.4)
|
188
|
+
irb
|
189
|
+
rackup (>= 1.0.0)
|
161
190
|
rake (>= 12.2)
|
162
|
-
thor (~> 1.0)
|
163
|
-
zeitwerk (~> 2.
|
191
|
+
thor (~> 1.0, >= 1.2.2)
|
192
|
+
zeitwerk (~> 2.6)
|
164
193
|
rainbow (3.1.1)
|
165
|
-
rake (13.
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
rspec-
|
174
|
-
|
194
|
+
rake (13.2.1)
|
195
|
+
rdoc (6.7.0)
|
196
|
+
psych (>= 4.0.0)
|
197
|
+
regexp_parser (2.9.2)
|
198
|
+
reline (0.5.10)
|
199
|
+
io-console (~> 0.5)
|
200
|
+
rspec (3.13.0)
|
201
|
+
rspec-core (~> 3.13.0)
|
202
|
+
rspec-expectations (~> 3.13.0)
|
203
|
+
rspec-mocks (~> 3.13.0)
|
204
|
+
rspec-core (3.13.1)
|
205
|
+
rspec-support (~> 3.13.0)
|
206
|
+
rspec-expectations (3.13.3)
|
175
207
|
diff-lcs (>= 1.2.0, < 2.0)
|
176
|
-
rspec-support (~> 3.
|
177
|
-
rspec-mocks (3.
|
208
|
+
rspec-support (~> 3.13.0)
|
209
|
+
rspec-mocks (3.13.1)
|
178
210
|
diff-lcs (>= 1.2.0, < 2.0)
|
179
|
-
rspec-support (~> 3.
|
180
|
-
rspec-support (3.
|
181
|
-
rubocop (1.
|
211
|
+
rspec-support (~> 3.13.0)
|
212
|
+
rspec-support (3.13.1)
|
213
|
+
rubocop (1.66.1)
|
182
214
|
json (~> 2.3)
|
215
|
+
language_server-protocol (>= 3.17.0)
|
183
216
|
parallel (~> 1.10)
|
184
|
-
parser (>= 3.
|
217
|
+
parser (>= 3.3.0.2)
|
185
218
|
rainbow (>= 2.2.2, < 4.0)
|
186
|
-
regexp_parser (>=
|
187
|
-
|
188
|
-
rubocop-ast (>= 1.23.0, < 2.0)
|
219
|
+
regexp_parser (>= 2.4, < 3.0)
|
220
|
+
rubocop-ast (>= 1.32.2, < 2.0)
|
189
221
|
ruby-progressbar (~> 1.7)
|
190
|
-
unicode-display_width (>=
|
191
|
-
rubocop-ast (1.
|
192
|
-
parser (>= 3.
|
222
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
223
|
+
rubocop-ast (1.32.3)
|
224
|
+
parser (>= 3.3.1.0)
|
193
225
|
rubocop-rspec (2.17.1)
|
194
226
|
rubocop (~> 1.33)
|
195
|
-
ruby-progressbar (1.
|
196
|
-
ruby-vips (2.
|
227
|
+
ruby-progressbar (1.13.0)
|
228
|
+
ruby-vips (2.2.2)
|
197
229
|
ffi (~> 1.12)
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
230
|
+
logger
|
231
|
+
ssrf_filter (1.1.2)
|
232
|
+
stringio (3.1.1)
|
233
|
+
thor (1.3.2)
|
234
|
+
timeout (0.4.1)
|
235
|
+
tzinfo (2.0.6)
|
202
236
|
concurrent-ruby (~> 1.0)
|
203
|
-
unicode-display_width (2.
|
204
|
-
webp-ffi (0.
|
237
|
+
unicode-display_width (2.5.0)
|
238
|
+
webp-ffi (0.4.0)
|
205
239
|
ffi (>= 1.9.0)
|
206
240
|
ffi-compiler (>= 0.1.2)
|
207
|
-
|
241
|
+
webrick (1.8.1)
|
242
|
+
websocket-driver (0.7.6)
|
208
243
|
websocket-extensions (>= 0.1.0)
|
209
244
|
websocket-extensions (0.1.5)
|
210
|
-
zeitwerk (2.6.
|
245
|
+
zeitwerk (2.6.18)
|
211
246
|
|
212
247
|
PLATFORMS
|
213
248
|
arm64-darwin-21
|
data/README.md
CHANGED
@@ -2,10 +2,11 @@
|
|
2
2
|
|
3
3
|
## Motivation
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
*Update:*
|
6
|
+
Since Rails 7.1 and this [PR](https://github.com/rails/rails/pull/48100), Rails has a `picture` HTML tag helper.
|
7
7
|
|
8
|
-
This gem
|
8
|
+
This gem provides helpers to simplify the transition to using WebP images in Ruby on Rails.
|
9
|
+
A Carrierwave module with some utility methods is provided.
|
9
10
|
|
10
11
|
|
11
12
|
## Installation
|
@@ -23,93 +24,44 @@ Or add it to your application's Gemfile
|
|
23
24
|
```ruby
|
24
25
|
gem 'next_gen_images'
|
25
26
|
```
|
26
|
-
and then run
|
27
|
+
and then run
|
27
28
|
```bash
|
28
29
|
bundle install
|
29
30
|
```
|
30
31
|
|
31
32
|
## Usage
|
32
33
|
|
33
|
-
###
|
34
|
+
### Asset conversion to public/assets for production usage
|
34
35
|
|
35
|
-
|
36
|
-
```
|
37
|
-
picture_tag SOURCE, PICTURE_TAG_OPTIONS, image: IMAGE_TAG_OPTIONS
|
38
|
-
```
|
39
|
-
|
40
|
-
This will output something like this:
|
41
|
-
```html
|
42
|
-
<picture PICTURE_TAG_OPTIONS>
|
43
|
-
<source srcset=SOURCE ...>
|
44
|
-
...
|
45
|
-
<img IMAGE_TAG_OPTIONS>
|
46
|
-
</picture>
|
47
|
-
```
|
48
|
-
|
49
|
-
Depending on your needs, you can use the `picture_tag` in 3 different ways:
|
36
|
+
A rake task called `next_gen_images:precompile` that precompiles all of your images to WebP is provided. You can enable it so it runs automatically when you do an `assets:precompile` via an initializer like this:
|
50
37
|
|
51
|
-
1. You can pass your existing image as the source, and add the `add_webp: true` option to automatically infer the `webp` image from the PNG/JPEG and add fallback image support.
|
52
38
|
```ruby
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
```html
|
57
|
-
<picture>
|
58
|
-
<source srcset="/assets/satellite.png.web" type="image/webp">
|
59
|
-
<source srcset="/assets/satellite.png" type="image/png">
|
60
|
-
<img alt="satellite image" class="mt-0" src="/assets/satellite.png.webp">
|
61
|
-
</picture>
|
62
|
-
```
|
63
|
-
|
64
|
-
2. If you want to manually specify your sources, you can pass an array of images to the `picture_tag`. This is extremely useful for dynamically generated images (that a user uploaded for example).
|
65
|
-
```ruby
|
66
|
-
picture_tag [post.cover_image.webp.medium.url, post.cover_image.medium.url], image: { alt: 'post image', class: 'rounded' }
|
39
|
+
NextGenImages.configure do |config|
|
40
|
+
config.run_on_precompile = true
|
41
|
+
end
|
67
42
|
```
|
68
43
|
|
69
|
-
|
70
|
-
```
|
71
|
-
|
72
|
-
<source srcset="/uploads/posts/1/cover-image.png.webp" type="image/webp">
|
73
|
-
<source srcset="/uploads/posts/1/cover-image.png" type="image/png">
|
74
|
-
<img alt="post image" class="rounded" src="/uploads/posts/1/cover-image.png.webp">
|
75
|
-
</picture>
|
44
|
+
If you want to manually convert your images, you can run:
|
45
|
+
```bash
|
46
|
+
rake next_gen_images:precompile
|
76
47
|
```
|
48
|
+
Assets will be compiled to `public/assets/` or the path that you specifed in `Rails.application.config.assets.prefix`.
|
49
|
+
The file names of the generated image assets will be in the format:`OLD_IMAGE.OLD_EXTENSION.webp`
|
77
50
|
|
78
|
-
|
51
|
+
### Asset conversion of /app/assets/images directory
|
79
52
|
|
80
|
-
|
81
|
-
= picture_tag 'city.png', image: { alt: 'city image' } do
|
82
|
-
= source_tag srcset: "#{image_path('city_small.png')}.webp", type: 'image/webp', media: '(max-width: 1728px)'
|
83
|
-
= source_tag srcset: "#{image_path('city_big.png')}.webp", type: 'image/webp'
|
84
|
-
= source_tag srcset: image_path('city_small.png'), type: 'image/png', media: '(max-width: 1728px)'
|
85
|
-
= source_tag srcset: image_path('city_big.png'), type: 'image/png'
|
86
|
-
```
|
87
|
-
Which will create the following HTML:
|
88
|
-
```html
|
89
|
-
<picture>
|
90
|
-
<source srcset="/assets/city_small.png.webp" type="image/webp" media="(max-width: 1728px)">
|
91
|
-
<source srcset="/assets/city_small.png.webp" type="image/webp">
|
92
|
-
<source srcset="/assets/city_small.png" type="image/png" media="(max-width: 1728px)">
|
93
|
-
<source srcset="/assets/city_big.png" type="image/png">
|
94
|
-
<img alt="city image" src="/assets/city_small.png.webp">
|
95
|
-
</picture>
|
96
|
-
```
|
53
|
+
In addition to the `next_gen_images:precompile` task, there's also a task to convert images in the `app/assets/images` directory to WebP. It leaves the original images untouched:
|
97
54
|
|
98
|
-
### Asset conversion
|
99
|
-
|
100
|
-
A rake task called `assets:webp` that converts all of your images to WebP is provided. When you run the `assets:precompile` task, the `assets:webp` task will also run (via an `enhance` to the `assets:precompile` task) and automatically convert your images to WebP.
|
101
|
-
If you want to manually convert your images, you can run:
|
102
55
|
```bash
|
103
|
-
rake
|
56
|
+
rake next_gen_images:convert_app_images
|
104
57
|
```
|
105
|
-
|
106
|
-
The file names of the generated image assets will be in the format:`OLD_IMAGE.OLD_EXTENSION.webp`
|
58
|
+
The file names of the generated image assets will be in the format `OLD_IMAGE.webp` and will be placed in the `app/assets/images` directory.
|
107
59
|
|
108
60
|
### ActiveStorage integration
|
109
61
|
|
110
62
|
If you are using ActiveStorage you will need to first enable the `image_processing` gem. Usually, uncommenting this from your Gemfile:
|
111
63
|
```
|
112
|
-
gem "image_processing"
|
64
|
+
gem "image_processing"
|
113
65
|
```
|
114
66
|
|
115
67
|
Then, you will need to install the VIPS image processing library binaries. Check the [VIPS Ruby wrapper gem](https://github.com/libvips/ruby-vips) for installation instructions for your OS.
|
@@ -141,7 +93,6 @@ class User < ApplicationRecord
|
|
141
93
|
end
|
142
94
|
```
|
143
95
|
|
144
|
-
|
145
96
|
### Carrierwave integration
|
146
97
|
|
147
98
|
A `convert_to_webp` method that converts images to WebP is provided. You can send any encoding option available to [webp-ffi](https://github.com/le0pard/webp-ffi#encode-webp-image).
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NextGenImages
|
4
|
+
class << self
|
5
|
+
def configure
|
6
|
+
yield(config)
|
7
|
+
end
|
8
|
+
|
9
|
+
def config
|
10
|
+
@config ||= Config.new
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Config
|
15
|
+
attr_accessor :run_on_precompile
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@run_on_precompile = false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,14 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'next_gen_images/view_helpers'
|
4
|
-
|
5
3
|
module NextGenImages
|
6
4
|
# Load everything we need into rails
|
7
5
|
class Railtie < Rails::Railtie
|
8
|
-
initializer 'next_gen_images.view_helpers' do
|
9
|
-
ActionView::Base.include ViewHelpers
|
10
|
-
end
|
11
|
-
|
12
6
|
rake_tasks do
|
13
7
|
path = File.expand_path(__dir__)
|
14
8
|
Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
|
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
require 'webp-ffi'
|
4
4
|
|
5
|
-
namespace :
|
5
|
+
namespace :next_gen_images do # rubocop:disable Metrics/BlockLength
|
6
6
|
desc 'Create .webp versions of assets'
|
7
|
-
task
|
7
|
+
task precompile: :environment do
|
8
8
|
image_types = /\.(?:png|jpe?g)$/
|
9
9
|
|
10
10
|
public_assets = File.join(
|
@@ -31,9 +31,26 @@ namespace :assets do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
desc 'Convert JPEG/PNG images in app/assets/images to WebP'
|
35
|
+
task convert_app_images: :environment do
|
36
|
+
source_dir = Rails.root.join('app', 'assets', 'images')
|
37
|
+
|
38
|
+
Dir.glob(File.join(source_dir, '**', '*.{jpg,jpeg,png}')).each do |image_path|
|
39
|
+
webp_path = image_path.sub(/\.(jpg|jpeg|png)$/i, '.webp')
|
40
|
+
|
41
|
+
begin
|
42
|
+
WebP.encode(image_path, webp_path, lossless: 0, quality: 80, method: 6)
|
43
|
+
puts "Converted: #{image_path} -> #{webp_path}"
|
44
|
+
rescue StandardError => e
|
45
|
+
puts "Error converting #{image_path}: #{e.message}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
puts 'Conversion complete. WebP images are in the same directories as the original images.'
|
38
50
|
end
|
39
51
|
end
|
52
|
+
|
53
|
+
# Hook into existing assets:precompile task
|
54
|
+
Rake::Task['assets:precompile'].enhance do
|
55
|
+
Rake::Task['next_gen_images:precompile'].invoke if defined?(NextGenImages) && NextGenImages.config.run_on_precompile
|
56
|
+
end
|
data/lib/next_gen_images.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'webp-ffi'
|
4
4
|
|
5
5
|
require_relative 'next_gen_images/version'
|
6
|
+
require_relative 'next_gen_images/config'
|
6
7
|
require_relative 'next_gen_images/railtie' if defined?(Rails)
|
7
8
|
require_relative 'next_gen_images/engine'
|
8
|
-
require_relative 'next_gen_images/view_helpers'
|
9
9
|
require_relative 'next_gen_images/carrierwave_helpers'
|
data/next_gen_images.gemspec
CHANGED
@@ -31,8 +31,8 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
32
|
spec.require_paths = ['lib']
|
33
33
|
|
34
|
-
spec.
|
35
|
-
spec.
|
34
|
+
spec.add_dependency 'rails', '>= 7.1'
|
35
|
+
spec.add_dependency 'webp-ffi', '~> 0.4.0'
|
36
36
|
spec.add_development_dependency 'bundler'
|
37
37
|
spec.add_development_dependency 'carrierwave', '>= 2.0'
|
38
38
|
spec.add_development_dependency 'rspec', '~> 3.12.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: next_gen_images
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JP Balarini
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '7.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '7.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: webp-ffi
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.4.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 0.4.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -111,11 +111,11 @@ files:
|
|
111
111
|
- Rakefile
|
112
112
|
- lib/next_gen_images.rb
|
113
113
|
- lib/next_gen_images/carrierwave_helpers.rb
|
114
|
+
- lib/next_gen_images/config.rb
|
114
115
|
- lib/next_gen_images/engine.rb
|
115
116
|
- lib/next_gen_images/railtie.rb
|
116
117
|
- lib/next_gen_images/tasks/assets/webp.rake
|
117
118
|
- lib/next_gen_images/version.rb
|
118
|
-
- lib/next_gen_images/view_helpers.rb
|
119
119
|
- next_gen_images.gemspec
|
120
120
|
- sig/next_gen_images.rbs
|
121
121
|
homepage: https://eagerworks.com
|
@@ -1,87 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'action_view'
|
4
|
-
|
5
|
-
module NextGenImages
|
6
|
-
module ViewHelpers
|
7
|
-
def self.included(klass)
|
8
|
-
klass.class_eval do
|
9
|
-
include ActionView::Context
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def picture_tag(source, options = {}, &block)
|
14
|
-
picture_options = options.except(:image)
|
15
|
-
|
16
|
-
content_tag :picture, picture_options do
|
17
|
-
build_picture_content(source, options, block)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def source_tag(options = {})
|
22
|
-
tag :source, options
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def build_picture_content(source, options, block)
|
28
|
-
image_options = options.fetch(:image, {})
|
29
|
-
image_options[:src] = build_img_src(source)
|
30
|
-
add_webp = options.fetch(:add_webp, false)
|
31
|
-
|
32
|
-
content = ''.html_safe
|
33
|
-
if block.present?
|
34
|
-
content << capture(&block).html_safe
|
35
|
-
else
|
36
|
-
[source].flatten.each do |img_src|
|
37
|
-
content << build_source_from_img(image_path(img_src), add_webp)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
content << tag('img', image_options)
|
41
|
-
end
|
42
|
-
|
43
|
-
def build_img_src(source)
|
44
|
-
case source
|
45
|
-
when String
|
46
|
-
image_path(source)
|
47
|
-
when Array
|
48
|
-
image_path(source.last)
|
49
|
-
else
|
50
|
-
''
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def build_source_from_img(img_path, add_webp)
|
55
|
-
source_tags = ''.html_safe
|
56
|
-
webp_path = "#{img_path}.webp"
|
57
|
-
# order of source tags matters
|
58
|
-
if add_webp && file_exist_in_public_path?(webp_path)
|
59
|
-
source_tags << source_tag(srcset: webp_path, type: 'image/webp')
|
60
|
-
end
|
61
|
-
source_tags + source_tag(
|
62
|
-
srcset: img_path,
|
63
|
-
type: image_type(img_path)
|
64
|
-
)
|
65
|
-
end
|
66
|
-
|
67
|
-
def image_type(image_path)
|
68
|
-
extension = File.extname(image_path)
|
69
|
-
Rack::Mime::MIME_TYPES.merge!({
|
70
|
-
'.webp' => 'image/webp'
|
71
|
-
})
|
72
|
-
Rack::Mime.mime_type(extension).to_s
|
73
|
-
end
|
74
|
-
|
75
|
-
def file_exist_in_public_path?(path)
|
76
|
-
# for performance reasons, we assume production contains the asset
|
77
|
-
return true if ::Rails.env.production?
|
78
|
-
|
79
|
-
public_path = File.join(
|
80
|
-
::Rails.root,
|
81
|
-
'public',
|
82
|
-
path
|
83
|
-
)
|
84
|
-
File.exist?(public_path)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|