0

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;
Philippe CARLE
  • 549
  • 3
  • 18

2 Answers2

1

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.

Danack
  • 24,939
  • 16
  • 90
  • 122
  • I think you got it ! I tried to update ghostscript on my server but yum update that no packages are marked for update… i'll investigate further later in the day ! – Philippe CARLE Apr 18 '14 at 06:55
  • 1
    You can just download the file from that server and run it from a directory, you don't need to install the updated version to test. – Danack Apr 18 '14 at 08:33
0

It is not a problem with anti-aliasing most likely.

Increase your resolution up to 400 and see what happens.

$imagick->setResolution(400,400);
Anatoliy Kim
  • 768
  • 4
  • 13
  • It definitely is a problem of aliasing… even with higher resolutions, texts' appearance is always the same… The screenshot i posted is not scaled, pixels are displayed exactly like this ! – Philippe CARLE Apr 15 '14 at 15:57
  • Try this? http://php.net/manual/en/imagick.setcompressionquality.php, first comment says a different command is used depending on php version. Maybe yours doesn't work? – Anatoliy Kim Apr 15 '14 at 18:46
  • I'm 99% sure it's not a resolution problem or a compression problem. By zooming on the text, there is absolutely no jpeg artefacts. If i increase the resolution, it's getting better, but 1) it's way too heavy and 2) it still is aliased. This is an aliasing problem, but it seems google's not my friend on this question ! I found out that there are more options using imagemagick with command line and dTextAlphaBits sounds like an option for this kind of problem… can't find similar function using php. – Philippe CARLE Apr 16 '14 at 06:36
  • http://www.php.net/manual/en/book.imagick.php - this is the only thing I can advice you then. Search a function you think you need and then google for its usage. – Anatoliy Kim Apr 16 '14 at 09:21