{"id":2658,"date":"2023-08-30T23:31:36","date_gmt":"2023-08-30T15:31:36","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2658"},"modified":"2023-08-30T23:31:36","modified_gmt":"2023-08-30T15:31:36","slug":"2658","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2658.html","title":{"rendered":"\u5982\u4f55\u5229\u7528PHP\u5c06PDF\u8f6c\u4e3a\u56fe\u7247\u4ee5\u53ca\u62fc\u63a5\u56fe\u7247\uff08\u9644\u4ee3\u7801\uff09"},"content":{"rendered":"

<\/p>\n

\u5bf9\u4e8e\u5982\u4f55\u5229\u7528PHP\u5c06PDF\u8f6c\u4e3a\u56fe\u7247\uff0c\u5176\u4e2d\u6700\u4e3b\u8981\u7684\u662f\u9700\u8981\u5728\u670d\u52a1\u5668\u7aef\u5b89\u88c5ImageMagick\u6269\u5c55\u3002\u6709\u4e86ImageMagick\u6269\u5c55\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n

* @param $pdf \u5f85\u5904\u7406\u7684PDF\u6587\u4ef6
* @param $path \u5f85\u4fdd\u5b58\u7684\u56fe\u7247\u8def\u5f84
* @param int|\u5f85\u5bfc\u51fa\u7684\u9875\u9762 $page \u5f85\u5bfc\u51fa\u7684\u9875\u9762 -1\u4e3a\u5168\u90e8 0\u4e3a\u7b2c\u4e00\u9875 1\u4e3a\u7b2c\u4e8c\u9875
* @return \u4fdd\u5b58\u597d\u7684\u56fe\u7247\u8def\u5f84\u548c\u6587\u4ef6\u540d \u6ce8\uff1a\u6b64\u5904\u4e3a\u5751 \u5bf9\u4e8eImagick\u4e2d\u7684$pdf\u8def\u5f84 \u548c$path\u8def\u5f84\u6765\u8bf4\uff0c php\u7248\u672c\u4e3a5+ \u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3002php7+\u7248\u672c\u5fc5\u987b\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002\u6240\u4ee5\uff0c\u5efa\u8bae\u5927\u4f19\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002
\u6ce8\uff1a\u6b64\u5904\u4e3a\u5751 \u5bf9\u4e8eImagick\u4e2d\u7684$pdf\u8def\u5f84 \u548c$path\u8def\u5f84\u6765\u8bf4\uff0c php\u7248\u672c\u4e3a5+ \u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3002php7+\u7248\u672c\u5fc5\u987b\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002\u6240\u4ee5\uff0c\u5efa\u8bae\u5927\u4f19\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n

\u5177\u4f53\u5982\u4f55\u5229\u7528PHP\u5c06PDF\u8f6c\u4e3a\u56fe\u7247\u8bf7\u770b\u5982\u4e0b\u5177\u4f53\u5b9e\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n

<?php\r\n\/**\r\n * Created by PhpStorm.\r\n * User: Administrator\r\n * Date Time: 2023\/08\/30 23:31\r\n * Author: Mk\r\n * Email: 304276099@qq.com\r\n *\/\r\n \r\nnamespace utils;\r\n \r\n \r\nclass Handler\r\n{\r\n    \/**\r\n     * PDF2PNG\r\n     * @param $pdf  \u5f85\u5904\u7406\u7684PDF\u6587\u4ef6\r\n     * @param $path \u5f85\u4fdd\u5b58\u7684\u56fe\u7247\u8def\u5f84\r\n     * @param int|\u5f85\u5bfc\u51fa\u7684\u9875\u9762 $page \u5f85\u5bfc\u51fa\u7684\u9875\u9762 -1\u4e3a\u5168\u90e8 0\u4e3a\u7b2c\u4e00\u9875 1\u4e3a\u7b2c\u4e8c\u9875\r\n     * @return \u4fdd\u5b58\u597d\u7684\u56fe\u7247\u8def\u5f84\u548c\u6587\u4ef6\u540d \u6ce8\uff1a\u6b64\u5904\u4e3a\u5751 \u5bf9\u4e8eImagick\u4e2d\u7684$pdf\u8def\u5f84 \u548c$path\u8def\u5f84\u6765\u8bf4\uff0c   php\u7248\u672c\u4e3a5+ \u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3002php7+\u7248\u672c\u5fc5\u987b\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002\u6240\u4ee5\uff0c\u5efa\u8bae\u5927\u4f19\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002\r\n     * \u6ce8\uff1a\u6b64\u5904\u4e3a\u5751 \u5bf9\u4e8eImagick\u4e2d\u7684$pdf\u8def\u5f84 \u548c$path\u8def\u5f84\u6765\u8bf4\uff0c   php\u7248\u672c\u4e3a5+ \u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3002php7+\u7248\u672c\u5fc5\u987b\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002\u6240\u4ee5\uff0c\u5efa\u8bae\u5927\u4f19\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002\r\n     *\/\r\n    static function pdfToPng($pdf,$path,$page=-1)\r\n    {\r\n        if(!extension_loaded('imagick'))\r\n        {\r\n            return 1;\r\n        }\r\n        if(!file_exists($pdf))\r\n        {\r\n            return 2;\r\n        }\r\n        if(!is_readable($pdf))\r\n        {\r\n            return 3;\r\n        }\r\n        $im = new \\Imagick();\r\n        $im->setResolution(150,150);\r\n        $im->setCompressionQuality(100);\r\n        if($page==-1){\r\n            $im->readImage($pdf);\r\n        } else{\r\n            $im->readImage($pdf."[".$page."]");\r\n        }\r\n \r\n        foreach ($im as $Key => $Var)\r\n        {\r\n            $Var->setImageFormat('png');\r\n            $filename = $path. md5($Key.time()).'.png';\r\n            if($Var->writeImage($filename) == true)\r\n            {\r\n                $Return[] = $filename;\r\n            }\r\n        }\r\n        \/\/\u8fd4\u56de\u8f6c\u5316\u56fe\u7247\u6570\u7ec4\uff0c\u7531\u4e8epdf\u53ef\u80fd\u591a\u9875\uff0c\u6b64\u5904\u8fd4\u56de\u4e8c\u7ef4\u6570\u7ec4\u3002\r\n        return $Return;\r\n    }\r\n \r\n \r\n    \/**\r\n     * spliceimg\r\n     * @param array $imgs pdf\u8f6c\u5316png  \u8def\u5f84\r\n     * @param string $img_path\r\n     * @return string \u5c06\u591a\u4e2a\u56fe\u7247\u62fc\u63a5\u4e3a\u6210\u56fe\u7684\u8def\u5f84\r\n     * \u6ce8\uff1a\u591a\u9875\u7684pdf\u8f6c\u5316\u4e3a\u56fe\u7247\u540e\u62fc\u63a5\u65b9\u6cd5\r\n     * @internal param string $path \u5f85\u4fdd\u5b58\u7684\u56fe\u7247\u8def\u5f84\r\n     *\/\r\n    static function spliceImg($imgs = array(),$img_path = '')\r\n    {\r\n \r\n        $width = 500; \/\/\u81ea\u5b9a\u4e49\u5bbd\u5ea6\r\n        $height = null;\r\n        $pic_tall = 0;\/\/\u83b7\u53d6\u603b\u9ad8\u5ea6\r\n        foreach ($imgs as $key => $value) {\r\n            $arr = getimagesize($value);\r\n            $height = $width\/$arr[0]*$arr[1];\r\n            $pic_tall += $height;\r\n        }\r\n        $pic_tall = intval($pic_tall);\r\n        \/\/ \u521b\u5efa\u957f\u56fe\r\n        $targetImg = imagecreatetruecolor($width,$pic_tall);\r\n        \/\/\u5206\u914d\u4e00\u4e2a\u767d\u8272\u5e95\u8272\r\n        $color = imagecolorAllocate($targetImg,255,255,255);\r\n        imagefill($targetImg,0,0,$color);\r\n \r\n        $tmp  = 0;\r\n        $tmpy = 0; \/\/\u56fe\u7247\u4e4b\u95f4\u7684\u95f4\u8ddd\r\n        $src = null;\r\n        $size = null;\r\n        foreach ($imgs as $k => $v) {\r\n            $src = Imagecreatefrompng($v);\r\n            $size = getimagesize($v);\r\n            \/\/5.\u8fdb\u884c\u7f29\u653e\r\n            imagecopyresampled($targetImg,$src, $tmp, $tmpy,0,0,$width,$height,$size[0],$size[1]);\r\n            \/\/imagecopy($targetImg, $src, $tmp, $tmpy, 0, 0, $size[0],$size[1]);\r\n            $tmpy = $tmpy + $height;\r\n            \/\/\u91ca\u653e\u8d44\u6e90\u5185\u5b58\r\n            imagedestroy($src);\r\n            unlink($imgs[$k]);\r\n        }\r\n        $returnfile = $img_path.date('Y-m-d');\r\n        if (!file_exists($returnfile))\r\n        {\r\n            if (!self::makeDir($returnfile))\r\n            {\r\n                \/* \u521b\u5efa\u76ee\u5f55\u5931\u8d25 *\/\r\n                return false;\r\n            }\r\n        }\r\n        $return_imgpath = $returnfile.'\/'.md5(time().$pic_tall.'pdftopng').'.png';\r\n        imagepng($targetImg,$return_imgpath);\r\n        return $return_imgpath;\r\n \r\n    }\r\n \r\n \r\n    \/**\r\n     * makeDir\r\n     * @param string $folder \u751f\u6210\u76ee\u5f55\u5730\u5740\r\n     * \u6ce8\uff1a\u751f\u6210\u76ee\u5f55\u65b9\u6cd5\r\n     * @return bool\r\n     *\/\r\n    static function makeDir($folder)\r\n    {\r\n        $reval = false;\r\n        if (!file_exists($folder))\r\n        {\r\n            \/* \u5982\u679c\u76ee\u5f55\u4e0d\u5b58\u5728\u5219\u5c1d\u8bd5\u521b\u5efa\u8be5\u76ee\u5f55 *\/\r\n            @umask(0);\r\n            \/* \u5c06\u76ee\u5f55\u8def\u5f84\u62c6\u5206\u6210\u6570\u7ec4 *\/\r\n            preg_match_all('\/([^\\\/]*)\\\/?\/i', $folder, $atmp);\r\n            \/* \u5982\u679c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e3a\/\u5219\u5f53\u4f5c\u7269\u7406\u8def\u5f84\u5904\u7406 *\/\r\n            $base = ($atmp[0][0] == '\/') ? '\/' : '';\r\n            \/* \u904d\u5386\u5305\u542b\u8def\u5f84\u4fe1\u606f\u7684\u6570\u7ec4 *\/\r\n            foreach ($atmp[1] AS $val)\r\n            {\r\n                if ('' != $val)\r\n                {\r\n                    $base .= $val;\r\n                    if ('..' == $val || '.' == $val)\r\n                    {\r\n                        \/* \u5982\u679c\u76ee\u5f55\u4e3a.\u6216\u8005..\u5219\u76f4\u63a5\u8865\/\u7ee7\u7eed\u4e0b\u4e00\u4e2a\u5faa\u73af *\/\r\n                        $base .= '\/';\r\n                        continue;\r\n                    }\r\n                }\r\n                else\r\n                {\r\n                    continue;\r\n                }\r\n                $base .= '\/';\r\n                if (!file_exists($base))\r\n                {\r\n                    \/* \u5c1d\u8bd5\u521b\u5efa\u76ee\u5f55\uff0c\u5982\u679c\u521b\u5efa\u5931\u8d25\u5219\u7ee7\u7eed\u5faa\u73af *\/\r\n                    if (@mkdir(rtrim($base, '\/'), 0777))\r\n                    {\r\n                        @chmod($base, 0777);\r\n                        $reval = true;\r\n                    }\r\n                }\r\n            }\r\n        }\r\n        else\r\n        {\r\n            \/* \u8def\u5f84\u5df2\u7ecf\u5b58\u5728\u3002\u8fd4\u56de\u8be5\u8def\u5f84\u662f\u4e0d\u662f\u4e00\u4e2a\u76ee\u5f55 *\/\r\n            $reval = is_dir($folder);\r\n        }\r\n        clearstatcache();\r\n        return $reval;\r\n    }\r\n}<\/pre>\n

\u8c03\u7528\u8bed\u53e5\u628apdf\u8f6c\u6210png\uff0c\u5e76\u62fc\u63a5\u56fe\u7247\uff1a\u628apdf\u8f6c\u6210png\uff0c\u5e76\u62fc\u63a5\u56fe\u7247\uff08\u517c\u5bb9ios\u9884\u89c8\u65e0\u6cd5\u663e\u793a\u7b7e\u7ae0\u95ee\u9898\uff09<\/strong><\/p>\n

\/\/ \u628apdf\u8f6c\u6210png\uff0c\u5e76\u62fc\u63a5\u56fe\u7247\uff08\u517c\u5bb9ios\u9884\u89c8\u65e0\u6cd5\u663e\u793a\u7b7e\u7ae0\u95ee\u9898\uff09\r\n        $imgArr = Handler::pdfToPng($path,$_SERVER['DOCUMENT_ROOT'].'\/file\/');\r\n        $img = '';\r\n        if($imgArr&&is_array($imgArr)){\r\n            $img = Handler::spliceImg($imgArr,$_SERVER['DOCUMENT_ROOT'].'\/file\/agreement\/');\r\n        }<\/pre>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5bf9\u4e8e\u5982\u4f55\u5229\u7528PHP\u5c06PDF\u8f6c\u4e3a\u56fe\u7247\uff0c\u5176\u4e2d\u6700\u4e3b\u8981\u7684\u662f\u9700\u8981\u5728\u670d\u52a1\u5668\u7aef\u5b89\u88c5ImageMagick\u6269\u5c55\u3002\u6709\u4e86Image […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-2658","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2658"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2658"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2658\/revisions"}],"predecessor-version":[{"id":2659,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2658\/revisions\/2659"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}