Golang Image Generator in runtime (favicon, watermark, resizing images).

Roman Romadin
ITNEXT
Published in
4 min readNov 8, 2020

--

Using Go you can create the image files in runtime. Find the small list below to observe the cases where it might be useful.

  1. Favicon
  2. Pixel-tracker
  3. Placeholder
  4. Watermark
  5. Image resizing

1. Favicon

Often Go applications are considered as a server part for delivering content for internal and/or external services. It is good practice to have an external program (wrapper) that returns the response — a kind of getaway to give only the desired payload and, in particular, without web-specific requests. Under web-specific requests from the client to the server-side, I mean favicon requests and various manifests, which are usually considered more as “front-end” types of resources. That is, in most cases, the same specialists who do the “front-part” of the project and responsible for their work.

But what if your Go app is also able to give out favicons and manifests on its own. And not just take a static favicon and manifest file but generate/create these byte arrays in runtime. This will allow our application to not depend on these static files.

Of course you can send a static file in response:

But we want to create a favicon dynamically. There are at least two similar ways to dynamically create a favicon:

  • The first method
  • The second method

2. Pixel-tracker

Pixel tracker is widely used for traffic arbitration.

3. Placeholder

An example of using placeholder

Placeholders example

To generate placeholders, we will use the same approach as for fixed-size images, but with the ability to add text and change the size of the image and font.

First, let’s define the requirements:

  • We receive a request for input in the form:
/WIDTH/HEIGHT/COLOR/TEXT/TEXT_COLOR/FONT_SIZEFor example: 
/600/200/622E68/Placeholder (stub)/FFFFFF/60
  • The text should be placed in the middle of the picture.
  • The text comes in the request from the client or going to the format: “WIDTH x HEIGHT”.
  • The text must be latin and cyrillic script
  • We provide default parameters
  • We output the bytes of pictures

First of all let’s make a color parsing package. In the incoming request, color is a string in hexadecimal notation. Other “color formats” are not expected.

The image package below:

The result of the request to address (url)

http://localhost:8080/600/200/622E68/Placeholder (заглушка)/FFFFFF
Placeholder (заглушка)

And for such request:

http://localhost:8080/480/200
placeholder with default text

4. Watermark

Watermark may be useful for such cases:

  • to indicate the copyright holder on the published image
  • you can add advertising text to the image
  • purpose of the image and its restrictions for use

For example, specify that this image or photo can only be used for certain purposes and / or for a limited period of time.

5. Resizing images

In particular the platform needs to store a set of images in various resizings and formats.

Sometimes you are required to change the set of the sizes. In this case, you need to resize new dimensions and possibly delete others.

There are two possible scenarios for implementing resizing:

  • one-time (resizing for the entire set on demand)
  • dynamic (resizing in runtime)

One-time resizing has an unbearable advantage — we resize all or part of the images once (for example, once a day). In this case, we are demanding disk space. Fast disks are closer to the client and hot data (images).

Dynamic image resizing allows you to free up space for all the “resizing” and not worry about their relevance. But in this case, we are demanding on computing resources and the resizing “caching” layer can be “shifted” closer to the client and the set of sizes can be updated at any time.

In other words, the ideal option (for resources) is hybrid resizing: we select data that needs to be resized dynamically or one-time. Also, if you often change the size and format of images, dynamic resizing is more suitable for you (if you have computing resources). I would recommend making calculations for your planned requirements and capacities.

Note

All code examples are provided for informational purposes. Accordingly, you need to test and adapt this approach to your requirements and goals.

--

--