Skip to content

Converts text as a string to an image containing that text.

License

Notifications You must be signed in to change notification settings

bostrom/text-to-image

Repository files navigation

Text to image

Code Coverage Npm Version License

A library for generating an image data URI representing an image containing the text of your choice.

Originally part of a Twitter bot for publishing tweets longer than 140 characters, the generator takes a string, an optional configuration object, and an optional callback function as arguments and produces an image data URI (data:image/png;base64,iVBORw0KGgoAAAA...).

Table of contents

Try it

Use this CodeSandbox to try out text-to-image in your browser.

Installation

npm i text-to-image

Note that text-to-image uses node-canvas to generate the images. For text-to-image to install, you might have to fulfill the installation requirements for node-canvas. Please refer to their documentation for instructions.

Function signature

The signature for both the syncronous and asyncronous functions is

(content: string, config?: GenerateOptions) => string | Promise<string>;

See below for documentation on the function arguments.

Usage

import { generate, generateSync } from 'text-to-image';

// using the asynchronous API with await
const dataUri = await generate('Lorem ipsum dolor sit amet');

// using the asynchronous API with .then
generate('Lorem ipsum dolor sit amet').then(function (dataUri) {
  // use the dataUri
});

// using the synchronous API
const dataUri = generateSync('Lorem ipsum dolor sit amet');

This is an example of a full dataUri generated by the above examples:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAAwCAYAAAA/1CyWAAAABmJLR0QA/wD/AP+gvaeTAAARHElEQVR4nO3dZ1BUVxsH8D/sIsUBWXoVWJSi6KKg4kbKgAUHJ3RUojEIohQxmhjRRI3GYBRXBR0sY4k4MZNYEj9Ek4wlmujYEcWSQLCCneYAUpbn/cBwX6/gvrDRoPM+vxlmuA/n3D3nnut97rll1SEiAmOMMdZFut3dAMYYY28nTiCMMca0wgmEMcaYVjiBMMYY0wonEMYYY1rhBMIYY0wrnEAYY4xphRMIY4wxrXACYYwxppUuJZDa2lpUVlaipaXldbWnW9XU1ODp06fd3Yx/RW1tLaqrq7tc7+nTp6ipqXkNLXo728HY/7MuJZC0tDSYmZnhzp07r6s93SogIAAhISHd3Yx/RXJyMgYNGtTlepGRkRg2bNhraFHXjBkzBkqlUli+f/8+zp8/j4aGhm5sVdc8ePAA58+fx7Nn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of your browser and you should see the generated image.

With the default options the image generator will adjust the height of the image automatically to fit all text, while the width of the image is fixed to the specified width.

Text formatting rules

Normal spaces are stripped from the beginning of paragraphs. To add leading spaces, either use tab or non-breaking space characters.

Line breaks can be added with \n.

Example:

import { generate } from 'text-to-image';

// Add indent as tabs
const tabbedText = await generate(
  '\tDonec id elit non mi porta gravida at eget metus. \n\tSed posuere consectetur est at lobortis.',
);

// Add indent as non-breaking spaces
const spacedText = await generate(
  '\xA0\xA0Donec id elit non mi porta gravida at eget metus. \n\xA0\xA0Sed posuere consectetur est at lobortis.',
);

Configuring (GenerateOptions)

The generate and generateSync functions take an optional second parameter containing configuration options for the image generation. All configuraion parameters are optional.

Note that the supplied configuration values are not validated. Invalid values may lead to unexpected results or the image not getting generated at all. For color value validations, consider using a library like validate-color before passing the value to this library.

The available options are as follows.

Name Type Default value Description
bgColor string | CanvasGradient | CanvasPattern #FFFFFF Sets the background color of the image. See CanvasRenderingContext2D.fillStyle for valid values, or use a color value validator (see note above).
customHeight number 0 Sets the height of the generated image in pixels. If falsy, will automatically calculate the height based on the amount of text.
extensions Array<Extension> [] An array of Extensions.
fontFamily string Helvetica The font family to use for the text in the image. See CSS font-family for valid values.
fontPath string The file system path to a font file to use, also specify fontFamily if you use this.
fontSize number 18 The font size to use for the text in the image. See CSS font-size for valid values.
fontWeight string | number normal The font weight to use for the text in the image. See CSS font-weight for valid values.
lineHeight number 28 The line height for the generated text.
margin number 10 The margin (all sides) between the text and the border of the image.
maxWidth number 400 Sets the width of the generated image in pixels.
textAlign string left The text alignment for the generated text. See CanvasRenderingContext2D.textAlign for valid values.
textColor string #000000 Sets the text color. See CanvasRenderingContext2D.fillStyle for valid values, or use a color value validator (see note above).
verticalAlign string top Use to set center height with customHeight (possible values: �top�, �center�).

