Skip to content

Instantly share code, notes, and snippets.

@michaelkitson
Created May 26, 2023 22:06
Show Gist options
  • Save michaelkitson/a34b6e982fda80b7f5ee3c1134520798 to your computer and use it in GitHub Desktop.
Save michaelkitson/a34b6e982fda80b7f5ee3c1134520798 to your computer and use it in GitHub Desktop.
A quick and dirty sprockets extension example to transform images between formats.
# 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