前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >imagick 操作 pdf 生成首页(某页)缩略图 + 总页数

imagick 操作 pdf 生成首页(某页)缩略图 + 总页数

作者头像
躺平程序员老修
发布2023-09-05 16:15:02
2970
发布2023-09-05 16:15:02
举报
文章被收录于专栏:躺平程序员老修

imagick 操作 pdf 生成首页(某页)缩略图 + 总页数

代码语言:javascript
复制
   /**
     * pdf 生成首页(某页)缩略图 + 总页数
     *
     * author shyZhen <huaixiu.zhen@gmail.com>
     * https://www.litblc.com
     *
     * @param $pdf
     * @param $tempPdfPath
     * @param $isFullPath    // 是否是全路径(带有路径+文件名)
     * @param int $page 0默认第一页
     * @param int $width
     * @param int $height
     *
     * @return array
     *
     * @throws ImagickException
     * @throws \setasign\Fpdi\PdfParser\PdfParserException
     */
    public function getPdfPage($pdf, $tempPdfPath, $isFullPath = false, $page = 0, $width = 200, $height = 200)
    {
        $page = intval($page);
        $page = $page >= 0 ? "[{$page}]" : '';

        try{
        // 解决由于pdf单页分辨率过大,导致无法成功加载的问题
        $image3 = new Imagick();
        $image3->setResolution(25, 25);
        $image3->pingImage($pdf . $page);
        $w = $image3->getImageWidth();
        $h = $image3->getImageHeight();
        if ($w > 3000 || $h > 2000) {
            throw new Exception('该PDF文件分辨率过大,无法进行解析');
        }
        $image3->clear();


        $image = new Imagick();
        // read 文件之前,需要先设置分辨率
        // 设置图片分辨率 由于需要缩略图,此处默认比较小,使用25*25
        // https://stackoverflow.com/questions/14033954/set-density-parameter-for-imagick-with-php
        // https://stackoverflow.com/questions/9783216/convert-postscript-delegate-failed/23206401#23206401
        $image->setResolution(25, 25);

        // 使用100的质量生成的文件更小
        $image->setcompressionquality(100);

        // 读取内容
        $image->readImage($pdf . $page);
        $image->stripimage();

        $image->thumbnailImage($width, $height, true);
        if ($isFullPath) {
            $result = $tempPdfPath;
        } else {
            $fileName = self::uuid().'.png';
            $result = $tempPdfPath. '/' .$fileName;
        }
        $image->writeImages($result, false);
        $image->clear();

        // 读取pdf页数
        $image2 = new Imagick();
        $image2->setResolution(25, 25);
        $image2->pingImage($pdf);
        $pageCount = $image2->getNumberImages();
        $image2->clear();
        // $pageCount = self::$fpdi->setSourceFile($pdf);  // fpdi不支持压缩过的pdf
        } catch(Exception $e) {
             throw new Exception('非法文件', $e->getCode());
        }
        return [
            'url' => $result,
            'count' => $pageCount
        ];
    }

调用示例:

代码语言:javascript
复制
    /**
     * pdf 生成首页(某页)缩略图 + 总页数 使用DEMO
     *
     * author shyZhen <huaixiu.zhen@gmail.com>
     * https://www.litblc.com
     *
     * @return array
     *
     * @throws \ImagickException
     * @throws \setasign\Fpdi\PdfParser\PdfParserException
     */
    public function getPdfPage()
    {
        $tempPdfPath = '/mnt/hgfs/platform/tempPdf';
        $pdf = '/mnt/hgfs/platform/tempPdf/ZHX.pdf';

        $tcpdfLoader = TcpdfLoader::getInstance();
        $pdfPath = $tcpdfLoader->getPdfPage($pdf, $tempPdfPath);

        return $pdfPath;
    }

注意事项

在测试阶段中,发现由于pdf首页分辨率过大导致上传失败,故需要先使用pingImage来验证:

e774df6d8a22ff16a2af165ea2bd5fd.png
e774df6d8a22ff16a2af165ea2bd5fd.png
31f8b79333e983c3d663d83abb02771.png
31f8b79333e983c3d663d83abb02771.png
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • imagick 操作 pdf 生成首页(某页)缩略图 + 总页数
  • 注意事项
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档