Final week, I described add a dynamic watermark to your pictures on the JVM. I did not discover any library, so I needed to develop the function, or, extra exactly, an embryo of a function, on my own. Relying in your tech stack, you need to seek for an present library or roll up your sleeves. For instance, Rust provides such an out-of-the-box library. Worse, this strategy may be unattainable to implement if you do not have entry to the supply picture.
One other different is to make use of ready-made elements, particularly imgproxy and Apache APISIX. I already mixed them to resize pictures on-the-fly.
Here is the overall sequence stream of the method:
- When APISIX receives a selected sample, it calls
imgproxy
with the related parameters imgproxy
fetches the unique picture and the watermark to use- It watermarks the unique picture and returns the consequence to APISIX
As an example the sample is /watermark/*
.
We will outline two routes:
routes:
- uri: "*" #1
upstream:
nodes:
"server:3000": 1
- uri: /watermark/* #2
plugins:
proxy-rewrite: #3
regex_uri:
- /watermark/(.*)
- /dummy_sig/watermark:0.8:nowe:20:20:0.2/plain/http://server:3000/$1 #4
upstream:
nodes:
"imgproxy:8080": 1 #5
- Catch-all route that forwards to the net server
- Watermark pictures route
- Rewrite the URL…
- …with an
imgproxy
-configured route and… - …ahead to
imageproxy
You’ll find the precise rewritten URL syntax in imgproxy documentation. The watermark itself is configured through a single surroundings variable. You can purchase imgproxy
‘s Professional model should you want completely different watermarks. As a poor man’s different, you would additionally arrange completely different situations, every with its watermark, and configure APISIX to route the request to the specified occasion.
On this put up, we carried out a watermarking function with the assistance of imgproxy
. The extra I give it some thought, the extra I feel they make a match made in Heaven.
The entire supply code for this put up might be discovered on GitHub.