image_magick
image_magick is a Dart client for running ImageMagick through dart:io process calls on macOS or Linux. It gives you a small typed API for:
- checking whether ImageMagick is available
- quickly checking whether a file is a readable image
- listing supported readable image formats
- inspecting an image
- cropping to an exact region
- resizing while preserving aspect ratio
- converting into WebP or other output formats
- rotating and flipping images
- stripping metadata from output files
Install ImageMagick
macOS
With Homebrew:
brew install imagemagick
Verify the install:
magick -version
If your system exposes the legacy commands instead, these should also work:
convert -version
identify -version
Docker
For Debian or Ubuntu based images:
RUN apt-get update && apt-get install -y imagemagick
A slightly cleaner variant is:
RUN apt-get update \
&& apt-get install -y imagemagick \
&& rm -rf /var/lib/apt/lists/*
Add The Package
dart pub add image_magick
Usage
import 'package:image_magick/image_magick.dart';
Future<void> main() async {
bool supported = await ImageMagickClient.isSupported();
if (!supported) {
throw StateError('ImageMagick is not installed or is not on PATH.');
}
ImageMagickClient client = ImageMagickClient();
List<String> formats = await client.getSupportedFormats();
print(formats);
ImageMagickImageInfo original = await client.inspect(
inputPath: '/tmp/input.png',
);
print(original);
ImageMagickImageInfo resized = await client.resize(
inputPath: '/tmp/input.png',
outputPath: '/tmp/output.webp',
width: 1200,
height: 1200,
onlyShrink: true,
quality: 82,
format: 'webp',
);
print(resized);
bool readable = await client.canRead(inputPath: '/tmp/input.png');
print(readable);
ImageMagickImageInfo cropped = await client.crop(
inputPath: '/tmp/input.png',
outputPath: '/tmp/cropped.png',
width: 800,
height: 800,
x: 100,
y: 50,
format: 'png',
);
print(cropped);
ImageMagickImageInfo converted = await client.convert(
inputPath: '/tmp/input.png',
outputPath: '/tmp/output.jpg',
format: 'jpeg',
quality: 85,
);
print(converted);
ImageMagickImageInfo rotated = await client.rotate(
inputPath: '/tmp/input.png',
outputPath: '/tmp/rotated.png',
degrees: 90,
format: 'png',
);
print(rotated);
ImageMagickImageInfo stripped = await client.stripMetadata(
inputPath: '/tmp/input.png',
outputPath: '/tmp/clean.webp',
format: 'webp',
);
print(stripped);
}
API Notes
ImageMagickClient.isSupported()checks for either the modernmagickcommand or the legacyconvertandidentifypair.canRead()is a lightweightidentify -pingprobe for quick validation.crop()uses exact pixel bounds and resets the page with+repage.resize()uses ImageMagick geometry rules, so aspect ratio is preserved automatically.resize()requires at least one ofwidthorheight.convert()forces the requested output format with the ImageMagickformat:pathsyntax.rotate(),flipHorizontal(), andflipVertical()are thin wrappers around stable ImageMagick transform flags.stripMetadata()removes comments, EXIF, ICC profiles, and other embedded metadata with-strip.inspect()returnsImageMagickImageInfo, which includes the path, format, width, height, and MIME type when ImageMagick reports one.ImageMagickClient.legacy()is available if you want to explicitly targetconvertandidentify.