h2non / imaginary

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing
https://fly.io/docs/app-guides/run-a-global-image-service/
MIT License
5.57k stars 456 forks source link

Watermark Image with replication #323

Open Redmega opened 4 years ago

Redmega commented 4 years ago

Is there a way to achieve replication with a watermark image? I've been searching but have yet to find anything.

These are the two solutions I came up with:

  1. Use the watermark image dimensions and the target image dimensions to calculate how many times the watermark fits, and their positions, then send a pipeline request to Imaginary with the watermark actions for each position.
  2. Resize the watermark image dynamically using mirror to overlay on the target.

1 takes some dev effort and the limit of 10 pipeline actions would certainly get in the way. 2 would take a lot of bandwidth as Imaginary only takes a URL for the watermark image, so we'd need to send the image to s3 or some other service to get a url for Imaginary. Since each watermark would then be specific for a certain image, it wouldn't really be reusable so it would have to be deleted.

If it's supported by the built-in API, we could say that /watermarkimage has noreplicate: true by default, so it's not a breaking change. Setting it to false would "stamp" the image with the watermarked image across the entire image.

Eptarch commented 4 years ago

I think our case may provide the third solution.

We have images with known width (1200 px), but unknown height (anywhere between 500 and 3600 px) and it needs to be watermarked with replicated watermark (say, 50 by 150 px) with diagonal text. After tinkering with Imaginary I tried creating large 1200 by 3600 px watermark image, and shifted origin to -25:-75 and applying that giant watermark to original image. It turned out alright - the image remains within its original dimensions.

No need to use pipeline with 10 operations limit that way.