Converting a PDF file into an image using imagick in PHP

Source: Internet
Author: User
This article describes how to convert a PDF file into an image using imagick in PHP. This article focuses on the handling of two errors, especially GhostScript errors. For more information, see PHP Manual, the description of imagick is really concise. for each member function, click open to see the following text:

The code is as follows:


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


After the PHP loading problem was solved just now, it was quite convenient to process images. you can find the following link on the Internet:

The code is as follows:


<? Php
Header ("Content-type: image/jpeg ");
/**
* Reduce the image size.
*
* @ Param $ image: binary image to be processed
* @ Param $ width the width (px) of the image size after processing)
* @ Param $ height the height of the image size after processing (px)
* @ Param $ crop whether to crop the image
*
* @ Return: the 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 the PDF to PNG. PDF!

The code is as follows:


<? Php
/**
* Optional 2png
* @ Param $ pdf file to be processed
* @ Param $ path: 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 specifies the path and file name of the saved image.
*/
Function compute 2png ($ 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(1_key.time(1_0000.'.png ';
If ($ Var-> writeImage ($ filename) = true)
{
$ Return [] = $ filename;
}
}
Return $ Return;
}
$ Path = "images"; // make sure this folder is in the current directory. This folder is not checked because it is always used.
$ S = 1272png ("testbench", $ path );
$ Scount = count ($ s );
For ($ I = 0; $ I <$ scount; $ I ++)
{
Echo"

Page ". ($ I + 1 )."

";
}
?>


Is it similar to above? But it is always unsuccessful. if there is an exception in readImage, view the error output:

The code is as follows:


PHP Fatal error: Uncaught exception 'imagickexception' with message 'postscriptdelegatefailed 'tested': No such file or directory'


Baidu and Google have been around for a long time. what's the last thought? isn't it Postscript? I am looking for it.
This Postscript is actually a GhostScript.
After installation, run it. 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.