Out of the box, Active Storage supports a number of transformations, through the image_processing gem. This gem provides a common interface to both MiniMagick (the old standard) and ruby-vips (the new default).
I recently needed the ability to lighten images uploaded via Active Storage (for use as watermarks), but there isn’t a built-in method to do this through the transformations interface. However, I discovered that you can register your own custom ImageProcessing methods like this. (Thanks to this blog post for the approach.)
ImageProcessing
require 'image_processing' module ImageProcessing::Vips::CustomProcessing extend ActiveSupport::Concern included do def lighten(percentage) overlay = image.new_from_image([255, 255, 255, (255 * percentage).to_i]) image.composite(overlay, :over) end end end
Create a custom module to be included in the processor, adding a lighten method. The image is an accumulator available to all methods in the processing pipeline. The new_from_image method creates a new image of the same dimensions as the original, setting every pixel to RGB value (255, 255, 255) or white, with an opacity set to the percentage argument. Then this overlay image is “composited” over the original image with the composite method.
lighten
image
new_from_image
composite