Use imagick in PHP to convert PDF into images, _PHP tutorial

WBOY
Release: 2016-07-13 10:08:59
Original
1222 people have browsed it

Use imagick in PHP to convert PDF into images,

In the PHP Manual, the description of imagick is really concise. For each member function, click to open and you will see the following text:

Copy code The code is as follows:

Warning
This function is currently not documented; only its argument list is available.

After solving the PHP loading problem just now, it is very convenient to process images. I found this paragraph online:
Copy code The code is as follows:

Header("Content-type: image/jpeg");
/**
* Reduce image size.
*
* @param $image The binary image to be processed
* @param $width The width of the processed image size (px)
* @param $height The height of the processed image size (px)
* @param $crop Whether to crop the image
*
* @return processed binary image
*/
function resize($image, $width, $height, $crop)
{
$imagick = new Imagick($image);
$w = $imagick->getImageWidth();
$h = $imagick->getImageHeight();
if ($w > $width || $h > $height)
{
If ($crop)
          {
            $imagick->cropThumbnailImage($width, $height);
}
      else
          {
            $imagick->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1, true);
}
}
$processed_image = $imagick->getImageBlob();
Return $processed_image;
}
$s=resize("123.jpg", 60, 40, 1);
echo $s;
?>

But what I want to do with this extension is convert PDF to PNG. PDF!
Copy code The code is as follows:

/**
* PDF2PNG
* @param $pdf PDF file to be processed
* @param $path The 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 The path and file name of the saved image
*/
function pdf2png($pdf,$path,$page=-1)
{
if(!extension_loaded('imagick'))

        return false;                                   }  
If(!file_exists($pdf))

        return false;                                   }  
$im = new Imagick();
$im->setResolution(120,120);
$im->setCompressionQuality(100);
if($page==-1)
         $im->readImage($pdf);
else
         $im->readImage($pdf."[".$page."]");
foreach ($im as $Key => $Var)

$Var->setImageFormat('png');
$filename = $path."/".md5($Key.time()).'.png';
If($Var->writeImage($filename) == true)
                                                                                      $Return[] = $filename;
                                                                                                                   }  
Return $Return;
}
$path="images";//Please make sure there is this folder in the current directory. Since it is used all the time, it will not be checked
$s=pdf2png("test.pdf",$path);
$scount=count($s);
for($i=0;$i<$scount;$i++)
{
echo "
Page ".($i+1)."

" ;
}
?>


Is it similar to the one above? But it always fails. There is an exception on the readImage side. Check the error output:


Copy code
The code is as follows: PHP Fatal error: Uncaught exception 'ImagickException' with message 'PostscriptDelegateFailed `test.pdf': No such file or directory'

After searching Baidu and Google for a long time, I finally had an idea, isn't it Postscript? I'm looking for it, I'm pretending.
This Postscript is actually GhostScript.
After installing it, run it and it’s OK!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947926.htmlTechArticleImagick is used in PHP to convert PDF into pictures. In the PHP Manual, the description of imagick is really concise. , for each member function, click to open and you will see the following text: Copy the code code such as...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!