1. 程式人生 > >拼接多幅圖片成為一張圖片(微信底部微信和指紋合二為一)

拼接多幅圖片成為一張圖片(微信底部微信和指紋合二為一)

/**
 * 拼接多幅圖片成為一張圖片
 *
 * 引數說明:原圖片為檔案路徑陣列,目的圖片如果留空,則不儲存結果
 *
 * 例子:
 * <code>
 * $ci = new CombineImage(array("D:/Downloads/1.jpg", "D:/Downloads/2.png"), "D:/Downloads/3.png");
 * $ci->combine();
 * $ci->show();
 * </code>
 *
 * @author 張榮傑
 * @version 2012.8.9
 */
class CombineImage {
    /**
     * 原圖地址陣列
     */
private $srcImages; /** * 每張圖片縮放到這個寬度 */ private $width; /** * 每張圖片縮放到這個高度 */ private $height; /** * 拼接模式,可以選擇水平或垂直 */ private $mode; /** * 水平拼接模式常量 */ const COMBINE_MODE_HORIZONTAL = "horizontal"; /** * 垂直拼接模式常量 */ const
COMBINE_MODE_VERTICAL = "vertical"; /** * 目標圖片地址 */ private $destImage; /** * 臨時畫布 */ private $canvas; /** * 建構函式,傳入原圖地址陣列和目標圖片地址 */ public function __construct($srcImages = '', $desImage = '', $width = 410, $height = 410, $mode = self::COMBINE_MODE_HORIZONTAL) { $this
->srcImages = $srcImages; $this->destImage = $desImage; $this->width = $width; $this->height = $height; $this->mode = $mode; $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; } $this->createCanvas(); for($i=0; $i<count($this->srcImages); $i++) { $srcImage = $this->srcImages[$i]; $srcImageInfo = getimagesize($srcImage); /*echo '<pre/>'; var_dump($srcImageInfo); echo '<br/>=====';*/ // 如果能夠正確的獲取原圖的基本資訊 if ($srcImageInfo) { $srcWidth = $srcImageInfo[0]; $srcHeight = $srcImageInfo[1]; $fileType = $srcImageInfo[2]; if ($fileType == 2) { // 原圖是 jpg 型別 $srcImage = imagecreatefromjpeg($srcImage); } else if ($fileType == 3) { // 原圖是 png 型別 $srcImage = imagecreatefrompng($srcImage); } else if($fileType == 1){ // 原圖是 gift 型別 $srcImage = imagecreatefromgif($srcImage); }else { // 無法識別的型別 continue; } // 計算當前原圖片應該位於畫布的哪個位置 if ($this->mode == self::COMBINE_MODE_HORIZONTAL) { $destX = $i * ($this->width);//間距 $desyY = 0; } elseif ($this->mode == self::COMBINE_MODE_VERTICAL) { $destX = 0; $desyY = $i * $this->height; } imagecopyresampled($this->canvas, $srcImage, $destX, $desyY, 0, 0, $this->width, $this->height, $srcWidth, $srcHeight); } } // 如果有指定目標地址,則輸出到檔案 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() { $totalImage = count($this->srcImages); if ($this->mode == self::COMBINE_MODE_HORIZONTAL) { $width = $totalImage * $this->width;//間距 $height = $this->height; } else if ($this->mode == self::COMBINE_MODE_VERTICAL) { $width = $this->width; $height = $totalImage * $this->height; } $this->canvas = imagecreatetruecolor($width, $height); imagealphablending($this->canvas,false);//這裡很重要,意思是不合並顏色,直接用$img影象顏色替換,包括透明色; imagesavealpha($this->canvas,true);//這裡很重要,意思是不要丟了$thumb影象的透明色; // 使畫布透明 $white = imagecolorallocate($this->canvas, 255, 255, 255); // $bg = imagecolorallocatealpha($this->canvas , 0 , 0 , 0 , 127); imagefill($this->canvas, 0, 0, $white); // imagefill($this->canvas, 0, 0, $bg); imagecolortransparent($this->canvas, $white); // imagecolortransparent($this->canvas, $bg); } /** * 私有函式,儲存結果到檔案 */ private function output() { // 獲取目標檔案的字尾 $fileType = substr(strrchr($this->destImage, '.'), 1); if ($fileType=='jpg' || $fileType=='jpeg') { imagejpeg($this->canvas, $this->destImage); } else if($fileType=='gif'){ imagegif($this->canvas,$this->destImage); }else { // 預設輸出 png 圖片 imagepng($this->canvas, $this->destImage); } } /** * @return the $srcImages */ public function getSrcImages() { return $this->srcImages; } /** * @param Array $srcImages */ public function setSrcImages($srcImages) { $this->srcImages = $srcImages; } /** * @return the $width */ public function getWidth() { return $this->width; } /** * @param int $width */ public function setWidth($width) { $this->width = $width; } /** * @return the $height */ public function getHeight() { return $this->height; } /** * @param int $height */ public function setHeight($height) { $this->height = $height; } /** * @return the $mode */ public function getMode() { return $this->mode; } /** * @param const $mode */ public function setMode($mode) { $this->mode = $mode; } /** * @return the $destImage */ public function getDestImage() { return $this->destImage; } /** * @param String $destImage */ public function setDestImage($destImage) { $this->destImage = $destImage; } } ?>