二. 将PDF转成jpg图片
过程:
1. 安装Imagick扩展.
2. 安装ImageMagick功能
3. 安装ghostScript功能
安装完成后, 可以开始测试
因为有些发票带销货清单,和发票纸张方向不同,必须做标记,下一步函数才能识别,根据方向来新建PDF页面. FPDF中,p是横向,L是坚向,
在应用过程中,发现png占用量少,就全用png, 没用Jpg
/* * 分页转图片 */ function pdfToJpg($from_path,$target_path){ $arr = array(); try{ $img = new Imagick(); $img->setCompressionQuality(100); $img->setResolution(180,180); $img->readImage($from_path); $imgNum = $img->getNumberImages(); foreach ($img as $k => $sub){ $canvas = new Imagick(); //定义新图片 $sub->setImageFormat('png'); $sub->stripImage(); $sub->trimImage(0); $width = $sub->getImageWidth() + 100; $height = $sub->getImageHeight() + 100; //正常转图片画布 $canvas->newImage($width,$height,new ImagickPixel('white')); $canvas->compositeImage($sub,Imagick::COMPOSITE_DEFAULT,50,50); $canvas->resetIterator(); //给横向的图片做一个标记 if($width<$height) { $canvas->appendImages(true)->writeImage($target_path.'_'.$k.'_p.png'); $arr[] = [$target_path.'_'.$k.'_p.png']; } else { $canvas->appendImages(true)->writeImage($target_path.'_'.$k.'.png'); $arr[] = [$target_path.'_'.$k.'.png']; } //转发票二维码要的画布 //$canvas->newImage(200,200,new ImagickPixel('white')); //$canvas->compositeImage($sub,Imagick::COMPOSITE_DEFAULT,5,5); } return $arr; }catch (Exception $e){ echo $e->getMessage(); echo $e->getTraceAsString(); return false; } }