Skip to content
View as Markdown

Converter ​

@dicebear/converter turns an avatar into PNG, JPEG, WebP, or AVIF, in the browser and in Node.js.

Installation ​

sh
npm install @dicebear/converter

The converter doesn't need @dicebear/core. It's optimized for DiceBear, but it converts SVGs from other sources too.

Usage ​

js
import { toPng } from '@dicebear/converter';
import { Style, Avatar } from '@dicebear/core';
import lorelei from '@dicebear/styles/lorelei.json' with { type: 'json' };

const style = new Style(lorelei);
const avatar = new Avatar(style, { seed: 'Alice' });

const png = toPng(avatar, { size: 128 });

const dataUri = await png.toDataUri(); // for an <img> source or CSS
const buffer = await png.toArrayBuffer(); // for files or binary data

In Node.js, write the buffer to a file:

js
import { writeFile } from 'node:fs/promises';

await writeFile('avatar.png', Buffer.from(buffer));

Functions ​

FunctionFormatBrowserNode.js
toPng(svg, options?)PNGYesYes
toJpeg(svg, options?)JPEGYesYes
toWebp(svg, options?)WebPYes*Yes
toAvif(svg, options?)AVIFYes*Yes

Each function takes an SVG string or an object with a toString() method, such as an Avatar. The result has two methods: toDataUri() resolves to a data URI, and toArrayBuffer() to an ArrayBuffer.

* In the browser the conversion runs through an HTML canvas, so WebP and AVIF depend on the browser being able to export them. Where it can't, you get PNG. WebP works in all modern browsers, AVIF support varies. See caniuse.com and MDN for details.

Options ​

OptionTypeDefaultEnvironmentDescription
sizenumber512Browser + Node.jsWidth and height of the square output in pixels. Values above 2048 are clamped to 2048, invalid values (NaN, <= 0, Infinity) fall back to 512
fontsstring[][]Node.jsPaths to font files for styles that render text, such as initials. Without it, the system fonts are used
includeExifbooleanfalseNode.jsCopies the style title, source URL, creator, license, and copyright notice from the SVG into Exif fields, so license and attribution information stays with the image

Warning

includeExif uses an exiftool singleton that you have to end when your application exits. See the exiftool-vendored documentation for more information.

js
import { exiftool } from 'exiftool-vendored';

// When your application exits:
await exiftool.end();

The package is fully typed and exports the Options and Result types.

Rendering with resvg yourself ​

toPng and the other functions handle this for you. If you drive resvg directly instead, run the SVG through normalizeMaskType first:

js
import { normalizeMaskType } from '@dicebear/converter';

const svg = normalizeMaskType(avatar.toString());

resvg reads mask-type only as a presentation attribute, not from a style declaration. Figma writes the declaration, so official avatar styles carry masks that resvg would treat as the luminance default and render as fully hidden. normalizeMaskType mirrors the value onto the attribute. If nothing needs fixing, you get your input back unchanged. Otherwise the function re-emits the SVG from a parsed tree, so formatting details like quote style may change, while the rendered image stays the same. Browsers honor both forms, so this only matters when you rasterize.