Andrew
Andrew

Reputation: 218

PHP Imagick converting SVG without antialiasing

I'm trying to write a PHP function to convert an SVG image without any antialiasing (so that the final PNG is blocky and contains only the colours specified in the SVG).

The command line equivalent is:

convert +antialias /path/to.svg /path/to.png

I'm assuming that I need to use PHP's Imagick::setOption method to pass in "+antialias", but the documentation is very sparse.

The following snippet will write a PNG file, but none of the options prevents antialiased pixels being rendered:

$image = new Imagick();

// None of these have any affect - output image is always antialiased.
$image->setOption('+antialias', true);
$image->setOption('-antialias', true);
$image->setOption('+antialias', 'true');
$image->setOption('-antialias', 'true');
$image->setOption('antialias', true);
$image->setOption('antialias', false);
$image->setOption('antialias', 'true');
$image->setOption('antialias', 'false');

$image->readImage('/path/to.svg');
$image->writeImage('/path/to.png');

Any help would be great, thanks.

Upvotes: 2

Views: 696

Answers (3)

Noel Whitemore
Noel Whitemore

Reputation: 794

Anti-aliasing can be disabled using the Imagick PHP module but not by the expected method. As explained in this Imagick GitHub issue the correct way to disable anti-aliasing is

$imagick->setAntiAlias(FALSE);

Using $imagick->setOption() does not appear to work at all, and the PHP manual currently has no entry for setAntiAlias(). I've tested this in my own image generation script and it works. My reason for disabling anti-aliasing was that I needed to have a way to take color samples from my image and map them to a defined palette without having anti-aliased pixels generating colors not defined in the palette.

Upvotes: 0

jcupitt
jcupitt

Reputation: 11220

I don't think what you want is possible, unfortunately. ImageMagick shells out to Inkscape to render SVGs, and Inkscape has no command-line option to disable antialiasing.

https://graphicdesign.stackexchange.com/questions/138075/export-svg-without-anti-aliasing-in-inkscape-1-0-by-command-line

You could render at high resolution and then do nearest-neighbour downsampling. It would reduce the visible antialiasing, but you would still get some colours not in the SVG file.

Upvotes: 2

fmw42
fmw42

Reputation: 53212

That is not a good way to anti-alias a vector file such as SVG. The proper way in command line would be to set the desired density before reading the file and then resize back to compensate for a large magnification when using a large density. So for example

convert -density 288 image.svg -resize 25% image.png

Nominal density (default) is 72. So 288 = 72*4. Thus we resize afterwards by 1/4 = 25%, unless you want a larger output. Then resize by a larger value.

In PHP Imagick, you can create a new Imagick() instance. Then set the desired density. Then read the input SVG. Then resize. Then set the PNG format and save to disk. See setImageResolution for setting the density in Imagick. See https://www.php.net/manual/en/imagick.setimageresolution.php

Upvotes: 2

Related Questions