PHP uses Imagick to generate PNG thumbnails from PDF

Source: Internet
Author: User
Tags imagemagick

Php_imagick what?

A php extension that allows PHP to invoke ImageMagick functionality. Using this extension enables PHP to have the same functionality as ImageMagick.
ImageMagick is a powerful, stable, and free toolset and development kit that can be used to read, write, and process picture files in more than 185 basic formats, including popular TIFF, JPEG, GIF, PNG, PDF, and PHOTOCD formats. With ImageMagick, you can dynamically generate pictures based on the needs of your Web application, and you can resize, rotate, sharpen, subtractive, or add special effects to one (or a group) of pictures and save the results of the operation in the same format or in other formats.

Php_imagick How to use

. 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 animation picture

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

?>

All right, let's get to the point.


PDF Build PNG home Thumbnail (server needs support Imagick)

The code is as follows Copy Code

/**
* 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 =0)
{
if (!is_dir ($path))
{
mkdir ($path, true);
}
if (!extension_loaded (' Imagick '))
{
Echo ' didn't find imagick!. ' ;
return false;
}
if (!file_exists ($pdf))
{
Echo ' did not find pdf ';
return false;
}
$im = new Imagick ();
$im->setresolution (120,120); Set Image resolution
$im->setcompressionquality (80); Compression ratio

$im->readimage ($pdf.) [". $page."]); Set the first page to read a PDF
$im->thumbnailimage (+, true); Change the size of an image
$im->scaleimage (200,100,true); Shrink Zoom Small Image
$filename = $path. " /". Time (). '. PNG ';

if ($im->writeimage ($filename) = = True)
{
$Return = $filename;
}
return $Return;
}

$s =pdf2png (' file/1371273225-ceshi_ppt.pdf ', ' 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.