next_gen_images 1.1.1 → 1.2.0

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: 0f92dcb3e7a5dc2c7e613431683190de72b6aea0b2044b6a216ab40185659155
4
- data.tar.gz: ecd61c0cce652c8f8d2cdfd6185056df9252d545741e2cdd5deb44ca9139954e
3
+ metadata.gz: f7b22e97a51be91471701809cebd26b182e03fc339072ddb89294785126bb5ba
4
+ data.tar.gz: 7fbd8e00f2b425087f0088d04f6500e69a8d8f8264b9a9d6afdb557f91da69fb
5
5
  SHA512:
6
- metadata.gz: eb48b10ae8d6b9d8b02813436e433f1574619d45a8c1b623174df1063ecbd42e96d08204539d497413ad9c6351c28e172e688df590afaf27b4d39d381b2d219a
7
- data.tar.gz: a434d1d4d118cc4f8547c328d806210c98e6ff9981901da407fa06e9bbdd59c9e9fc85b8d11f595f2ad9a74048b327d6fc3b28f5ea257f9fcab2be8056d190cf
6
+ metadata.gz: 03e7d38f6e089c2cbb7c0b0980519f9ad92f2351b8726b3df42a50580653afbc5f1ad9448295d911dec9bbcd7bcbc5130bf3d3aebce76a46203e89fb964862da
7
+ data.tar.gz: 92638be75bf7b5892d5472d3c6dc0bdb067184bf80e03b0967802afc8844ffaee2bc626986d6da0db76b3a8b4874961f9dba2e69e03e4382690b03d703357659
data/Gemfile.lock CHANGED
@@ -1,108 +1,126 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- next_gen_images (1.1.1)
5
- rails (>= 3.1)
6
- webp-ffi (~> 0.3.1)
4
+ next_gen_images (1.2.0)
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.0.4)
12
- actionpack (= 7.0.4)
13
- activesupport (= 7.0.4)
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
- actionmailbox (7.0.4)
17
- actionpack (= 7.0.4)
18
- activejob (= 7.0.4)
19
- activerecord (= 7.0.4)
20
- activestorage (= 7.0.4)
21
- activesupport (= 7.0.4)
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.0.4)
27
- actionpack (= 7.0.4)
28
- actionview (= 7.0.4)
29
- activejob (= 7.0.4)
30
- activesupport (= 7.0.4)
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.0)
36
- actionpack (7.0.4)
37
- actionview (= 7.0.4)
38
- activesupport (= 7.0.4)
39
- rack (~> 2.0, >= 2.2.0)
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.0)
42
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
43
- actiontext (7.0.4)
44
- actionpack (= 7.0.4)
45
- activerecord (= 7.0.4)
46
- activestorage (= 7.0.4)
47
- activesupport (= 7.0.4)
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.0.4)
51
- activesupport (= 7.0.4)
54
+ actionview (7.1.4)
55
+ activesupport (= 7.1.4)
52
56
  builder (~> 3.1)
53
- erubi (~> 1.4)
54
- rails-dom-testing (~> 2.0)
55
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
56
- activejob (7.0.4)
57
- activesupport (= 7.0.4)
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.0.4)
60
- activesupport (= 7.0.4)
61
- activerecord (7.0.4)
62
- activemodel (= 7.0.4)
63
- activesupport (= 7.0.4)
64
- activestorage (7.0.4)
65
- actionpack (= 7.0.4)
66
- activejob (= 7.0.4)
67
- activerecord (= 7.0.4)
68
- activesupport (= 7.0.4)
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
- mini_mime (>= 1.1.0)
71
- activesupport (7.0.4)
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.4)
77
- public_suffix (>= 2.0.2, < 6.0)
85
+ addressable (2.8.7)
86
+ public_suffix (>= 2.0.2, < 7.0)
78
87
  ast (2.4.2)
79
- builder (3.2.4)
80
- carrierwave (2.2.3)
81
- activemodel (>= 5.0.0)
82
- activesupport (>= 5.0.0)
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.1.10)
98
+ concurrent-ruby (1.3.4)
99
+ connection_pool (2.4.1)
89
100
  crass (1.0.6)
90
- date (3.3.3)
91
- diff-lcs (1.5.0)
92
- erubi (1.12.0)
93
- ffi (1.15.5)
94
- ffi-compiler (1.0.1)
95
- ffi (>= 1.0.0)
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.0)
98
- activesupport (>= 5.0)
99
- i18n (1.12.0)
109
+ globalid (1.2.1)
110
+ activesupport (>= 6.1)
111
+ i18n (1.14.5)
100
112
  concurrent-ruby (~> 1.0)
101
- image_processing (1.12.2)
113
+ image_processing (1.13.0)
102
114
  mini_magick (>= 4.9.5, < 5)
103
115
  ruby-vips (>= 2.0.17, < 3)
104
- json (2.6.2)
105
- loofah (2.21.3)
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.2)
114
- method_source (1.0.0)
115
- mini_magick (4.12.0)
116
- mini_mime (1.1.2)
117
- minitest (5.16.3)
118
- net-imap (0.3.4)
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.1)
141
+ net-protocol (0.2.2)
124
142
  timeout
125
- net-smtp (0.3.3)
143
+ net-smtp (0.5.0)
126
144
  net-protocol
