Frontend Development 16 min read

Designing a Scalable Template Algorithm for Automated Advertising Creative Generation

This article explains how to build an automated system that generates diverse advertising creative images by classifying size ratios, designing multi‑layer templates, selecting the best template through element structure, constraints, style and industry rules, and rendering them with Node.js, Puppeteer, and canvas.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Designing a Scalable Template Algorithm for Automated Advertising Creative Generation

Many small advertisers on the Sohu ad platform lack design resources, prompting the creation of a tool that generates creative ad images with minimal user input—just the ad title and product image.

The generation pipeline works as follows: the user submits required data, a Node.js service launches a headless Chrome instance via Puppeteer, which runs a JavaScript canvas program. The program selects the most suitable template based on the target dimensions, applies colors, backgrounds, and decorative elements, and finally renders the image on an HTML canvas. The rendered canvas is then screenshot‑captured by Puppeteer to produce the final image.

Ad image sizes are grouped into seven ratio categories (square, normal horizontal, middle horizontal, long horizontal, normal vertical, middle vertical, long vertical) using simple formulas such as Math.abs(width-height) < Math.min(width, height)/3 or width>height && width . Templates are designed for each ratio class.

Each template consists of three layers: background, canvas, and element layers. All positions and sizes are expressed as percentages of the final image width ( wrapWidth ) and height ( wrapHeight ) and use absolute positioning to ensure proportional scaling.

Template selection considers four factors: the composition of elements (e.g., 2 texts, 1 image, 1 button), element constraint rules, template style (e.g., fresh, classic), and template industry suitability. The algorithm first tries exact matches, then excludes unsuitable templates, and finally falls back to generic ones.

Element rendering is performed in three passes: (1) compute each element’s dimensions, font size, line height, etc.; (2) compute positional values (left, top) using previously calculated attributes; (3) compute additional properties such as maximum text rows, line‑height, and box model settings. Only after all calculations are complete is the actual rendering performed.

Special handling is described for text blanks (ensuring text fills its container without overflow) and ultra‑small fonts: when the required font size falls below 12 px, the system scales the entire layout, renders, then downsamples the image to the original size.

Images maintain aspect ratio by retrieving their original width ( originWidth ) and height ( originHeight ) and using these values in template calculations.

When themeMode is set to auto , the system extracts the dominant color from product images, clusters similar colors, and generates a coordinated color scheme (main, primary, secondary, and their opposite colors) for use throughout the template.

The color scheme, background selection, and decorative elements (both background and element decorations) are combined to produce a rich variety of templates. Massive template generation is achieved by mixing structural designs with different colors/backgrounds, automatically adapting a base template to the seven size ratios, and extracting reusable templates from existing ad images.

PuppeteerCanvasnodejsad-creativeauto-designtemplate-algorithm
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.