Skip to content

Commit

Permalink
Add webp as a new framework default image type
Browse files Browse the repository at this point in the history
Follows rails#38918 and
rails#38988

At the time, webp browser support was limited. Now 96% of browsers
support webp: https://caniuse.com/?search=webp
  • Loading branch information
lewispb committed Feb 2, 2024
1 parent 3d132b8 commit 3df408b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions activestorage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Add `image/webp` to `config.active_storage.web_image_content_types` when `load_defaults "7.2"`
is set.

*Lewis Buckley*

* Fix N+1 query when fetching preview images for non-image assets

*Aaron Patterson & Justin Searls*
Expand Down
Binary file added activestorage/test/fixtures/files/valley.webp
Binary file not shown.
11 changes: 11 additions & 0 deletions activestorage/test/models/variant_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase
assert_equal 8, image.height
end

test "resized variation of WEBP blob" do
blob = create_file_blob(filename: "valley.webp")
variant = blob.variant(resize_to_limit: [50, 50]).processed
assert_match(/valley\.webp/, variant.url)

image = read_image(variant)
assert_equal "WEBP", image.type
assert_equal 50, image.width
assert_equal 33, image.height
end

test "optimized variation of GIF blob" do
blob = create_file_blob(filename: "image.gif", content_type: "image/gif")

Expand Down
17 changes: 11 additions & 6 deletions guides/source/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ NOTE: If you need to apply configuration directly to a class, use a [lazy load h

Below are the default values associated with each target version. In cases of conflicting values, newer versions take precedence over older versions.

#### Default Values for Target Version 7.2

- [`config.active_storage.web_image_content_types`](#config-active-storage-web-image-content-types): `%w[image/png image/jpeg image/gif image/webp]`

#### Default Values for Target Version 7.1

- [`config.action_dispatch.debug_exception_log_level`](#config-action-dispatch-debug-exception-log-level): `:error`
Expand Down Expand Up @@ -2847,13 +2851,14 @@ config.active_storage.variable_content_types = %w(image/png image/gif image/jpeg

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

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

#### `config.active_storage.content_types_to_serve_as_binary`

Expand Down
4 changes: 4 additions & 0 deletions railties/lib/rails/application/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ def load_defaults(target_version)
end
when "7.2"
load_defaults "7.1"

if respond_to?(:active_storage)
active_storage.web_image_content_types = %w( image/png image/jpeg image/gif image/webp )
end
else
raise "Unknown version #{target_version.to_s.inspect}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
#
# Read the Guide for Upgrading Ruby on Rails for more info on each option.
# https://guides.rubyonrails.org/upgrading_ruby_on_rails.html

# Adds webp to the list of content types Active Storage considers as an image
# Prevents automatic conversion to a fallback PNG, and assumes clients support webp, as they support gif, jpeg, and png.
# This is possible due to broad browser support for webp, but older browsers and email clients may still not support
# webp.
# Rails.application.config.active_storage.web_image_content_types = %w[image/png image/jpeg image/gif image/webp]

0 comments on commit 3df408b

Please sign in to comment.