All formats

Imagick

LanguagePHP
LicensePHP License 3.01 (ImageMagick license for the library; Ghostscript, needed for EPS, is AGPL-3.0)
Repositorygithub.com/Imagick/imagick
Documentationphp.net/imagick
Depends onImageMagick, Ghostscript
Read / writeyes (rasterized through Ghostscript) / yes (raster image wrapped in EPS)

When Imagick reads an EPS file, ImageMagick calls Ghostscript to render it, cropped to the bounding box, at the resolution set with setResolution(). That call must come before readImage(). The result is a bitmap you can resize and save in any format.

Installation#

Shell
sudo apt install php-imagick ghostscript    # Debian / Ubuntu

If you get “not authorized EPS”, the ImageMagick policy.xml of your distribution blocks the format; relax that line.

Basic usage: convert EPS to PNG#

PHP
<?php
try {
    $im = new Imagick();
    $im->setResolution(300, 300);       // before readImage
    $im->readImage('logo.eps');
    $im->setImageFormat('png');         // keeps transparency where nothing is drawn
    $im->writeImage('logo.png');
    $im->clear();
} catch (ImagickException $e) {
    echo 'Error: ', $e->getMessage(), "\n";
}

How to convert EPS to JPEG of a given width#

PHP
$im = new Imagick();
$im->setResolution(300, 300);
$im->readImage('logo.eps');
$im->setImageBackgroundColor('white');
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$im->resizeImage(1200, 0, Imagick::FILTER_LANCZOS, 1);   // 0 = keep aspect ratio
$im->setImageFormat('jpeg');
$im->setImageCompressionQuality(90);
$im->writeImage('logo.jpg');

How to get the size of an EPS graphic#

PHP
$im = new Imagick();
$im->pingImage('logo.eps');
printf("%d x %d pt\n", $im->getImageWidth(), $im->getImageHeight());  // 72 dpi: px = pt

How to save an image as EPS#

PHP
$im = new Imagick('photo.jpg');
$im->setImageFormat('eps3');   // eps2, eps3, or ept (with a TIFF preview)
$im->writeImage('photo.eps');

Tips#