# ImageMagick Conversion Capabilities in the p2r3/convert Project

> Discover ImageMagick conversion capabilities in the p2r3/convert project. This WebAssembly engine supports dynamic raster image conversion across many formats with lossless detection and PDF handling.

- Repository: [p2r3/convert](https://github.com/p2r3/convert)
- Tags: how-to-guide
- Published: 2026-02-19

---

**The p2r3/convert project leverages a WebAssembly-compiled ImageMagick engine to support dynamic raster image conversion across dozens of formats, with built-in detection for lossless formats and special handling for PDF output.**

The `p2r3/convert` repository implements a robust image conversion pipeline using ImageMagick compiled to WebAssembly. At the core of this system is the `ImageMagickHandler` class, which bridges the TypeScript application code with the `@imagemagick/magick-wasm` engine. This handler dynamically discovers available formats at runtime, enabling support for a wide range of image types without hardcoding format lists.

## WebAssembly Initialization and Format Discovery

The `ImageMagickHandler` initializes by loading the compiled WebAssembly module and querying the engine for its capabilities.

### Loading the ImageMagick Engine

When the handler's `init()` method is called, it loads the WebAssembly binary from `/convert/wasm/magick.wasm` as specified in [`src/handlers/ImageMagick.ts`](https://github.com/p2r3/convert/blob/main/src/handlers/ImageMagick.ts) (lines 24-27). This establishes the connection between the JavaScript runtime and the ImageMagick processing engine.

### Runtime Format Enumeration

After loading, the handler enumerates every format supported by the compiled binary using `Magick.supportedFormats` (lines 29-50). This approach creates a **runtime-determined catalogue** that automatically adapts to the exact ImageMagick version shipped with the project, ensuring the format list remains accurate across updates.

## Format Filtering and Classification

The handler applies intelligent filtering to ensure security and compatibility while categorizing formats by their capabilities.

### Security and Compatibility Filters

The system explicitly excludes certain formats and MIME types that could pose security risks or compatibility issues. According to the source code in [`src/handlers/ImageMagick.ts`](https://github.com/p2r3/convert/blob/main/src/handlers/ImageMagick.ts) (lines 31-39), the handler filters out:

- **APNG** and **SVG** formats (deliberately skipped)
- Any format with a `text/*` MIME type
- Any format with a `video/*` MIME type
- Formats with `application/json` MIME type

### Directional Format Support

The handler tracks whether each format supports input (`from`), output (`to`), or both. Notably, **PDF files are restricted to write-only operations** (`from: false`), meaning the system can generate PDFs from images but cannot convert PDFs to other image formats (lines 44-46).

### Lossless Format Detection

The handler identifies truly lossless formats through a dedicated `lossless` flag. Formats marked as lossless include `png`, `bmp`, and `tiff` (lines 49-50). This metadata allows the application to indicate quality preservation capabilities to users.

## Supported Image Formats

After filtering and classification, the handler reports a comprehensive catalogue of supported formats organized by category:

- **Raster images**: `png`, `jpeg`/`jpg`, `gif`, `bmp`, `tiff`, `ico`, `webp`, `heic`, `heif`, `avif`, `jxl` (lossless support for `png`, `bmp`, `tiff`)
- **Raw formats**: `ppm`, `pgm`, `pbm`, `raw`, and other ImageMagick-supported raw types
- **Multi-page documents**: `pdf` (write-only output)
- **Legacy formats**: Various other raster formats supported by the compiled ImageMagick build

The handler prioritizes common formats in UI selectors by sorting `png`, `jpeg`, `gif`, and `pdf` to appear first in the list (lines 55-63).

## Practical Implementation Examples

The following examples demonstrate how to use the `ImageMagickHandler` for common conversion tasks.

### Converting PNG to JPEG

```typescript
import ImageMagickHandler from "./handlers/ImageMagick.ts";
import { readFileSync } from "fs";

async function pngToJpeg(pngPath: string, jpegPath: string) {
  const handler = new ImageMagickHandler();
  await handler.init();                     // loads the WASM module
  const pngBytes = readFileSync(pngPath);
  const pngFormat = handler.supportedFormats!.find(f => f.format === "png")!;
  const jpegFormat = handler.supportedFormats!.find(f => f.format === "jpeg")!;

  const output = await handler.doConvert(
    [{ bytes: new Uint8Array(pngBytes), name: "image.png" }],
    pngFormat,
    jpegFormat
  );

  // `output[0].bytes` now holds the JPEG data
  writeFileSync(jpegPath, Buffer.from(output[0].bytes));
}

```

### Generating PDF from BMP

Since PDF is write-only in this implementation, you can convert raster images to PDF but not the reverse:

```typescript
async function bmpToPdf(bmpPath: string, pdfPath: string) {
  const handler = new ImageMagickHandler();
  await handler.init();
  const bmp = handler.supportedFormats!.find(f => f.format === "bmp")!;
  const pdf = handler.supportedFormats!.find(f => f.format === "pdf")!; // write-only

  const bmpBytes = readFileSync(bmpPath);
  const result = await handler.doConvert(
    [{ bytes: new Uint8Array(bmpBytes), name: "image.bmp" }],
    bmp,
    pdf
  );

  writeFileSync(pdfPath, Buffer.from(result[0].bytes));
}

```

## Summary

- The `ImageMagickHandler` in [`src/handlers/ImageMagick.ts`](https://github.com/p2r3/convert/blob/main/src/handlers/ImageMagick.ts) provides the primary ImageMagick conversion capabilities by wrapping the `@imagemagick/magick-wasm` engine.
- Format support is determined at runtime by querying `Magick.supportedFormats`, creating a dynamic catalogue that adapts to the compiled ImageMagick version.
- Security filters exclude SVG, APNG, text files, video files, and JSON, while PDF output is restricted to write-only operations.
- Lossless formats (`png`, `bmp`, `tiff`) are explicitly flagged for quality-conscious workflows.
- The handler exposes a `doConvert` method that accepts source files, source format metadata, and target format metadata to perform conversions entirely within the browser or Node.js WebAssembly environment.

## Frequently Asked Questions

### What image formats does the p2r3/convert project support?

The project supports any raster image format that the compiled ImageMagick WebAssembly binary can handle, excluding SVG, APNG, text, video, and JSON types. This includes common formats like PNG, JPEG, GIF, WebP, HEIC, AVIF, and JXL, along with legacy formats such as BMP, TIFF, ICO, and various raw formats (PPM, PGM, PBM).

### Why are SVG and APNG excluded from conversion?

According to the source code in [`src/handlers/ImageMagick.ts`](https://github.com/p2r3/convert/blob/main/src/handlers/ImageMagick.ts) (lines 31-39), SVG and APNG are deliberately skipped during format enumeration. SVG is excluded likely due to security concerns with XML parsing and potential XSS vectors, while APNG may be excluded due to complexity or compatibility issues with the specific ImageMagick WASM build used in the project.

### Can the project convert images to PDF?

Yes, but with a significant restriction. The `ImageMagickHandler` allows PDF as a **write-only format** (`from: false`), meaning you can convert images (like BMP, PNG, or JPEG) into PDF documents, but you cannot convert existing PDFs into image formats. This one-way conversion is enforced in the format discovery logic at lines 44-46 of [`src/handlers/ImageMagick.ts`](https://github.com/p2r3/convert/blob/main/src/handlers/ImageMagick.ts).

### How does the project determine which formats are lossless?

During format initialization, the handler checks the format identifier against a hardcoded list of lossless formats. Specifically, `png`, `bmp`, and `tiff` are flagged with `lossless: true` in the `FileFormat` object creation (lines 49-50). This allows the application UI to indicate to users which conversion options will preserve image quality without compression artifacts.