First Kind
| 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;
} www.111cn.net
$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> '; |
Second Kind
| The code is as follows |
Copy Code |
function Pdf2png ($PDF, $Path) {
if (!extension_loaded (' Imagick ')) {
return false;
}
if (!file_exists ($PDF)) {
return false;
}
$IM = new Imagick ();
$IM->setresolution (120,120);
$IM->setcompressionquality (100);
$IM->readimage ($PDF);
foreach ($IM as $Key => $Var) {
$Var->setimageformat (' png ');
$Filename = $Path. ' /'. MD5 ($Key. Time ()). PNG ';
if ($Var->writeimage ($Filename) = = True) {
$Return [] = $Filename;
} www.111cn.net
}
return $Return;
} |
Create a JPG 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
| code is as follows |
copy code |
<?php
/ * Create a new Imagick object and read in GIF */
$im = new Imagick ("Example.gif"),
/* Resize All frames */
for Each ($im as $frame) {
/* 50x50 frames */
$frame->thumbnailimage;
/* Set the virtual canvas to Corr ECT size */
$frame->setimagepage (0, 0);
/* Notice writeimages instead of Writeimage */
$im->writeimages ("Example_small.gif", true);
|