Home  >  Article  >  Backend Development  >  PHP uses Imagick to generate png thumbnails from pdf_PHP tutorial

PHP uses Imagick to generate png thumbnails from pdf_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:46:18833browse

If the thumbnail is a picture, we can directly use the php gD library. This article will introduce the method of Imagick to generate PNG thumbnails from PDF. Here we will use a plug-in. Let me show you an example. .

php_imagickwhat

A PHP extension that allows PHP to call the ImageMagick function. Using this extension allows PHP to have the same functionality as ImageMagick.
ImageMagick is a powerful, stable and free toolset and development package that can be used to read, write and process image files in more than 185 basic formats, including popular TIFF, JPEG, GIF, PNG, PDF and PhotoCD formats. . Using ImageMagick, you can dynamically generate images according to the needs of web applications. You can also change the size, rotate, sharpen, reduce color or add special effects to an image (or a group of images), and save the results in the same format. or save in other formats.

How to use php_imagick

. Create a thumbnail and display it

The code is as follows Copy code
 代码如下 复制代码

header('Content-type: image/jpeg');

$image = new Imagick('image.jpg');

// If 0 is provided as a width or height parameter,// aspect ratio is maintained

$image->thumbnailImage(100, 0);

echo $image;

?>

header('Content-type: image/jpeg');

$image = new Imagick('image.jpg');
代码如下 复制代码

/* Create a new imagick object and read in GIF */

$im = new Imagick("example.gif");

/* Resize all frames */

foreach ($im as $frame) {

/* 50x50 frames */

$frame->thumbnailImage(50, 50);

/* Set the virtual canvas to correct size */

$frame->setImagePage(50, 50, 0, 0);

}/* Notice writeImages instead of writeImage */

$im->writeImages("example_small.gif", true);

?>

// If 0 is provided as a width or height parameter,// aspect ratio is maintained $image->thumbnailImage(100, 0); echo $image; ?> Thumbnail GIF animated pictures
The code is as follows Copy code
<🎜>/* Create a new imagick object and read in GIF */<🎜> <🎜>$im = new Imagick("example.gif");<🎜> <🎜>/* Resize all frames */<🎜> <🎜>foreach ($im as $frame) {<🎜> <🎜>/* 50x50 frames */<🎜> <🎜>$frame->thumbnailImage(50, 50); /* Set the virtual canvas to correct size */ $frame->setImagePage(50, 50, 0, 0); }/* Notice writeImages instead of writeImage */ $im->writeImages("example_small.gif", true); ?>

Okay, let’s get to the point.


Generate png homepage thumbnail from pdf (the server needs to support Imagick)

The code is as follows
 代码如下 复制代码

/**
* PDF2PNG  
* @param $pdf  待处理的PDF文件
* @param $path 待保存的图片路径
* @param $page 待导出的页面 -1为全部 0为第一页 1为第二页
* @return      保存好的图片路径和文件名
*/
 function pdf2png($pdf,$path,$page=0)
{  
   if(!is_dir($path))
   {
       mkdir($path,true);
   }
   if(!extension_loaded('imagick'))
   {  
     echo '没有找到imagick!' ;
     return false;
   }  
   if(!file_exists($pdf))
   {  
      echo '没有找到pdf' ;
       return false;  
   }  
   $im = new Imagick();  
   $im->setResolution(120,120);   //设置图像分辨率
   $im->setCompressionQuality(80); //压缩比

   $im->readImage($pdf."[".$page."]"); //设置读取pdf的第一页
   //$im->thumbnailImage(200, 100, true); // 改变图像的大小
   $im->scaleImage(200,100,true); //缩放大小图像
   $filename = $path."/". time().'.png';

   if($im->writeImage($filename) == true)
   {  
      $Return  = $filename;  
   }  
   return $Return;  
}  

$s=pdf2png('file/1371273225-ceshi_ppt.pdf','images');
echo "

";

Copy code
/** * PDF2PNG

* @param $pdf PDF file to be processed

* @param $path The path of the image to be saved * @return The path and file name of the saved image*/ function pdf2png($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); //Set image resolution $im->setCompressionQuality(80); //Compression ratio $im->readImage($pdf."[".$page."]"); //Set the first page to read pdf //$im->thumbnailImage(200, 100, true); //Change the size of the image $im->scaleImage(200,100,true); //Zoom image $filename = $path."/". time().'.png'; if($im->writeImage($filename) == true) {  $Return = $filename; }   Return $Return; } $s=pdf2png('file/1371273225-ceshi_ppt.pdf','images'); echo "
"; http://www.bkjia.com/PHPjc/632954.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632954.htmlTechArticleIf the thumbnail is a picture, we can directly use the php gD library. This article will introduce Imagick How to generate png thumbnails from pdf. Here we have to use a plug-in. Next I will...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn