Using Imagick in PHP to convert PDFs into pictures, _php tutorials

Source: Internet
Author: User

Using Imagick in PHP to convert PDFs into pictures,


PHP Manual, the description of Imagick, is really concise, each member function, click Open to see the following text:
Copy CodeThe code is as follows:
Warning
This function was currently not documented; Only it argument list is available.

Just solved the problem of PHP loading, the processing of the picture is quite convenient, the Internet casually find a paragraph:
Copy CodeThe code is as follows:
<?php
Header ("Content-type:image/jpeg");
/**
* Reduce image size.
*
* @param $image binary images to be processed
* Width of picture size after @param $width processing (px)
* Height of picture size after @param $height processing (px)
* @param $crop whether to crop the picture
*
* @return Binary images processed
*/
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'm going to do with this extension is to convert the PDF to PNG. PDF Oh!
Copy CodeThe code is as follows:
<?php
/**
* Pdf2png
* @param $pdf pending PDF file
* @param $path The path of the picture to be saved
* @param $page the page to be exported-1 for all 0 for the first page 1 for the second page
* @return saved picture path and file name.
*/
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";//Make sure that the folder is in the current directory, because it is always used, so it is not detected
$s =pdf2png ("test.pdf", $path);
$scount =count ($s);
for ($i =0; $i < $scount; $i + +)
{
echo "Page". ($i + 1). "

";
}
?>

Is it the same as above? But always unsuccessful, readimage over there, check the error output:
Copy the Code code as follows:
PHP Fatal error:uncaught exception ' imagickexception ' with message ' postscriptdelegatefailed ' test.pdf ': No such file or Directory

Baidu Google for a long time, the last brainwave, not to say PostScript it? I'm looking, I'm faking.
This postscript, in fact, is Ghostscript.
After the installation, a run, OK!

http://www.bkjia.com/PHPjc/947926.html www.bkjia.com true http://www.bkjia.com/PHPjc/947926.html techarticle PHP using Imagick implementation of the PDF into a picture, PHP manual, the description of Imagick, is really concise, each member function, click Open to see the following text: Copy code code such as ...

  • 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.