Use the imagick plug-in to generate images from PDF files

Source: Internet
Author: User

There are two functions in total:

First

/**
* Optional 2png
* @ Param $ pdf file to be processed
* @ Param $ path: path of the image to be saved
* @ Param $ page the page to be exported-1 is all 0 is the first page 1 is the second page
* @ Return specifies the path and file name of the saved image.
*/
Function compute 2png ($ pdf, $ path, $ page = 0)

If (! Is_dir ($ path ))
   {
Mkdir ($ path, true );
   }
If (! Extension_loaded ('imagick '))
   { 
Echo 'imagick not found! ';
Return false;
   } 
If (! File_exists ($ pdf ))
   { 
Echo 'PDF not found ';
Return false;
   } 
$ Im = new Imagick ();
$ Im-> setResolution (120,120); // sets the image resolution.
$ Im-> setCompressionQuality (80); // compression ratio
$ Im-> readImage ($ pdf. "[". $ page. "]"); // Set to read the first page of pdf
// $ Im-> thumbnailImage (200,100, true); // change the image size
$ Im-> scaleImage (200,100, true); // resize the image
$ Filename = $ path. "/". time().'.png'
If ($ im-> writeImage ($ filename) = true)
   { 
$ Return = $ filename;
   } 
Return $ Return;

$ S = %2png ('File/137%3225-ceshi_ppt}', 'images ');
Echo'

'

Second

Function compute 2png ($ PDF, $ Path ){
If (! Extension_loaded ('imagick ')){
Return false;
   }
If (! File_exists ($ PDF )){
Return false;
   }
$ IM = new imagick ();
$ IM-> setResolution (120,120 );
$ IM-> setCompressionQuality (100 );
$ IM-> readImage ($ PDF );
Foreach ($ IM as $ Key => $ Var ){
$ Var-> setImageFormat ('PNG ');
$ Filename = $ Path. '/'.md5(?key.time({}.'.png'
If ($ Var-> writeImage ($ Filename) = true ){
$ Return [] = $ Filename;
       }
   }
Return $ Return;
}

Original article: http://www.lao8.org/article_1759/php_pdf_imagick.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.