Navigate back to the homepage

Two Approaches of Dynamically Generating Images with Azure Functions

John Kilmister
April 23rd, 2022 · 3 min read

I recently started looking into how to generate images using JavaScript Azure functions on a consumption plan. This is something that until recently has been a challenge as in the past supporting libraries have had dependencies that needed to be pre-installed on servers.

I found two solutions that offered two very different approaches to solve the same problem. In this post I want to take you through both approaches with the pros and cons of each and why you might want to use one over the other.

HTML to Image

I initially came across a blog post by Thomas Pentenrieder on generating dynamic images with azure functions.

This approach uses and NPM package called node-html-to-image. The package wraps puppeteer which in turn is automating a chromium browser loading in the provided HTML and taking a screenshot saving it out as an image. The NPM package also supports templating through handlebars JS making it easy to create dynamic templates.

This is something that has recently been made possible on Azure consumption plan functions through the addition of some dependencies being added on the Azure servers. I am not going to cover any of the code or setup needed here, for that I suggest you read Thomas is great blog post on the topic.

In Thomas’s post he is generating banner images, however as its rendering HTML you could generate images from a HTML canvas (for complex drawing or charts), render a map or use more extreme CSS3 to apply text effects depending on your needs. Basically, anything that will render in the browser can be captured.

To try this out I created a dynamic sticker image with a google font, SVG text effects and a transparent background. The code is on GitHub and I have also talked over how this works in Live Code session on twitch.

Sample Generated Sticker

The downside to this approach is that it will only work functions running on a Linux server, there’s a little bit of extra setup you need for your project and in my sample, I found it took between 3-5 seconds to render the image. The time to render is not a surprise as it is opening up a browser, rending the HTML then saving out the screenshot. The only small challenge I found to running this was that locally on a Mac we also needed to install chromium to get it to work locally, on windows I had no issues.

Image Generation Library

The Jimp NPM package is a fully node-based image manipulation library with no external dependencies. The focus of this package is very different to that of node-html-to-image. You start with a base image, in either a URL or image buffer then a set of methods allow you to change the image, resizing it, changing the colour, adding text, overlay other images etc. During this entire time, you are working with images and pixels rather than HTML Markup.

I could imagine this would work well for projects where you are creating dynamic gravatar like images or needed to dynamically resize images on demand.

To show how this works I created a typescript code sample that takes a simple image of a cat from an API that we then overlay a single line of text on to.

1import { AzureFunction, Context, HttpRequest } from "@azure/functions"
2import * as Jimp from 'jimp';
3import fetch from 'node-fetch';
5const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
7 const urlResp = await fetch("", {
8 method: 'GET'
9 });
11 const catData = await urlResp.json();
12 const imgPath = catData[0].url;
14 const font = await Jimp.loadFont(Jimp.FONT_SANS_32_WHITE);
15 const text = req.query.text ?? '';
17 const newImageBytes = await => {
18 return img
19 .print(
20 font,
21 0,
22 0, {
23 text: text,
26 },
27 catData[0].width,
28 catData[0].height
29 )
30 .getBufferAsync(Jimp.MIME_JPEG);
31 });
33 context.res = {
34 headers:{"content-type":"image/jpg"},
35 body: newImageBytes
36 };
39export default httpTrigger;

This sample when run renders an image in under 2 seconds although most of the time is the API call to get cat image. If we use a local image, then the render time is about half a second. The full sample can be found on GitHub.

Sample Cat Image

The compromise to this approach is that working with text is more challenging. Fonts are effectively images themselves using BMFont format rather than flexible true type fonts. This means it’s not as easy to change the colour or styling and applying text effects such as drop shadow is virtually impossible.


In this post we looked at two different approaches to generate images inside Azure functions.

Using node image to HTML allows us lots of flexibility generating images from any HTML source including a wide range of fonts colours and content. There are however compromises such as performance.

In comparison using the Jimp library we can quickly generate and combine images. Fonts however, and more complex arrangements will be more challenging.

Generating great looking images is very possible in a cost-effective way using Azure Functions on a consumption plan. The approach will come down to your use case.

Title Photo by pine watt on Unsplash

If you enjoyed reading please share...

More articles from John kilmister

51 Links and Resources for Technical Teams, Developers, Managers and Architects

Over the years I have collected a number of useful links that had been shared to me by colleagues, found on twitter, in newsletters or just…

April 3rd, 2022 · 2 min read

How to Separate Production, Test and Development Resources in Azure

When following the best practice of least privilege access, separating our production from our development and test resources is essential…

March 14th, 2022 · 4 min read
Link to $ to $ to $ to $