Download and install right version of ghostscript. In my case my PHP was x86 architecture, so I download Ghostscript 9.14 for Windows (32 bit)
.
Check, is imagick
extension available and loaded.
This line should be present in your php.ini
:
extension=php_imagick.dll
Also, check php_imagick.dll
in PHP's ext
directory.
<?php
function genPdfThumbnail($source, $target)
{
//$source = realpath($source);
$target = dirname($source).DIRECTORY_SEPARATOR.$target;
$im = new Imagick($source."[0]"); // 0-first page, 1-second page
$im->setImageColorspace(255); // prevent image colors from inverting
$im->setimageformat("jpeg");
$im->thumbnailimage(160, 120); // width and height
$im->writeimage($target);
$im->clear();
$im->destroy();
}
Call that function:
<?php
genPdfThumbnail('/uploads/my.pdf','my.jpg'); // generates /uploads/my.jpg
Use an PHP's external program execution functions to call pdftoppm which has the ability select pages thereby avoiding the whole "loading everything into memory problem", it's in poppler-utils on Ubuntu
example:
pdftoppm -l 1 -scale-to 150 -jpeg example.pdf > thumb.jpg
which translates to take the last page as 1 (you can specify a first page), scale the largest size to 150 pixels and throw out a jpeg.
https://linux.die.net/man/1/pdftoppm if windows is required then the poppler libraries are listed as containing pdftocairo which use the same arguments and has some windows specific printing options.