Example:

import { generate } from 'text-to-image';

const dataUri = await generate('Lorem ipsum dolor sit amet', {
  maxWidth: 720,
  fontSize: 18,
  fontFamily: 'Arial',
  lineHeight: 30,
  margin: 5,
  bgColor: 'blue',
  textColor: 'red',
});

Extensions

Extensions are a form of middleware that can be used to produce side-effects or manipulate the image beyond the core functionality before the final data URL is formed and returned. An extension is a function that takes the Canvas instance and a ComputedOptions object (the original configuration object augmented with defaults for omitted values) as arguments and returns a Canvas.

(canvas: Canvas, conf: ComputedOptions) => Canvas | Promise<Canvas>;

Each extension will receive the canvas returned by the previous extension. The canvas returned by the last extension will be used when producing the final data URL.

Note that all extensions for generateSync must be synchronous, too. If your extension contains asyncronous code, use the async generate function instead.

The following example extension draws a border around the image 5px from the edge using the configured text color.

// All types from 'node-canvas' are re-exported for convenience
import { Canvas, ComputedOptions, generate } from 'text-to-image';

const makeBorder = (canvas: Canvas, conf: ComputedOptions) => {
  const { width, height } = canvas;
  const ctx = canvas.getContext('2d');
  ctx.strokeStyle = conf.textColor;
  ctx.strokeRect(5, 5, width - 10, height - 10);
  return canvas;
};

const uri = await generate('Lorem ipsum dolor sit amet', {
  textColor: 'green',
  extensions: [makeBorder],
});

If your extension needs to take some configuration parameters, then it's advisable to create an extension factory function. The following example extension factory takes the padding size as argument and returns an extension.

const makeBorder =
  (padding: number) => (canvas: Canvas, conf: ComputedOptions) => {
    const { width, height } = canvas;
    const ctx = canvas.getContext('2d');
    ctx.strokeStyle = conf.textColor;
    ctx.strokeRect(padding, padding, width - padding * 2, height - padding * 2);
    return canvas;
  };

const uri = await generate('Lorem ipsum dolor sit amet', {
  textColor: 'green',
  extensions: [makeBorder(2)],
});

Debugging with the fileWriter extension

Without configuration the fileWriter extension saves the current canvas as a PNG in the current working directory (where the process was started). The name of the image will be the current date and time. This facilitates debugging the image generation as the produced image can be viewed as an image file instead of a data URL.

import { generate } from 'text-to-image';
import fileWriter from 'text-to-image/extensions/fileWriter';

const dataUri = await generate('Lorem ipsum dolor sit amet', {
  textColor: 'red',
  extensions: [fileWriter()],
});

For more control over the file name and location, specify the fileName option to the fileWriter. The fileName can include path segments in addition to the file name. A relative path (or only a file name without path) will be resolved starting from the current working directory. Any missing parent directories to the file will be created as needed.

Example:

import path from 'path';
import { generate } from 'text-to-image';
import fileWriter from 'text-to-image/extensions/fileWriter';

const dataUri = await generate('Lorem ipsum dolor sit amet', {
  textColor: 'red',
  extensions: [
    fileWriter({
      fileName: path.join('some', 'custom', 'path', 'to', 'debug_file.png'),
    }),
  ],
});

This will create the debug file some/custom/path/to/debug_file.png in the current working directory.

Creating speech bubbles with the bubbleTail extension

The bubbleTail extension will draw a speech bubble "tail", a triangle at the bottom of the image. It takes the desired width and height of the tail as configuration parameters.

import { generate } from 'text-to-image';
import bubbleTail from 'text-to-image/extensions/bubbleTail';

const dataUri = await generate('Lorem ipsum dolor sit amet', {
  textColor: 'red',
  extensions: [bubbleTail({ width: 30, height: 20 })],
});

Typescript

For imports to work correctly with TypeScript, make sure you're using typescript version 4.7 or higher and have the following configuration in your tsconfig.json:

{
  "compilerOptions": {
    "module": "Node16",
    "moduleResolution": "Node16"
  }
}

See discussion in microsoft/TypeScript#33079 for more information.

Test

The library is tested using Jest. Run the test suit by executing

npm test

A coverage report will be generated in coverage/.

Contributing

Pull requests are welcome. Read the Contributing guidelines.

License

ISC License (ISC)