127
- nio4r (2.5.9)
128
- nokogiri (1.15.2-arm64-darwin)
145
+ nio4r (2.7.3)
146
+ nokogiri (1.16.7-arm64-darwin)
129
147
  racc (~> 1.4)
130
- parallel (1.22.1)
131
- parser (3.1.2.1)
148
+ parallel (1.26.3)
149
+ parser (3.3.5.0)
132
150
  ast (~> 2.4.1)
133
- public_suffix (5.0.1)
134
- racc (1.6.2)
135
- rack (2.2.7)
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
- rails (7.0.4)
139
- actioncable (= 7.0.4)
140
- actionmailbox (= 7.0.4)
141
- actionmailer (= 7.0.4)
142
- actionpack (= 7.0.4)
143
- actiontext (= 7.0.4)
144
- actionview (= 7.0.4)
145
- activejob (= 7.0.4)
146
- activemodel (= 7.0.4)
147
- activerecord (= 7.0.4)
148
- activestorage (= 7.0.4)
149
- activesupport (= 7.0.4)
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.0.4)
152
- rails-dom-testing (2.0.3)
153
- activesupport (>= 4.2.0)
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.5.0)
156
- loofah (~> 2.19, >= 2.19.1)
157
- railties (7.0.4)
158
- actionpack (= 7.0.4)
159
- activesupport (= 7.0.4)
160
- method_source
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.5)
191
+ thor (~> 1.0, >= 1.2.2)
192
+ zeitwerk (~> 2.6)
164
193
  rainbow (3.1.1)
165
- rake (13.0.6)
166
- regexp_parser (2.6.0)
167
- rexml (3.2.5)
168
- rspec (3.12.0)
169
- rspec-core (~> 3.12.0)
170
- rspec-expectations (~> 3.12.0)
171
- rspec-mocks (~> 3.12.0)
172
- rspec-core (3.12.0)
173
- rspec-support (~> 3.12.0)
174
- rspec-expectations (3.12.0)
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.12.0)
177
- rspec-mocks (3.12.0)
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.12.0)
180
- rspec-support (3.12.0)
181
- rubocop (1.38.0)
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.1.2.1)
217
+ parser (>= 3.3.0.2)
185
218
  rainbow (>= 2.2.2, < 4.0)
186
- regexp_parser (>= 1.8, < 3.0)
187
- rexml (>= 3.2.5, < 4.0)
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 (>= 1.4.0, < 3.0)
191
- rubocop-ast (1.23.0)
192
- parser (>= 3.1.1.0)
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.11.0)
196
- ruby-vips (2.1.4)
227
+ ruby-progressbar (1.13.0)
228
+ ruby-vips (2.2.2)
197
229
  ffi (~> 1.12)
198
- ssrf_filter (1.1.1)
199
- thor (1.2.2)
200
- timeout (0.3.2)
201
- tzinfo (2.0.5)
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.3.0)
204
- webp-ffi (0.3.1)
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
- websocket-driver (0.7.5)
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.8)
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
- Currently Rails does not provide a `picture` HTML tag helper. An HTML `picture` tag provides [several advantages](https://blog.bitsrc.io/why-you-should-use-picture-tag-instead-of-img-tag-b9841e86bf8b) over an image tag, like fallback image support, resolution switching and art direction.
6
- Also, most of a website assets are still JPEG or PNG images, and we don't want to convert our assets manually to WebP, we want this to happen automatically if it's needed.
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 brings next gen image formats to Ruby on Rails. A `picture_tag` view helper is provided along several utilities to automatically convert them. Currently, it supports `WebP` images. A Carrierwave module with some utility methods is provided.
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
- ### View Helpers
34
+ ### Asset conversion to public/assets for production usage
34
35
 
35
- This gem adds support for a `picture_tag` helper. Syntax is the following:
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
- picture_tag 'satellite.png', image: { alt: 'satellite image', class: 'mt-0' }, add_webp: true
54
- ```
55
- This will create the following HTML:
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
- This will create the following HTML:
70
- ```html
71
- <picture>
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
- 3. The last option is to provide a block that will be inserted directly into the picture tag. This can be extremely useful for art direction.
51
+ ### Asset conversion of /app/assets/images directory
79
52
 
80
- ```ruby
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 assets:webp
56
+ rake next_gen_images:convert_app_images
104
57
  ```
105
- Assets will be compiled to `public/assets/` or the path that you specifed in `Rails.application.config.assets.prefix`.
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", "~> 1.2"
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
@@ -2,9 +2,9 @@
2
2
 
3
3
  require 'webp-ffi'
4
4
 
5
- namespace :assets do
5
+ namespace :next_gen_images do # rubocop:disable Metrics/BlockLength
6
6
  desc 'Create .webp versions of assets'
7
- task webp: :environment do
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
- # Hook into existing assets:precompile task
35
- Rake::Task['assets:precompile'].enhance do
36
- # TODO: if defined?(NextGenImages) && NextGenImages.config.run_on_precompile
37
- Rake::Task['assets:webp'].invoke
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NextGenImages
4
- VERSION = '1.1.1'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -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'
@@ -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.add_runtime_dependency 'rails', '>= 3.1'
35
- spec.add_runtime_dependency 'webp-ffi', '~> 0.3.1'
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.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Balarini
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-31 00:00:00.000000000 Z
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: '3.1'
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: '3.1'
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.3.1
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.3.1
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