Php uses Imagick to generate png thumbnails for pdf files

Source: Internet
Author: User
Tags imagemagick

If the thumbnail is an image, we can use the php gD library directly. This article will introduce the Imagick method to generate png thumbnails for pdf files. Here we will use a plug-in, the following is an example.

Php_imagick

A PHP extension that allows PHP to call the ImageMagick function. With this extension, PHP can have the same functions as ImageMagick.
ImageMagick is a powerful, stable, and free toolkit that can be used to read, write, and process image files in over 185 basic formats, including popular TIFF, JPEG, GIF, PNG, PDF, and PhotoCD. By using ImageMagick, You can dynamically generate images based on the needs of web applications. You can also resize, rotate, sharpen, subtract, or add special effects to one or more images, save the operation results in the same format or other formats.

How to Use php_imagick

. Create a thumbnail and display it.

The Code is as follows: Copy code

<? Php

Header ('content-type: image/jpeg ');

$ Image = new Imagick('image.jpg ');

// If 0 is provided as a width or height parameter, // aspect ratio is maintained

$ Image-> thumbnailImage (100, 0 );

Echo $ image;

?>

Thumbnail GIF Animated Image

The Code is as follows: Copy code

<? Php

/* Create a new imagick object and read in GIF */

$ Im = new Imagick ("example.gif ");

/* Resize all frames */

Foreach ($ im as $ frame ){

/* 50x50 frames */

$ Frame-> thumbnailImage (50, 50 );

/* Set the virtual canvas to correct size */

$ Frame-> setImagePage (50, 50, 0, 0 );

}/* Notice writeImages instead of writeImage */

$ Im-> writeImages ("example_small.gif", true );

?>

Let's get started.


Pdf to generate png homepage thumbnails (the server must support Imagick)

The Code is as follows: Copy code

/**
* 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 = 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); // sets the image resolution.
$ Im-> setCompressionQuality (80); // compression ratio

$ Im-> readImage ($ pdf. "[". $ page. "]"); // set to read the first page of pdf
// $ Im-> thumbnailImage (200,100, true); // change the image size
$ Im-> scaleImage (200,100, true); // resize the image
$ Filename = $ path. "/". time().'.png ';

If ($ im-> writeImage ($ filename) = true)
{
$ Return = $ filename;
}
Return $ Return;
}

$ S = %2png ('file/137%3225-ceshi_ppt}', 'images ');
Echo "<div align = center> </div> ";

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.