php 将pdf转成图片且将图片拼接
说明:
1.pdf转图片通过安装php扩展imagick实现。
2.由于windows扩展安装的一系列问题,建议在linux环境开发,windows大伙可以尝试安装。
3.为Centos 安装ImageMagick imagick for php步骤。ImageMagick是一套软件系列,主要用于图片的创建、编辑以及转换等 (安装方式有多种,本文只介绍一种安装方式)
(一)安装步骤:
1. 下载安装ImageMagick
1 2 3 4 5 6 | wget ftp: //mirror.aarnet.edu.au/pub/imagemagick/ImageMagick-6.6.8-10.tar.gz tar -xzvf ImageMagick-6.6.8-10.tar.gz ./configure --prefix=/usr/local/imagemagick make make install |
2.下载安装Imagick
地址:http://pecl.php.net/package/imagick
1 2 3 4 5 6 7 | wget http: //pecl.php.net/get/imagick-3.1.0RC1.tgz tar -xzvf imagick-3.1.0RC1 phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/local/imagemagick make make install |
3.手动配置php.ini
1 2 3 4 | Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ Installing header files: /usr/local/php/ include /php/ 生成imagick.so到/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ |
至此,php扩展安装完成。
(二)使用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | /** * PDF2PNG * @param $pdf 待处理的PDF文件 * @param $path 待保存的图片路径 * @param $page 待导出的页面 -1为全部 0为第一页 1为第二页 * @return 保存好的图片路径和文件名 * 注:此处为坑 对于Imagick中的$pdf路径 和$path路径来说, php版本为5+ 可以使用相对路径。php7+版本必须使用绝对路径。所以,建议大伙使用绝对路径。 */ function pdf2png( $pdf , $path , $page =-1) { if (! extension_loaded ( 'imagick' )) { return false; } if (! file_exists ( $pdf )) { return false; } if (! is_readable ( $pdf )) { return false; } $im = new Imagick(); $im ->setResolution(150,150); $im ->setCompressionQuality(100); if ( $page ==-1) $im ->readImage( $pdf ); else $im ->readImage( $pdf . "[" . $page . "]" ); foreach ( $im as $Key => $Var ) { $Var ->setImageFormat( 'png' ); $filename = $path . md5( $Key .time()). '.png' ; if ( $Var ->writeImage( $filename ) == true) { $Return [] = $filename ; } } //返回转化图片数组,由于pdf可能多页,此处返回二维数组。 return $Return ; } /** * Spliceimg * @param array $imgs pdf转化png 路径 * @param string $path 待保存的图片路径 * @return string 将多个图片拼接为成图的路径 * 注:多页的pdf转化为图片后拼接方法 */ function Spliceimg( $imgs = array (), $img_path = '' ) { //自定义宽度 $width = 1230; //获取总高度 $pic_tall = 0; foreach ( $imgs as $key => $value ) { $info = getimagesize ( $value ); $pic_tall += $width / $info [0]* $info [1]; } // 创建长图 $temp = imagecreatetruecolor( $width , $pic_tall ); //分配一个白色底色 $color = imagecolorAllocate( $temp ,255,255,255); imagefill( $temp ,0,0, $color ); $target_img = $temp ; $source = array (); foreach ( $imgs as $k => $v ) { $source [ $k ][ 'source' ] = Imagecreatefrompng( $v ); $source [ $k ][ 'size' ] = getimagesize ( $v ); } $num = 1; $tmp = 1; $tmpy = 2; //图片之间的间距 $count = count ( $imgs ); for ( $i = 0; $i < $count ; $i ++) { imagecopy( $target_img , $source [ $i ][ 'source' ], $tmp , $tmpy , 0, 0, $source [ $i ][ 'size' ][0], $source [ $i ][ 'size' ][1]); $tmpy = $tmpy + $source [ $i ][ 'size' ][1]; //释放资源内存 imagedestroy( $source [ $i ][ 'source' ]); } $returnfile = $img_path . date ( 'Y-m-d' ); if (! file_exists ( $returnfile )) { if (!make_dir( $returnfile )) { /* 创建目录失败 */ return false; } } $return_imgpath = $returnfile . '/' .md5(time(). $pic_tall . 'pdftopng' ). '.png' ; imagepng( $target_img , $return_imgpath ); return $return_imgpath ; } /** * make_dir * @param string $folder 生成目录地址 * 注:生成目录方法 */ function make_dir( $folder ) { $reval = false; if (! file_exists ( $folder )) { /* 如果目录不存在则尝试创建该目录 */ @umask(0); /* 将目录路径拆分成数组 */ preg_match_all( '/([^\/]*)\/?/i' , $folder , $atmp ); /* 如果第一个字符为/则当作物理路径处理 */ $base = ( $atmp [0][0] == '/' ) ? '/' : '' ; /* 遍历包含路径信息的数组 */ foreach ( $atmp [1] AS $val ) { if ( '' != $val ) { $base .= $val ; if ( '..' == $val || '.' == $val ) { /* 如果目录为.或者..则直接补/继续下一个循环 */ $base .= '/' ; continue ; } } else { continue ; } $base .= '/' ; if (! file_exists ( $base )) { /* 尝试创建目录,如果创建失败则继续循环 */ if (@ mkdir (rtrim( $base , '/' ), 0777)) { @ chmod ( $base , 0777); $reval = true; } } } } else { /* 路径已经存在。返回该路径是不是一个目录 */ $reval = is_dir ( $folder ); } clearstatcache(); return $reval ; } |
调用方法,实现将pdf 转为图片 进而拼接图片的功能~over~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
· 7 个最近很火的开源项目「GitHub 热点速览」
· DeepSeekV3:写代码很强了
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· Visual Studio 2022 v17.13新版发布:强化稳定性和安全,助力 .NET 开发提
· AI浏览器自动化实战