PHP using Imagick to translate PDF into pictures _php tips

Source: Internet
Author: User
Tags translate pdf

PHP Manual, the description of Imagick, is really concise, each member function, click Open to see the following text:

Copy Code code as follows:

Warning
This function is currently not documented; Only it argument list is available.

Just solved the problem of PHP loading, the image processing is quite convenient, the Internet casually find a paragraph:
Copy Code code as follows:

<?php
Header ("Content-type:image/jpeg");
/**
* Reduce the size of the picture.
*
* @param $image binary picture to be processed
* @param the width of the picture size (px) after $width processing
* @param height (px) of picture size after $height processing
* @param $crop whether to crop pictures
*
* @return processed binary picture
*/
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 turn the PDF into PNG. PDF Oh!
Copy Code code as follows:

<?php
/**
* Pdf2png
* @param $pdf PDF files to be processed
* @param $path picture path to save
* @param $page the page to be exported-1 is all 0 is the first page 1 is the second page
* @return saved picture path and filename
*/
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 that this folder is in the current directory and will not be detected because it has been used all the time.
$s =pdf2png ("test.pdf", $path);
$scount =count ($s);
for ($i =0; $i < $scount; $i + +)
{
echo "<div Align=center><font color=red>page". ($i + 1). " </font><br><a href=\ "". $s [$i]. " \ "Target=_blank></a></div><p>";
}
?>

Same as the top, huh? But always unsuccessful, readimage over there. View error Output:
Copy 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 Mody? I'm looking, I'm faking it.
This postscript, in fact, is Ghostscript.
When you are ready, run, OK!

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.