Home > php教程 > php手册 > body text

php将pdf生成png缩略图的实例程序

WBOY
Release: 2016-05-25 16:40:28
Original
1203 people have browsed it

需要安装imagick及其php扩展,一共有二个函数,都是从网上摘下来的,还没测试,这个方案应该可行,可以简单的仿照在线文档功能,有机会在项目中实践一下.

第一种,代码如下:

<?php
/** 
 * PDF2PNG
 * @param $pdf  待处理的PDF文件
 * @param $path 待保存的图片路径
 * @param $page 待导出的页面 -1为全部 0为第一页 1为第二页
 * @return      保存好的图片路径和文件名
 */
function pdf2png($pdf, $path, $page = 0) {
    if (!is_dir($path)) {
        mkdir($path, true);
    }
    if (!extension_loaded(&#39;imagick&#39;)) {
        echo &#39;没有找到imagick!&#39;;
        return false;
    }
    if (!file_exists($pdf)) {
        echo &#39;没有找到pdf&#39;;
        return false;
    } //www.phprm.com
    $im = new Imagick();
    $im->setResolution(120, 120); //设置图像分辨率
    $im->setCompressionQuality(80); //压缩比
    $im->readImage($pdf . "[" . $page . "]"); //设置读取pdf的第一页
    //$im->thumbnailImage(200, 100, true); // 改变图像的大小
    $im->scaleImage(200, 100, true); //缩放大小图像
    $filename = $path . "/" . time() . &#39;.png&#39;;
    if ($im->writeImage($filename) == true) {
        $Return = $filename;
    }
    return $Return;
}
$s = pdf2png(&#39;file/1371273225-ceshi_ppt.pdf&#39;, &#39;images&#39;);
echo &#39;<div align="center"><img  src="&#39; . $s . &#39;" alt="php将pdf生成png缩略图的实例程序 " ></div>&#39;;
?>
Copy after login

第二种,代码如下:

<?php
function pdf2png($PDF, $Path) {
    if (!extension_loaded(&#39;imagick&#39;)) {
        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(&#39;png&#39;);
        $Filename = $Path . &#39;/&#39; . md5($Key . time()) . &#39;.png&#39;;
        if ($Var->writeImage($Filename) == true) {
            $Return[] = $Filename;
        } //www.phprm.com
        
    }
    return $Return;
}
?>
Copy after login

创建一个jpg缩略图并显示出来,代码如下:

<?php
header(&#39;Content-type: image/jpeg&#39;);
$image = new Imagick(&#39;image.jpg&#39;);
// If 0 is provided as a width or height parameter,// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
?>
Copy after login

缩略GIF动画图片,代码如下:

<?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);
?>
Copy after login


文章地址:

转载随意^^请带上本文地址!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template