Home > Backend Development > PHP Tutorial > Two ways to use imagick to read PDF and generate png thumbnails with PHP_PHP Tutorial

Two ways to use imagick to read PDF and generate png thumbnails with PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:35:25
Original
967 people have browsed it

1. What is ImageMagick
ImageMagick is a set of powerful, stable and free tools and development kits 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.
2. What is php_imagick?
A PHP extension that allows PHP to call the ImageMagick function. Using this extension allows PHP to have the same functionality as ImageMagick.
3. Two ways to generate png thumbnails from PDF
The first one:

Copy the code The code is as follows:
/**
* PDF2PNG
* @param $pdf PDF file to be processed
* @param $path Image path to be saved
* @param $page Page to be exported - 1 for all 0 for the first Page 1 is the second page
* @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); // Scale the image
$filename = $path."/". time().'.png';
if($im->writeImage($filename) == true)
{
$Return = $filename;
}
return $Return;
}
$s = pdf2png('file/1371273225-ceshi_ppt.pdf','images');
echo '
';

Second type:


Copy code The code is as follows:function pdf2png($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;
}

http://www.bkjia.com/PHPjc/743932.html

truehttp: //www.bkjia.com/PHPjc/743932.htmlTechArticle1. What is ImageMagick? ImageMagick is a set of powerful, stable and free toolsets and development kits that can be used To read, write and process image files in over 185 basic formats, including...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template