Convert PDF to JPEG with PHP and ImageMagick Convert PDF to JPEG with PHP and ImageMagick php php

Convert PDF to JPEG with PHP and ImageMagick


It can be done using setResolution, but you need to do it before loading an image.Try something like this:

// instantiate Imagick $im = new Imagick();$im->setResolution(300,300);$im->readimage('document.pdf[0]'); $im->setImageFormat('jpeg');    $im->writeImage('thumb.jpg'); $im->clear(); $im->destroy();


The quality of the image produced from the PDF can be changed by setting the density (which is the DPI) before reading in the PDF - this gets past to ghostscript (gs) underneath which rasterizes the PDF. To get a good result, supersample at double the density you require, and use resample to get back to the desired DPI. Remember to change the colorspace to RGB if you want an RGB JPEG.

A typical command line version for convert might be:

convert -density 600 document.pdf[0] -colorspace RGB -resample 300 output.jpg

If you need to crop it, a -shave command following the resample is usually sensible, if the image is centred within the page.

As for the PHP IMagick extension, well, I never personally use it - so am unsure of how you specify file reading hints to it, but I would hope it is possible.


$im = new imagick();//this must be called before reading the image, otherwise has no effect$img->setResolution(200,200);//read the pdf$img->readImage("{$pdf_file}[0]");