Add watermark in an image

Hello,

What’s the best way to add a watermark to an image using Shrine ?
I checked if a plugin exists but apparently no.

Surely, I need to use MiniMagick directly. What is teh best place in my uploader to do this ?

Thanks,
Alexandre

The ImageProcessing gem supports watermarking, see this wiki page.

Where to put it in your uploader depends on whether you’re processing up front (derivatives plugin) or on-the-fly (derivation_endpoint plugin). With on-the-fly it’s the same as thumbnail generation, while with up front you can create a separate processor, e.g:

class ImageUploader < Shrine
  Attacher.derivatives_processor :watermark do |original|
    magick    = ImageProcessing::MiniMagick.source(original)
    watermark = ...

    watermarked = magick.composite! watermark,
      mode:    "over",        # 
      gravity: "south-east",  # for example
      offset:  [10, 10],      #

    { watermarked: watermarked }
  end
end
photo.image_derivatives!(:watermark)
photo.image_url(:watermarked) #=> "..."

If the watermark file is dynamic, you can pass it into the processor from the outside:

class ImageUploader < Shrine
  Attacher.derivatives_processor do |original, watermark:|
    # ...
  end
end
photo.image_derivatives!(:watermark, watermark: watermark_file)

Hi Janko,

Thanks for the help and links.
I’m currently using Shrine 2.x, but it works for me too.
I posted the code is someone is interested, when adding the watermark to only one version of the image.

watermark = Rails.root.join('app', 'assets', 'images', 'ui', 'logos', 'logo-overlay.png')
versions[:original] = pipeline.composite(watermark,    
    								mode: "over",          # Apply the watermark 'over' the source (default)
    								gravity: "south-east", # Apply the watermark in the bottom right corner
    								offset: [10, 10],      # Apply the watermark 10 pixels up and left from ':gravity' setting
  									).quality(90).resize_to_fit!(640, 960)