4

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.

Chad E.
  • 1,196
  • 9
  • 12
  • 1
    I found no solution, maybe a bug, maybe not yet implemented. `gmagick` looks like a dead end to me. There's almost no documentation, only a very shallow one on PHP.net. No user-contributed notes (not that I'm missing them, but...). [Outdated releases](https://pecl.php.net/package/gmagick), [orphaned issue tracker](https://bugs.php.net/search.php?cmd=display&package_name[]=gmagick), last commit [2 years ago](https://svn.php.net/viewvc/pecl/gmagick/). – Sebastian B. May 28 '20 at 21:00

1 Answers1

0

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.

qzminski
  • 31
  • 3