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