<?php
function genPdfThumbnail($source, $target)
{
// Max vert or horiz resolution
$maxsize=550;
// Create object
$im = new Imagick($source."[0]"); // 0-first page, 1-second page
// Resolution
//$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
//$im->setResolution(300, 300);
//$imageResolution = $im->getImageResolution();
//print_r($imageResolution);
$im->readImage($source."[0]"); //Open after setting resolution.
//$im->setImageBackgroundColor('white');
// Avoid any funny business with colours and inverted alphas
$im = $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
// Type and quality
$im->setimageformat("jpeg");
$im->setImageCompressionQuality(75);
//$im->setImageCompression(imagick::COMPRESSION_JPEG);
//$im->thumbnailimage(1240, 1754, true, true); // width and height [Print Quality]
// Resizes to whichever is larger, width or height
if($im->getImageHeight() <= $im->getImageWidth())
{
// Resize image using the lanczos resampling algorithm based on width
$im->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
}
else
{
// Resize image using the lanczos resampling algorithm based on height
$im->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
}
// Strip out unneeded meta data
$im->stripImage();
$im->writeimage($target);
$im->clear();
$im->destroy();
}
// genPdfThumbnail('/uploads/my.pdf','my.jpg'); // generates /uploads/my.jpg
foreach ($argv as $arg) {
$e=explode("=",$arg);
if(count($e)==2)
$_GET[$e[0]]=$e[1];
else
$_GET[$e[0]]=0;
}
// Set the output directory
$outputDir = "/images/thumbs/";
// Get a list of files and directories, excluding parent and current
$allFiles = array_diff( scandir( $e[0] ), array('..','.') );
// Get a list of sub-directories to filter
$allDirs = array_filter( $allFiles, "is_dir" );
// Create an array with just the files in the current directory
$files = array_diff( $allFiles, $allDirs );
// Now filter for PDFs
foreach($files as $file => $pdf)
{
if(strpos($pdf, '.pdf') == false)
unset($files[$file]);
}
// Re-index the array, starting from [0]
$files = array_values($files);
$i = 0;
foreach($files as $file)
{
if(is_file($e[0].$file)){
$output_file = $outputDir . pathinfo($file, PATHINFO_FILENAME) . ".jpg";
$source_file = $e[0].$file;
genPdfThumbnail($source_file, $output_file);
echo $output_file . " ...generated" . "\n";
++$i;
}
}
echo "Completed " . $i . " files";
?>
Comments