Last active
December 8, 2018 20:07
-
-
Save thedineshj/e52cfac4f7079ccdce25404ee7ae6159 to your computer and use it in GitHub Desktop.
This function will generate a thumnail in png format for a pdf document using - ImageMagick ( http://php.net/imagick ) , Ghostscript (https://sourceforge.net/projects/ghostscript/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*** | |
* This function will generate a thumbnail in png format for a pdf document using | |
* ImageMagick - http://php.net/imagick | |
* Ghostscript - https://sourceforge.net/projects/ghostscript/ | |
* @param $source - string - relative path to the source pdf (for example : ./originals/lorem-ipsum.pdf ) | |
* @param $destination - string - relative path to the directory where you want to place the generated thumbnail (for example : './thumnails') | |
* @param $fileName - string - file name of the thumbnail with out extension (for example : pdfthumbdemo ) | |
* @param width - number - width of the thumbnail | |
* @param height - number - height of the thumbnail | |
*/ | |
function generateThumbnail($source, $destination, $fileName, $width, $height) | |
{ | |
try { | |
$source = realpath($source); | |
$destination = realpath($destination) . DIRECTORY_SEPARATOR . $fileName . ".png"; | |
$imagick = new Imagick(); | |
$imagick->readImage($source . "[0]"); | |
$imagick->thumbnailimage($width, $height); | |
$imagick->setIteratorIndex(0); | |
$imagick->writeImages($destination, false); | |
$imagick->clear(); | |
$imagick->destroy(); | |
return true; | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
return false; | |
} | |
} | |
/* | |
Usage - var_dump(generateThumbnail('./originals/lorem-ipsum.pdf','./thumbnails','pdfthumbdemo',500,500)); | |
For a working example visit | |
https://github.com/thedineshj/php-generate-thumnails-for-pdf-documents | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment