All formats

Imagick

LanguagePHP (C extension)
LicensePHP License 3.01 (ImageMagick: ImageMagick License, Apache-2.0 style)
Repositorygithub.com/Imagick/imagick
Documentationphp.net/imagick
Depends onImageMagick 6.5.3+ or 7 (with librsvg or Inkscape for best SVG rendering)
Read / writeread (rasterizes SVG to any format ImageMagick writes) / no

Imagick wraps ImageMagick’s MagickWand API. ImageMagick reads SVG with one of 3 renderers, chosen at build time: librsvg (best), Inkscape if it is installed, or its own internal MSVG renderer (limited: weak text, no filters, gaps in CSS). Run magick -list format | grep SVG (or convert -list format) to see which one your server uses: SVG SVG rw+ Scalable Vector Graphics (RSVG 2.x) means librsvg.

SVG is rendered at a resolution set with setResolution before reading the file. After that the result is a raster image like any other, which you can resize, composite and write in any format.

Installation#

Shell
# Debian / Ubuntu
sudo apt install php-imagick librsvg2-bin
# PECL
pecl install imagick

Enable the extension with extension=imagick in php.ini. On Windows, download the DLL matching your PHP build from PECL.

Basic usage: convert SVG to PNG#

PHP
<?php
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));  // keep transparency
$im->readImage('in.svg');
$im->setImageFormat('png32');
$im->writeImage('out.png');

echo $im->getImageWidth(), ' x ', $im->getImageHeight(), "\n";
$im->clear();

Without setBackgroundColor('transparent') the background is white.

How to render SVG at a higher resolution#

SVG is rendered at its intrinsic pixel size (96 DPI) by default. Set a higher resolution before readImage: 192 DPI doubles the pixel size:

PHP
$im = new Imagick();
$im->setResolution(192, 192);
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImage('in.svg');
$im->writeImage('out@2x.png');

How to render SVG to an exact width#

Render at a high enough resolution, then resize down for a sharp result:

PHP
$im = new Imagick();
$im->setResolution(300, 300);
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImage('in.svg');
$im->resizeImage(512, 0, Imagick::FILTER_LANCZOS, 1);  // height 0 keeps the aspect ratio
$im->writeImage('out-512.png');

How to convert SVG to JPEG#

PHP
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('white'));
$im->readImage('in.svg');
$im->setImageFormat('jpeg');
$im->setImageCompressionQuality(90);
$im->writeImage('out.jpg');

How to convert an SVG string from memory#

PHP
$svg = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" fill="orange"/></svg>';

$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImageBlob($svg);
$im->setImageFormat('png');

header('Content-Type: image/png');
echo $im->getImageBlob();

If ImageMagick doesn’t detect the format, prefix the content with a format hint by reading 'svg:' . $path or call $im->setFormat('svg') before readImageBlob.

Tips#