1. 程式人生 > 實用技巧 >記php多張圖片 合併生成豎列 縱向長圖(可用於商品詳情圖合併下載)

記php多張圖片 合併生成豎列 縱向長圖(可用於商品詳情圖合併下載)

<?php
namespace app\mapi\common\image;
/**
 * 拼接多幅圖片成為一張圖片
 *
 * 引數說明:原圖片為檔案路徑陣列,目的圖片如果留空,則不儲存結果
 *
 * 例子:
 * <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; } $heightAll+=$srcHeight; } $this->height = $heightAll; $this->createCanvas(); for ($i = 0; $i < count($this->srcImages); $i++) { $srcImage = $this->srcImages[$i]; //獲取原圖的基本資訊(切記不要https) list($srcWidth, $srcHeight, $fileType) = getimagesize($srcImage); if ($fileType == 2) { // 原圖是 jpg 型別 $srcImage = imagecreatefromjpeg($srcImage); } else if ($fileType == 3) { // 原圖是 png 型別 $srcImage = imagecreatefrompng($srcImage); } else { // 無法識別的型別 continue; } // 計算當前原圖片應該位於畫布的哪個位置 $destX = 0; if ($i == 0) { $desyY = 0; } else { $desyY += $srcHeight; } imagecopyresampled($this->canvas, $srcImage, $destX, $desyY, 0, 0, $srcWidth, $srcHeight, $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() { $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); } } } ?>