# ImageMagick in the browser — library for developers

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.

## Installation

```sh
npm install @openlibsdev/image-forge
```

```js
import ImageForge from "https://cdn.jsdelivr.net/npm/@openlibsdev/image-forge@1.0.0/+esm";
```

## Usage

```js
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

- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @simonwep/pickr 1.10.2 (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)
- imagetracerjs 1.2.6 (Unlicense)
- jspdf 2.5.2 (MIT)

- GitHub: https://github.com/OpenLibsDev/image-forge
- npm: https://www.npmjs.com/package/@openlibsdev/image-forge
- Open the app: https://openlibs.dev/en/app/image-forge/
