GD and ImageMagick when you need them#
Imager is the default, but two other raster libraries are worth knowing. GD is older, smaller, and everywhere: a great deal of existing Perl draws with it, and its colour-index model is worth understanding even if you never choose it new. ImageMagick, through Image::Magick, is the heavyweight: the widest format support and the deepest bag of transformations, at the cost of a large dependency. This chapter shows ImageMagick, points to the chapter on GD, and says when each beats Imager.
GD#
GD has a chapter of its own: raster drawing with GD covers its colour model, the primitives, text, file formats, scaling and filters, and ends with a complete program.
When GD wins: you are maintaining code that already uses it (a large amount of older Perl does), you want the smallest dependency that still draws, or you specifically need libgd behaviour to match another tool in a pipeline that also uses libgd. For new drawing work with no such constraint, Imager’s named-colour and hash-argument API is friendlier and its filter set is richer.
ImageMagick (Image::Magick)#
Image::Magick, also called PerlMagick, ships with ImageMagick itself and exposes its full engine. Its strengths are format breadth (it reads and writes formats nothing else on CPAN touches) and transformations (hundreds of operations from Resize and Blur through Distort, Morphology, and format-specific tricks). Its weakness is the dependency: you need a matching ImageMagick library installed, and it is large.
The API is verb-oriented: you create an object, Read or size it, then call named operations, each returning an error string that is empty on success.
use Image::Magick;
my $im = Image::Magick->new;
$im->Set(size => '200x200');
$im->ReadImage('canvas:white'); # a blank white canvas
# drawing is one Draw call per primitive, SVG-path-like
$im->Draw(primitive => 'line', points => '0,0 199,199',
stroke => 'blue');
$im->Draw(primitive => 'circle', points => '100,100 100,40',
stroke => 'red', fill => 'none');
$im->Draw(primitive => 'polygon', points => '50,10 150,10 100,90',
fill => 'skyblue');
# transformations are named methods
$im->Blur(radius => 0, sigma => 1.5);
$im->Resize(geometry => '100x100');
my $err = $im->Write('out.png');
warn "$err" if $err;
The circle primitive takes the center followed by any point on the perimeter (so 100,100 100,40 is center (100,100) with radius 60), which is ImageMagick’s convention rather than a radius. Every geometry argument is ImageMagick’s string mini-language ('200x200', '100x100+10+10'), so it is worth a glance at ImageMagick’s own geometry documentation for the details.
When ImageMagick wins: you need a format nothing else reads, you are doing heavy batch transformation (thumbnailing thousands of files, format conversion pipelines), or you want one of its many operations that would be a project to reimplement. For interactive drawing or a tight dependency footprint, it is more machine than you need, and Imager or GD fit better.
Choosing#
You want … | Reach for |
|---|---|
New raster drawing, friendly API, good filters |
|
Smallest dependency, or an existing GD codebase |
|
Widest formats, heavy batch transforms |
|
All three are raster and share the raster ceiling: fixed resolution, degradation on upscaling. When you need output that scales cleanly, the next chapter moves to vector graphics with Cairo and SVG.