ImageMagick in the browser: the JavaScript library

ImageMagick in the browser (magick-wasm): encode to 100+ formats, decode HEIC, TIFF, PSD and others, generate backgrounds, patterns and gradients, make PDF and SVG. The engine loads on the first call.

  • @openlibsdev/image-forge
  • v1.0.0
  • ES module
  • MIT

Installation

With a bundler or Node-style imports:

npm install @openlibsdev/image-forge
import ImageForge from "@openlibsdev/image-forge";

Or straight in a page, without a build step (jsDelivr resolves the dependencies):

<script type="module">
import ImageForge from "https://cdn.jsdelivr.net/npm/@openlibsdev/image-forge@1.0.0/+esm";
</script>

Usage

import ImageForge from "@openlibsdev/image-forge";

const url = await ImageForge.generateDataUrl("gradient", "#0d6efd-#20c997", 320, 200);
const bytes = await ImageForge.encode(url, "webp", { quality: 80 });   // Uint8Array
console.log(ImageForge.mimeFor("webp"), bytes.length);

API

ImageForge.encode(input, format, opts)

Converts an image (data URL, Blob, bytes) to a format and returns bytes.

ImageForge.exportFile(input, format, filename, opts)

Converts and downloads a file.

ImageForge.decodeImage(input)

Decodes any supported image to a PNG data URL.

ImageForge.generate(format, arg, width, height, opts)

Generates a picture: gradient, pattern, plasma, text and others.

ImageForge.generateDataUrl(format, arg, width, height, opts)

The same, as a data URL.

ImageForge.toPDF(image, width, height, opts)

Puts an image on a PDF page.

ImageForge.toSVG(imageData, opts)

Traces ImageData into SVG.

ImageForge.listExportFormats()

Formats it can write.

ImageForge.mimeFor(format)

MIME type of a format.

ImageForge.isNativeOnly(format)

Whether a format is better made by the browser (SVG, PDF).

ImageForge.ensureMagick()

Loads the engine in advance.

ImageForge.setUrls(o)

Sets the addresses of the engine files.

ImageForge.PATTERN_NAMES

Names of the built-in patterns.

ImageForge.GEN_COLOR1_FORMATS

Generators that take one color (xc, canvas).

ImageForge.GEN_COLOR2_FORMATS

Generators that take two colors (gradient, radial-gradient).

ImageForge.GEN_PATTERN_FORMATS

Generators that take a pattern name (pattern).

ImageForge.GEN_TEXT_FORMATS

Generators that draw text and need a registered font (caption, label, pango).

ImageForge.NAMED_COLORS

The 141 named colors ImageMagick understands, as [name, #rrggbb].

ImageForge.NATIVE_ONLY_FORMATS

Formats made by the browser instead of ImageMagick (svg, pdf).

ImageForge.SERVICE_DATA_FORMATS

Formats that are pixel data, not pictures (text): use generateFromTextFile for them.

ImageForge.SERVICE_PICTURE_FORMATS

Picture generators that need no input file: gradients, plasma, solid color, patterns, text.

ImageForge.classifyServiceArg(format)

What argument a generator takes: "color1", "color2", "text", "pattern" or "free".

ImageForge.engine()

The loaded magick-wasm module itself, for anything not wrapped by this library.

ImageForge.findNamedColor(text, lang)

Finds a named color by its name in the given language ("dark blue" → #00008b).

ImageForge.generateFromTextFile(file, width, height, opts)

Makes a picture from an ImageMagick pixel-enumeration text file and returns PNG bytes.

ImageForge.generateFromTextFileDataUrl(file, width, height, opts)

The same as generateFromTextFile, as a PNG data URL.

ImageForge.isServiceDataFormat(format)

Whether a format is pixel data (text) rather than a picture.

ImageForge.isServicePictureFormat(format)

Whether a format is a picture generator that needs no input file.

ImageForge.listColorNames()

Every color name the loaded ImageMagick knows.

ImageForge.listNamedColors(lang)

Lists the named colors with their names in the given language.

ImageForge.listServiceFormats()

Generator and service formats of the loaded ImageMagick, grouped by kind.

ImageForge.namedColorTexts(lang)

Labels of the color field in the given language (the texts of the color picker), English for a missing language.

ImageForge.textFontData

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

Dependencies

What the library loads, with the licenses (all are listed on the Licenses page).

NameVersionLicenseFiles
@openlibsdev/font-forge1.0.0MITGitHub
@imagemagick/magick-wasm0.0.35Apache-2.0 License · Source code
@imagemagick/magick-wasm0.0.35Apache-2.0 License · Source code
@simonwep/pickr1.10.2MIT License · Source code
@simonwep/pickr1.10.2MIT License · Source code
dejavu-fonts-ttf2.37.3Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain License · Source code
imagetracerjs1.2.6Unlicense License · Source code
jspdf2.5.2MIT License · Source code