Efficiently generating thumbnails of a pdf in php
Posted: 2014-01-16T17:38:39-07:00
Hi all,
We have some legacy code that generates a pdf preview by converting the pdf to 250px png thumbnails of each page (so 10 thumbnails for a 10 page pdf). This has been thrashing our server royally as load has grown, because the original developer did the thumbnails by writing the source pdf to disk and then doing a system call (from PHP) 3 times for each file. We've switched over to using Imagick for PHP instead. Currently we're doing something simple like in the PHP manual:
After doing some load testing this has been a HUGE improvement in terms of server load. However, while it's much less likely to kill the server, we've found it's not actually any faster than doing it via command line calls. I suppose this somewhat makes sense... But what I'm trying to figure out is if there is any way we can optimize this process to be quicker. I've tried a few things like resizing the pdf before converting or calling $image->setSize() before reading it in, but those didn't seem to apply to pdfs.
Any tips would be appreicated!
We have some legacy code that generates a pdf preview by converting the pdf to 250px png thumbnails of each page (so 10 thumbnails for a 10 page pdf). This has been thrashing our server royally as load has grown, because the original developer did the thumbnails by writing the source pdf to disk and then doing a system call (from PHP) 3 times for each file. We've switched over to using Imagick for PHP instead. Currently we're doing something simple like in the PHP manual:
Code: Select all
$image = new Imagick();
$image->readImageBlob($pdf->render());
$image->resetIterator();
$image = $image->appendImages(false);
$image->setImageFormat( "png" );
$image->scaleImage(0, 250);
return array(base64_encode($image->getImageBlob()));
Any tips would be appreicated!