php把pdf拼成一张,php 将 pdf 转成图片且将图片拼接

【转】php 将 pdf 转成图片且将图片拼接

时间:2020-06-18    阅读次数:350   来源:博客园

说明:

1.pdf转图片通过安装php扩展imagick实现。

2.由于windows扩展安装的一系列问题,建议在linux环境开发,windows大伙可以尝试安装。

3.为Centos 安装ImageMagick imagick for php步骤。ImageMagick是一套软件系列,主要用于图片的创建、编辑以及转换等  (安装方式有多种,本文只介绍一种安装方式)

(一)安装步骤

1、下载安装ImageMagickwget 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、下载安装Imagickwget 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

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扩展安装完成。

(二)使用方法/**

* 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 

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~

【责编:Richard】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值