Skip to content

Commit fdc5ec2

Browse files
committed
Merge PR rails#50953
2 parents cd053bc + a15f5b8 commit fdc5ec2

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

activestorage/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Add `image/webp` to `config.active_storage.web_image_content_types` when `load_defaults "7.2"`
2+
is set.
3+
4+
*Lewis Buckley*
5+
16
* Fix JSON-encoding of `ActiveStorage::Filename` instances.
27

38
*Jonathan del Strother*
29.6 KB
Binary file not shown.

activestorage/test/models/variant_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase
114114
assert_equal 8, image.height
115115
end
116116

117+
test "resized variation of WEBP blob" do
118+
blob = create_file_blob(filename: "valley.webp")
119+
variant = blob.variant(resize_to_limit: [50, 50]).processed
120+
assert_match(/valley\.webp/, variant.url)
121+
122+
image = read_image(variant)
123+
assert_equal "WEBP", image.type
124+
assert_equal 50, image.width
125+
assert_equal 33, image.height
126+
end
127+
117128
test "optimized variation of GIF blob" do
118129
blob = create_file_blob(filename: "image.gif", content_type: "image/gif")
119130

guides/source/configuring.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Below are the default values associated with each target version. In cases of co
6060

6161
#### Default Values for Target Version 7.2
6262

63+
- [`config.active_storage.web_image_content_types`](#config-active-storage-web-image-content-types): `%w[image/png image/jpeg image/gif image/webp]`
6364
- [`config.active_record.validate_migration_timestamps`](#config-active-record-validate-migration-timestamps): `true`
6465

6566
#### Default Values for Target Version 7.1
@@ -2865,13 +2866,14 @@ config.active_storage.variable_content_types = %w(image/png image/gif image/jpeg
28652866

28662867
Accepts an array of strings regarded as web image content types in which
28672868
variants can be processed without being converted to the fallback PNG format.
2868-
If you want to use `WebP` or `AVIF` variants in your application you can add
2869-
`image/webp` or `image/avif` to this array.
2870-
By default, this is defined as:
2869+
For example, if you want to use `AVIF` variants in your application you can add
2870+
`image/avif` to this array.
28712871

2872-
```ruby
2873-
config.active_storage.web_image_content_types = %w(image/png image/jpeg image/gif)
2874-
```
2872+
The default value depends on the `config.load_defaults` target version:
2873+
| Starting with version | The default value is |
2874+
| --------------------- | ----------------------------------------------- |
2875+
| (original) | `%w(image/png image/jpeg image/gif)` |
2876+
| 7.2 | `%w(image/png image/jpeg image/gif image/webp)` |
28752877

28762878
#### `config.active_storage.content_types_to_serve_as_binary`
28772879

railties/lib/rails/application/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ def load_defaults(target_version)
324324
when "7.2"
325325
load_defaults "7.1"
326326

327+
if respond_to?(:active_storage)
328+
active_storage.web_image_content_types = %w( image/png image/jpeg image/gif image/webp )
329+
end
330+
327331
if respond_to?(:active_record)
328332
active_record.validate_migration_timestamps = true
329333
end

railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_2.rb.tt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
# Read the Guide for Upgrading Ruby on Rails for more info on each option.
1010
# https://guides.rubyonrails.org/upgrading_ruby_on_rails.html
1111

12+
###
13+
# Adds image/webp to the list of content types Active Storage considers as an image
14+
# Prevents automatic conversion to a fallback PNG, and assumes clients support WebP, as they support gif, jpeg, and png.
15+
# This is possible due to broad browser support for WebP, but older browsers and email clients may still not support
16+
# WebP. Requires imagemagick/libvips built with WebP support.
17+
#++
18+
# Rails.application.config.active_storage.web_image_content_types = %w[image/png image/jpeg image/gif image/webp]
19+
1220
###
1321
# Enable validation of migration timestamps. When set, an ActiveRecord::InvalidMigrationTimestampError
1422
# will be raised if the timestamp prefix for a migration is more than a day ahead of the timestamp

0 commit comments

Comments
 (0)