Created
May 26, 2023 22:06
-
-
Save michaelkitson/a34b6e982fda80b7f5ee3c1134520798 to your computer and use it in GitHub Desktop.
A quick and dirty sprockets extension example to transform images between formats.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/sprockets.rb | |
class ImageTransformer | |
def call(input) | |
filename, content_type = input.values_at(:filename, :content_type) | |
new_extension = content_type.split('/').last | |
converted = ImageProcessing::Vips.source(filename).convert!(new_extension) | |
{ data: converted.read, charset: nil } | |
ensure | |
converted&.unlink | |
end | |
end | |
Sprockets.register_transformer('image/jpeg', 'image/webp', ImageTransformer.new) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment