技术饭

php通过ImageMagick类库将pdf转换成图片

copylian    0 评论    20418 浏览    2023.11.30

php通过ImageMagick类库将pdf转换成图片,Imagick 是用 ImageMagic API 来创建和修改图像的PHP官方扩展。ImageMagick 是用来创建,编辑,合并位图图像的一套组件。 它能够用于读取,转换,写入多种不同格式的图像。 包含 DPX、EXR、GIF、JPEG、JPEG-2000、PDF、PhotoCD、PNG、Postscript、SVG 和 TIFF。

如果需要支持pdf的操作,需要ghostscript的支持:https://www.php.net/manual/zh/imagick.requirements.php

ImageMagick >= 6.2.4 is required. The amount of file formats supported by Imagick depends entirely upon the amount of formats supported by your ImageMagick installation. For example, Imagemagick requires ghostscript to conduct PDF operations.

1、php安装ImageMagick类库,注意类库的版本时间是2021-12-14

    微信图片_20231130152448.png

2、安装ghostscript,选择合适的版本(与ImageMagick的版本需要相近):https://github.com/ArtifexSoftware/ghostpdl-downloads/releases?after=gs924rc2&page=2

微信图片_20231130152700.png

3、代码

/**

* 需要支持GhostScript(与Imagick相近版本)

* @param $pdf pdf文件

* @param $path 文件保存路径

* @param $single 是转成单张图片

* @param $format 输出格式:url-图片地址,base64-图片的base64编码

* @return array

* @throws ImagickException

*/

function pdf2png($pdf, $path, $single = false, $format = 'url'){

    try {

        //返回信息

        $return = [];


        //实例化Imagick

        $im = new Imagick();

        $im->setCompressionQuality(100);

        $im->setResolution(200, 200); // 设置分辨率 值越大分辨率越高

        $im->readImage($pdf); // 需要GhostScript的支持,GhostScript的版本与Imagick接近,否则可能会出现不必要的错误


        //重新绘制画布

        $canvas = new Imagick();

        $imgNum = $im->getNumberImages();

        foreach ($im as $k => $v) {

            $v->setImageFormat("png");

            $v->stripImage();

            $v->trimImage(0);

            $width = $v->getImageWidth() + 65;

            $height = $v->getImageHeight() + 65;

            if ($k + 1 == $imgNum) {

                $height += 10;

            } //最后添加10的height

            $canvas->newImage($width, $height, new ImagickPixel('white'));

            $canvas->setImageFormat("png");

            $canvas->compositeImage($v, Imagick::COMPOSITE_DEFAULT, 35, 35);


            //输出多张图片

            if (!$single) {

                $fileName = $path . substr(md5($pdf), 0, 16) . "_{$k}.png";

                if ($format == "base64") {

                    $return[] = 'data:image/png;base64,' . base64_encode($canvas->getImageBlob());

                } else if ($canvas->writeImage($fileName)) {

                    $return[] = $fileName;

                }

            }

        }


        //输出一张图片

        if ($single) {

            $fileName = $path . substr(md5($pdf), 0, 16) . ".png";

            $canvas->resetIterator();

            if ($format == "base64") {

                $return[] = 'data:image/png;base64,' . base64_encode($canvas->appendImages(true)->getImageBlob());

            } else {

                if ($canvas->appendImages(true)->writeImage($fileName)) {

                    $return[] = $fileName;

                }

            }

        }

        return $return;

    } catch (Exception $e) {

        throw $e;

    }

}


try {

        $images = pdf2png(__DIR__ . "/dealhtml/aaa.pdf", __DIR__ . "/dealhtml/html/", true, 'base64');

        print_r($images);

} catch (Exception $e){

        print_r($e->getMessage());

}


参考:

https://www.php.net/manual/zh/book.imagick.php

https://blog.csdn.net/jeff_love_marina/article/details/80838055

https://www.cnblogs.com/walterfong/p/10008596.html

https://www.cnblogs.com/xiaqiuchu/p/12545168.html

https://github.com/ArtifexSoftware/ghostpdl-downloads/releases?after=gs924rc2&page=2

只袄早~~~
感谢你的支持,我会继续努力!
扫码打赏,感谢您的支持!
php ImageMagick pdf 图片 

文明上网理性发言!

  • 还没有评论,沙发等你来抢