Gmagick set density before loading PDF

169 Views Asked by At

Converting a PDF to an image using Gmagick in PHP renders a very poor quality image.

The solution in Imagick was to call setResolution(x,y) before loading the PDF file. This would change the -density option.

There is no setResolution(x,y) in Gmagick, and unfortunately, calling setimageresolution(x,y) just throws an error:

PHP Fatal error: Uncaught exception 'GmagickException' with message 'Can not process empty Gmagick object'

Calling setimageresolution(x,y) after loading the PDF has no effect, and I can't find a way to set the -density option before loading the file.

EDIT: I would be happy for a way to set the default density system-wide. I do have root access.

1

There are 1 best solutions below

0
On

I bumped into a similar problem and solved it with the following code:

$image = new \Gmagick();
$image->setresolution(300, 300);
$image->readimage('sample.pdf[0]');

Note that the setresolution method is not documented in PHP anywhere, but it seems to work – at least for me.