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> "; |