需要安装imagick及其php扩展,一共有二个函数,都是从网上摘下来的,还没测试,这个方案应该可行,可以简单的仿照在线文档功能,有机会在项目中实践一下.
第一种,代码如下:
-
-
-
-
-
-
-
- function pdf2png($pdf,$path,$page=0)
- {
- if(!is_dir($path))
- {
- mkdir($path,true);
- }
- if(!extension_loaded('imagick'))
- {
- echo '没有找到imagick!' ;
- return false;
- }
- if(!file_exists($pdf))
- {
- echo '没有找到pdf' ;
- return false;
- }
- $im = new Imagick();
- $im->setResolution(120,120);
- $im->setCompressionQuality(80);
- $im->readImage($pdf."[".$page."]");
-
- $im->scaleImage(200,100,true);
- $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"><img src="'.$s.'"></div>';
第二种,代码如下:
- 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;
- }
- }
- return $Return;
- }
创建一个jpg缩略图并显示出来,代码如下:
- <?php
- header('Content-type: image/jpeg');
- $image = new Imagick('image.jpg');
-
- $image->thumbnailImage(100, 0);
- echo $image;
- ?>
缩略GIF动画图片,代码如下:
- <?php
-
- $im = new Imagick("example.gif");
-
- foreach ($im as $frame) {
-
- $frame->thumbnailImage(50, 50);
-
- $frame->setImagePage(50, 50, 0, 0);
- }
- $im->writeImages("example_small.gif", true);
- ?>
|