PHP using Imagick to generate PNG thumbnails _php tutorials

Source: Internet
Author: User
Tags imagemagick
Thumbnail image if it is a picture we directly use the PHP GD library can be implemented, this article is to introduce the Imagick PDF png thumbnail method, here we have to use a plug-in, let me show you an example.

Php_imagick what?

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

Php_imagick How to use

. Create a thumbnail and show it

The code is as follows Copy Code

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;

?>

Animated image of a thumbnail gif

The code is as follows Copy Code

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

?>

Well, it's far away, let's get to the chase.


PDF Generate PNG home thumbnail (server needs to support Imagick)

The code is as follows Copy Code

/**
* Pdf2png
* @param $pdf pending PDF file
* @param $path The path of the picture to be saved
* @param $page the page to be exported-1 for all 0 for the first page 1 for the second page
* @return saved picture path and file name.
*/
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 the image resolution
$im->setcompressionquality (80); Compression ratio

$im->readimage ($pdf. " [". $page."] "); Set the first page of the read pdf
$im->thumbnailimage (max., true); Change the size of the image
$im->scaleimage (200,100,true); Scaled small image
$filename = $path. " /". Time (). '. PNG ';

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

$s =pdf2png (' file/1371273225-ceshi_ppt.pdf ', ' images ');
echo "";

http://www.bkjia.com/PHPjc/632954.html www.bkjia.com true http://www.bkjia.com/PHPjc/632954.html techarticle thumbnail image If it is a picture we directly use the PHP GD library can be implemented, this article is to introduce the Imagick PDF png thumbnail method, here we have to use a plug-in, below I come ...

  • 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.