Skip to content

Commit 2a12b72

Browse files
JvHgeorgeclaghorn
JvH
authored andcommitted
Add web_image_content_types config option for ActiveStorage
Add `config.active_storage.web_image_content_types` to allow applications to add content types (like `image/webp`) in which variants can be processed, instead of letting those images be converted to the fallback PNG format.
1 parent 61d615c commit 2a12b72

File tree

6 files changed

+19
-6
lines changed

6 files changed

+19
-6
lines changed

activestorage/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Add `config.active_storage.web_image_content_types` to allow applications
2+
to add content types (like `image/webp`) in which variants can be processed,
3+
instead of letting those images be converted to the fallback PNG format.
4+
5+
*Jeroen van Haperen*
6+
17
* Add support for creating variants of `WebP` images out of the box.
28

39
*Dino Maric*

activestorage/app/models/active_storage/variant.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
# * {ImageProcessing::Vips}[https://github.com/janko-m/image_processing/blob/master/doc/vips.md#methods]
5454
# * {ruby-vips reference}[http://www.rubydoc.info/gems/ruby-vips/Vips/Image]
5555
class ActiveStorage::Variant
56-
WEB_IMAGE_CONTENT_TYPES = %w[ image/png image/jpeg image/jpg image/gif ]
57-
5856
attr_reader :blob, :variation
5957
delegate :service, to: :blob
6058

@@ -106,7 +104,7 @@ def process
106104

107105
def specification
108106
@specification ||=
109-
if WEB_IMAGE_CONTENT_TYPES.include?(blob.content_type)
107+
if ActiveStorage.web_image_content_types.include?(blob.content_type)
110108
Specification.new \
111109
filename: blob.filename,
112110
content_type: blob.content_type,

activestorage/app/models/active_storage/variant_with_record.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
class ActiveStorage::VariantWithRecord
4-
WEB_IMAGE_CONTENT_TYPES = %w[ image/png image/jpeg image/jpg image/gif ]
5-
64
attr_reader :blob, :variation
75

86
def initialize(blob, variation)
@@ -36,7 +34,7 @@ def url(http://github.com/**options)
3634
private
3735
def transform_blob
3836
blob.open do |input|
39-
if blob.content_type.in?(WEB_IMAGE_CONTENT_TYPES)
37+
if blob.content_type.in?(ActiveStorage.web_image_content_types)
4038
variation.transform(input) do |output|
4139
yield io: output, filename: blob.filename, content_type: blob.content_type, service_name: blob.service.name
4240
end

activestorage/lib/active_storage.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module ActiveStorage
5353
mattr_accessor :paths, default: {}
5454

5555
mattr_accessor :variable_content_types, default: []
56+
mattr_accessor :web_image_content_types, default: []
5657
mattr_accessor :binary_content_type, default: "application/octet-stream"
5758
mattr_accessor :content_types_to_serve_as_binary, default: []
5859
mattr_accessor :content_types_allowed_inline, default: []

activestorage/lib/active_storage/engine.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class Engine < Rails::Engine # :nodoc:
4141
image/webp
4242
)
4343

44+
config.active_storage.web_image_content_types = %w(
45+
image/png
46+
image/jpeg
47+
image/jpg
48+
image/gif
49+
)
50+
4451
config.active_storage.content_types_to_serve_as_binary = %w(
4552
text/html
4653
text/javascript
@@ -79,6 +86,7 @@ class Engine < Rails::Engine # :nodoc:
7986
ActiveStorage.draw_routes = app.config.active_storage.draw_routes != false
8087

8188
ActiveStorage.variable_content_types = app.config.active_storage.variable_content_types || []
89+
ActiveStorage.web_image_content_types = app.config.active_storage.web_image_content_types || []
8290
ActiveStorage.content_types_to_serve_as_binary = app.config.active_storage.content_types_to_serve_as_binary || []
8391
ActiveStorage.service_urls_expire_in = app.config.active_storage.service_urls_expire_in || 5.minutes
8492
ActiveStorage.content_types_allowed_inline = app.config.active_storage.content_types_allowed_inline || []

guides/source/configuring.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,8 @@ You can find more detailed configuration options in the
902902

903903
* `config.active_storage.variable_content_types` accepts an array of strings indicating the content types that Active Storage can transform through ImageMagick. The default is `%w(image/png image/gif image/jpg image/jpeg image/pjpeg image/tiff image/bmp image/vnd.adobe.photoshop image/vnd.microsoft.icon image/webp)`.
904904

905+
* `config.active_storage.web_image_content_types` 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` variants in your application you can add `image/webp` to this array. The default is `%w(image/png image/jpeg image/jpg image/gif)`.
906+
905907
* `config.active_storage.content_types_to_serve_as_binary` accepts an array of strings indicating the content types that Active Storage will always serve as an attachment, rather than inline. The default is `%w(text/html
906908
text/javascript image/svg+xml application/postscript application/x-shockwave-flash text/xml application/xml application/xhtml+xml application/mathml+xml text/cache-manifest)`.
907909

0 commit comments

Comments
 (0)