Overview
TheImageMagickHandler provides image format conversion capabilities using ImageMagick compiled to WebAssembly. It supports a wide range of image formats and handles batch conversions efficiently.
Supported Formats
ImageMagickHandler discovers formats from ImageMagick’ssupportedFormats list. Common formats include:
Raster Formats
- PNG - Portable Network Graphics
- JPEG - Joint Photographic Experts Group
- GIF - Graphics Interchange Format
- WebP - Web Picture format
- BMP - Windows Bitmap
- TIFF - Tagged Image File Format
- ICO - Microsoft Windows ICO
Vector & Other Formats
- PDF - Portable Document Format (write-only)
- MPO - Multi-Picture Object
- VST - Microsoft Visio Template
Filtered Formats
The handler excludes certain formats that are better handled by other handlers:Initialization
The handler loads ImageMagick WASM and discovers supported formats:Initialization Process
- Fetches ImageMagick WASM from
/convert/wasm/magick.wasm - Converts to Uint8Array
- Initializes ImageMagick with WASM bytes
- Iterates through
Magick.supportedFormats - Filters and normalizes format metadata
- Applies custom descriptions for common formats
- Prioritizes formats (PDF, GIF, JPEG, PNG)
Format Filtering
Formats are filtered based on MIME type validity:Custom Format Descriptions
Common formats receive standardized descriptions:Format Prioritization
Formats are sorted to prioritize common formats:Conversion Process
Basic Conversion
Multi-Image Handling
ImageMagickHandler usesMagickImageCollection to handle multiple input files:
Special Format Handling
RGB Format
Raw RGB data requires dimension estimation:
ICO Format
Windows ICO files are limited to 256×256 pixels:
PDF Reading
PDF is supported for writing but not reading:Read Settings
Input formats are configured usingMagickReadSettings:
Format Metadata
Each supported format includes:Human-readable format name (e.g., “Joint Photographic Experts Group”)
Normalized format identifier (e.g., “jpeg”, “png”)
File extension (e.g., “jpg”, “png”)
Normalized MIME type (e.g., “image/jpeg”)
Whether the format can be read (input)
Whether the format can be written (output)
ImageMagick’s internal format identifier
Format category extracted from MIME type (e.g., “image”)
true for PNG, BMP, TIFF; false for othersOutput File Naming
Output files preserve the input filename with updated extension:Format Normalization
JPEG format normalization:Lossless Detection
The handler identifies lossless formats:Properties
Handler identifier
Array of supported formats populated during initialization
true when initialization is complete and handler is ready for conversionsError Handling
The handler throws errors for:- Uninitialized handler usage
- Invalid format conversions
- WASM loading failures
- Image processing errors
Performance Considerations
- Uses ImageMagick WASM for in-browser processing
- Processes files in batches efficiently
- Automatically manages memory with
MagickImageCollection.use()resource scoping - Suitable for client-side image conversions without server roundtrips
Source Reference
Implementation:~/workspace/source/src/handlers/ImageMagick.ts