Nikolay's Blog

Rails ActiveStorage add watermark to image

Cover Image for Rails ActiveStorage add watermark to image
  • Created at:

Draw transparent watermark image with mini magic

WATERMARK_PATH = Rails.root.join('app/javascript/images/watermark.png')

variant = product.attachment.variant(
  resize_to_fit: [380, nil],
  gravity: 'center',
  draw: "image Over 0,0 0,0 \"#{WATERMARK_PATH}\""
)

url_for(variant) # to get url
image_tag(variant) # to get <img> tag

And then copy watermark.png image to app/javascript/images/watermark.png

You will get the following result:

Image with watermark