Dynamic Image Watermarking Made Simple with imgproxy and Apache APISIX

Learn how to add dynamic watermarks to images using imgproxy and Apache APISIX, with a detailed step-by-step process and source code example.


This content originally appeared on HackerNoon and was authored by Nicolas Frankel

Last week, I described how to add a dynamic watermark to your images on the JVM. I didn't find any library, so I had to develop the feature, or, more precisely, an embryo of a feature, by myself. Depending on your tech stack, you must search for an existing library or roll up your sleeves. For example, Rust offers such an out-of-the-box library. Worse, this approach might be impossible to implement if you don't have access to the source image.

\ Another alternative is to use ready-made components, namely imgproxy and Apache APISIX. I already combined them to resize images on-the-fly.

\ Here's the general sequence flow of the process:

\

  • When APISIX receives a specific pattern, it calls imgproxy with the relevant parameters
  • imgproxy fetches the original image and the watermark to apply
  • It watermarks the original image and returns the result to APISIX

\ Let's say the pattern is /watermark/*.

We can define 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

\

  1. Catch-all route that forwards to the web server
  2. Watermark images route
  3. Rewrite the URL…
  4. …with an imgproxy-configured route and…
  5. …forward to imageproxy

\ You can find the exact rewritten URL syntax in imgproxy documentation. The watermark itself is configured via a single environment variable. You should buy imgproxy's Pro version if you need different watermarks. As a poor man's alternative, you could also set up different instances, each with its watermark, and configure APISIX to route the request to the desired instance.

\ In this post, we implemented a watermarking feature with the help of imgproxy. The more I think about it, the more I think they make a match made in Heaven.

\ The complete source code for this post can be found on GitHub:

https://github.com/ajavageek/watermark-on-the-fly?embedable=true


To go further:

\

\ Originally published at A Java Geek on July 7th, 2024

\


This content originally appeared on HackerNoon and was authored by Nicolas Frankel


Print Share Comment Cite Upload Translate Updates
APA

Nicolas Frankel | Sciencx (2024-07-11T12:16:55+00:00) Dynamic Image Watermarking Made Simple with imgproxy and Apache APISIX. Retrieved from https://www.scien.cx/2024/07/11/dynamic-image-watermarking-made-simple-with-imgproxy-and-apache-apisix/

MLA
" » Dynamic Image Watermarking Made Simple with imgproxy and Apache APISIX." Nicolas Frankel | Sciencx - Thursday July 11, 2024, https://www.scien.cx/2024/07/11/dynamic-image-watermarking-made-simple-with-imgproxy-and-apache-apisix/
HARVARD
Nicolas Frankel | Sciencx Thursday July 11, 2024 » Dynamic Image Watermarking Made Simple with imgproxy and Apache APISIX., viewed ,<https://www.scien.cx/2024/07/11/dynamic-image-watermarking-made-simple-with-imgproxy-and-apache-apisix/>
VANCOUVER
Nicolas Frankel | Sciencx - » Dynamic Image Watermarking Made Simple with imgproxy and Apache APISIX. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/11/dynamic-image-watermarking-made-simple-with-imgproxy-and-apache-apisix/
CHICAGO
" » Dynamic Image Watermarking Made Simple with imgproxy and Apache APISIX." Nicolas Frankel | Sciencx - Accessed . https://www.scien.cx/2024/07/11/dynamic-image-watermarking-made-simple-with-imgproxy-and-apache-apisix/
IEEE
" » Dynamic Image Watermarking Made Simple with imgproxy and Apache APISIX." Nicolas Frankel | Sciencx [Online]. Available: https://www.scien.cx/2024/07/11/dynamic-image-watermarking-made-simple-with-imgproxy-and-apache-apisix/. [Accessed: ]
rf:citation
» Dynamic Image Watermarking Made Simple with imgproxy and Apache APISIX | Nicolas Frankel | Sciencx | https://www.scien.cx/2024/07/11/dynamic-image-watermarking-made-simple-with-imgproxy-and-apache-apisix/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.