Search code examples
phpimagickantialiasing

PHP Imagick PDF conversion Text aliasing


I'm having some troubles converting PDF files to JPEG with Imagick in PHP. After a lot of research, i'm able to convert CMYK pdf to RGB jpg without weird color conversion… but now, my last issue : the text is completely aliased ! The text from the original PDF file is not vectorized.

An example :
Aliased text with pdf conversion

Here is the code :

$imagick = new Imagick();

$imagick->setResolution(150,150);
$imagick->readImage('file.pdf');

//CMYK PROFILE
$icc = file_get_contents('USWebCoatedSWOP.icc'); 
$imagick->profileImage('icc', $icc); 
$imagick->setImageColorspace(imagick::COLORSPACE_CMYK); 

//RGB PROFILE
$icc = file_get_contents('sRGB_IEC61966-2-1_no_black_scaling.icc'); 
$imagick->profileImage('icc', $icc); 
$imagick->setImageColorspace(imagick::COLORSPACE_RGB); 

$imagick->setImageFormat( "jpg" );
$imagick->setImageCompression(imagick::COMPRESSION_JPEG); 
$imagick->setImageCompressionQuality(90); 

header( "Content-Type: image/jpeg" );
echo $imagick;

Solution

  • Image Magick uses Ghostscript to actually render PDFs, and I think you are using a version of Ghostscript that has a bug in it, that causes text not to be anti-aliased correctly.

    I tested the code you provided, as well as with a direct invocation of Ghostscript with the command.

    gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150  -sOutputFile=foo-%d.png flyer.pdf
    

    By default, my Centos box was using Ghostscript version 8.70 which shows the issue you're seeing, both when invoked from Imagick and from the gs command above. Downloading version 9.14 from here makes the text be anti-aliased correctly when using the command-line, and probably would when invoked via Imagick.