<?php namespace appmapicommonimage; /** * 拼接多幅图片成为一张图片 * * 参数说明:原图片为文件路径数组,目的图片如果留空,则不保存结果 * * 例子: * <code> * $ci = new CombineImage(array("./uploads/1.jpg", "./uploads/2.png"), "./uploads/3.png"); * $ci->combine(); * $ci->show(); * </code> * * @author yangjianhui * @version 2020/6/13 */ class CombineImage { /** * 原图地址数组 */ private $srcImages; /** * 每张图片缩放到这个宽度 */ private $width; /** * 每张图片缩放到这个高度 */ private $height; /** * 目标图片地址 */ private $destImage; /** * 临时画布 */ private $canvas; /** * CombineImage constructor. * * @param array $srcImages 需要图片路径数组 * @param string $desImage 输出目标图片地址 * @param int $width 输出后图片宽度 * @param int $height 输出后图片高度 */ public function __construct(array $srcImages, $desImage = '', $width = 750, $height = 12144) { $this->srcImages = $srcImages; $this->destImage = $desImage; $this->width = $width; $this->height = $height; $this->canvas = NULL; } public function __destruct() { if ($this->canvas != NULL) { imagedestroy($this->canvas); } } /** * 合并图片 */ public function combine() { if (empty($this->srcImages) || $this->width == 0 || $this->height == 0) { return; } /*压缩图片 计算压缩后的图片高度*/ /*获取所有图片高度*/ $heightAll = 0; for ($i = 0; $i < count($this->srcImages); $i++) { $srcImage = $this->srcImages[$i]; list($srcWidth, $srcHeight, $fileType) = getimagesize($srcImage); if ($fileType == 2) { $srcImage = imagecreatefromjpeg($srcImage); } else if ($fileType == 3) { $srcImage = imagecreatefrompng($srcImage); } else { continue; } $new_width = $srcWidth;//压缩后的图片宽 $new_height = $srcHeight;//压缩后的图片高 if($srcWidth >= 750){ $per = 750 / $srcWidth;//计算比例 $new_width = $srcWidth * $per; $new_height = $srcHeight * $per; } // $heightAll+=$srcHeight; $heightAll+=$new_height; } /*设置画布总高度 并固定宽度 开始压缩合成图片*/ $this->height = $heightAll; $this->width = 750; $this->createCanvas(); for ($i = 0; $i < count($this->srcImages); $i++) { $srcImage = $this->srcImages[$i]; //获取原图的基本信息(切记不要https) list($srcWidth, $srcHeight, $fileType) = getimagesize($srcImage); $new_width = $srcWidth;//压缩后的图片宽 $new_height = $srcHeight;//压缩后的图片高 if($srcWidth >= 750){ $per = 750 / $srcWidth;//计算比例 $new_width = $srcWidth * $per; $new_height = $srcHeight * $per; } if ($fileType == 2) { // 原图是 jpg 类型 $srcImage = imagecreatefromjpeg($srcImage); } else if ($fileType == 3) { // 原图是 png 类型 $srcImage = imagecreatefrompng($srcImage); } else { // 无法识别的类型 continue; } // 计算当前原图片应该位于画布的哪个位置 $destX = 0; if ($i == 0) { $desyY = 0; $desyY_pre = $new_height; } else { $desyY += $desyY_pre; $desyY_pre = $new_height; } // $desyY += $srcHeight; // echo $i."--".$srcHeight."--".$desyY." "; imagecopyresampled($this->canvas, $srcImage, $destX, $desyY, 0, 0, $new_width, $new_height, $srcWidth, $srcHeight); // echo $desyY.'--'; } // die; // 如果有指定目标地址,则输出到文件 if (!empty($this->destImage)) { $this->output(); } } /** * 输出结果到浏览器 */ public function show() { if ($this->canvas == NULL) { return; } header("Content-type: image/jpeg"); imagejpeg($this->canvas); } /** * 私有函数,创建画布 */ private function createCanvas() { ini_set('memory_limit', '256M'); $this->canvas = imagecreatetruecolor($this->width, $this->height); // 使画布透明 $white = imagecolorallocate($this->canvas, 255, 255, 255); imagefill($this->canvas, 0, 0, $white); imagecolortransparent($this->canvas, $white); } /** * 私有函数,保存结果到文件 */ private function output() { // 获取目标文件的后缀 $fileType = substr(strrchr($this->destImage, '.'), 1); if ($fileType == 'jpg' || $fileType == 'jpeg') { imagejpeg($this->canvas, $this->destImage); } else { // 默认输出 png 图片 imagepng($this->canvas, $this->destImage); } } /*图片压缩*/ public function compressedImage($imgsrc, $imgdst) { list($width, $height, $type) = getimagesize($imgsrc); $new_width = $width;//压缩后的图片宽 $new_height = $height;//压缩后的图片高 if($width >= 750){ $per = 750 / $width;//计算比例 $new_width = $width * $per; $new_height = $height * $per; } switch ($type) { case 1: $giftype = check_gifcartoon($imgsrc); if ($giftype) { header('Content-Type:image/gif'); $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是质量、压缩图片容量大小 imagejpeg($image_wp, $imgdst, 90); imagedestroy($image_wp); imagedestroy($image); } break; case 2: header('Content-Type:image/jpeg'); $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是质量、压缩图片容量大小 imagejpeg($image_wp, $imgdst, 90); imagedestroy($image_wp); imagedestroy($image); break; case 3: header('Content-Type:image/png'); $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是质量、压缩图片容量大小 imagejpeg($image_wp, $imgdst, 90); imagedestroy($image_wp); imagedestroy($image); break; } } } ?>