Advertisement
theitd

pdf2jpeg PHP

Nov 8th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?php
  2.     function genPdfThumbnail($source, $target)
  3.     {
  4.        
  5.         // Max vert or horiz resolution
  6.         $maxsize=550;
  7.  
  8.         // Create object
  9.         $im     = new Imagick($source."[0]"); // 0-first page, 1-second page
  10.        
  11.         // Resolution
  12.         //$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
  13.         //$im->setResolution(300, 300);
  14.         //$imageResolution = $im->getImageResolution();
  15.         //print_r($imageResolution);
  16.        
  17.         $im->readImage($source."[0]");  //Open after setting resolution.
  18.        
  19.         //$im->setImageBackgroundColor('white');
  20.         // Avoid any funny business with colours and inverted alphas
  21.         $im = $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
  22.  
  23.         // Type and quality
  24.         $im->setimageformat("jpeg");
  25.         $im->setImageCompressionQuality(75);
  26.         //$im->setImageCompression(imagick::COMPRESSION_JPEG);
  27.         //$im->thumbnailimage(1240, 1754, true, true); // width and height [Print Quality]
  28.        
  29.         // Resizes to whichever is larger, width or height
  30.         if($im->getImageHeight() <= $im->getImageWidth())
  31.         {
  32.             // Resize image using the lanczos resampling algorithm based on width
  33.             $im->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
  34.         }
  35.         else
  36.         {
  37.             // Resize image using the lanczos resampling algorithm based on height
  38.             $im->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
  39.         }
  40.        
  41.         // Strip out unneeded meta data
  42.         $im->stripImage();
  43.        
  44.         $im->writeimage($target);
  45.         $im->clear();
  46.         $im->destroy();
  47.     }
  48.  
  49. // genPdfThumbnail('/uploads/my.pdf','my.jpg'); // generates /uploads/my.jpg
  50.  
  51.  
  52. foreach ($argv as $arg) {
  53.     $e=explode("=",$arg);
  54.     if(count($e)==2)
  55.         $_GET[$e[0]]=$e[1];
  56.     else    
  57.         $_GET[$e[0]]=0;
  58. }
  59.  
  60. // Set the output directory
  61. $outputDir = "/images/thumbs/";
  62.  
  63. // Get a list of files and directories, excluding parent and current
  64. $allFiles =     array_diff( scandir( $e[0] ), array('..','.') );
  65.  
  66. // Get a list of sub-directories to filter
  67. $allDirs =      array_filter( $allFiles, "is_dir" );
  68.  
  69. // Create an array with just the files in the current directory
  70. $files =        array_diff( $allFiles, $allDirs );
  71.  
  72. // Now filter for PDFs
  73. foreach($files as $file => $pdf)
  74. {
  75.     if(strpos($pdf, '.pdf') == false)
  76.         unset($files[$file]);
  77. }
  78.  
  79. // Re-index the array, starting from [0]
  80. $files = array_values($files);
  81.  
  82. $i = 0;
  83.  
  84. foreach($files as $file)
  85. {
  86.     if(is_file($e[0].$file)){
  87.         $output_file = $outputDir . pathinfo($file, PATHINFO_FILENAME) . ".jpg";
  88.         $source_file = $e[0].$file;
  89.         genPdfThumbnail($source_file, $output_file);
  90.         echo $output_file . " ...generated" . "\n";
  91.         ++$i;
  92.     }
  93. }
  94.  
  95. echo "Completed " . $i . " files";
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement