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
This is very sweet. Thank you. Nevertheless, if the original PDF file is large (I have one around 36M), this method gets excruciatingly slow. I think that the problem is that Image Magick still parses the entire PDF, even if you tell it that you want only the first page. Fortunately, not all the files on our server are that large. I'm wondering if there is a way to tell Image Magick to stop reading after it's found the first page (or whatever page you request).