【舊代碼整理】一個處理圖片的class,拼接圖片,在圖片上下加文字

function是網上找來改改的,然後做成了一個Class

字體文件是 微軟雅黑 yahei.ttf  搜一搜可以下載到。

<?php
/**
一個處理圖片的class:
ROOT_PATH:項目絕對路徑常量,如 /project/apple,最後沒有斜槓
1、拼接圖片 implode_images(array $images,$out_filename,橫H豎V);
2、在圖片上下寫字 image_bind_text($pic,$text_top,$text_bottom,$out_pic_path,$height=30);
image_bind_text(輸入圖片路徑,圖片上面的文字,圖片下面的文字,輸出圖片路徑,文字高度);

像下面這樣使用
$image = Model_Image::instance();
$image->implode_images(................);
**/


class Model_Image {
	protected static $_instance = null;
	public static function instance(){
		if(self::$_instance == null){
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	public function implode_images($images, $out_filename, $align = 'V') {
		//header('Content-type:image/jpeg');
		if(!is_array($images) || count($images) <= 1) {
			return false;
		}
		$images_info = array();
		foreach($images as $image) {
			if($info = getimagesize($image))
				$images_info[] = $info;
			else
				return false;
		}
		unset($info);
		$the_max_width = 0;
		$the_max_height = 0;
		$total_height = 0;
		$total_width = 0;
		foreach($images_info as $info) {
			if($info[0] > $the_max_width) $the_max_width = $info[0];
			if($info[1] > $the_max_height) $the_max_height = $info[1];
			$total_width += $info[0];
			$total_height += $info[1];
		}
		$the_min_width = $the_max_width;
		$the_min_height = $the_max_height;
		foreach($images_info as $info) {
			if($info[0] < $the_min_width) $the_min_width = $info[0];
			if($info[1] < $the_min_height) $the_min_height = $info[1];
		}

		if(strtoupper($align) == 'V') {

			$images_info_new = array();
			foreach($images_info as $value){
				$new_info = $value;
				if($value[0] > $the_min_width){
					$new_info['w_o'] = $new_info[0];
					$new_info['h_o'] = $new_info[1];
					$new_info[1] = $new_info[1] / ($new_info[0] / $the_min_width);
					$new_info[1] = intval($new_info[1]);
					$new_info[0] = $the_min_width;
				}
				$images_info_new[] = $new_info;
			}

			$images_info = $images_info_new;
			
			$the_max_width = 0;
			$the_max_height = 0;
			$total_height = 0;
			$total_width = 0;
			foreach($images_info as $info) {
				if($info[0] > $the_max_width) $the_max_width = $info[0];
				if($info[1] > $the_max_height) $the_max_height = $info[1];
				$total_width += $info[0];
				$total_height += $info[1];
			}

			if(function_exists('imagecreatetruecolor')){
				$dst_im = imagecreatetruecolor($the_max_width, $total_height);
			} else {
				$dst_im = imagecreate($the_max_width, $total_height);
			}
			$startX = 0;
			$startY = 0;
			for($i = 0; $i < count($images); $i++) {
				//print_r($images_info);die();

				switch($images_info[$i][2])
				{ 
					case 1: 
						$src_im = imagecreatefromgif($images[$i]);
						
						break; 
					case 2: 
						$src_im = imagecreatefromjpeg($images[$i]);
						break; 
					case 3: 
						$src_im = imagecreatefrompng($images[$i]);
						break; 
				}
				if(!empty($images_info[$i]['w_o'])){
					$width = $images_info[$i]['w_o'];
					$height = $images_info[$i]['h_o'];
					$source = $src_im;
					$newwidth = $images_info[$i][0];
					$newheight = $images_info[$i][1];
					
					$thumb = imagecreatetruecolor($newwidth, $newheight);
					imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
					$src_im = $thumb;
				}

				imagecopymerge($dst_im, $src_im, $startX , $startY , 0, 0, $images_info[$i][0] , $images_info[$i][1] , 100);
				$startY += $images_info[$i][1];
				imagedestroy($src_im);
			}
			imagejpeg($dst_im,$out_filename);
		} elseif(strtoupper($align) == 'H') {
			
			$images_info_new = array();
			foreach($images_info as $value){
				$new_info = $value;
				if($value[1] > $the_min_height){
					$new_info['w_o'] = $new_info[0];
					$new_info['h_o'] = $new_info[1];
					$new_info[0] = $new_info[0] / ($new_info[1] / $the_min_height);
					$new_info[0] = intval($new_info[0]);
					$new_info[1] = $the_min_height;
				}
				$images_info_new[] = $new_info;
			}

			$images_info = $images_info_new;

			$the_max_width = 0;
			$the_max_height = 0;
			$total_height = 0;
			$total_width = 0;
			foreach($images_info as $info) {
				if($info[0] > $the_max_width) $the_max_width = $info[0];
				if($info[1] > $the_max_height) $the_max_height = $info[1];
				$total_width += $info[0];
				$total_height += $info[1];
			}

			if(function_exists('imagecreatetruecolor')) {
				$dst_im = imagecreatetruecolor($total_width, $the_max_height);
			} else {
				$dst_im = imagecreate($total_width, $the_max_height);
			}
			$startX = 0;
			$startY = 0;
			for($i = 0; $i < count($images); $i++) {
				switch($images_info[$i][2])
				{ 
					case 1: 
						$src_im = imagecreatefromgif($images[$i]);
						break; 
					case 2: 
						$src_im = imagecreatefromjpeg($images[$i]);
						break; 
					case 3: 
						$src_im = imagecreatefrompng($images[$i]);
						break; 
				}

				if(!empty($images_info[$i]['h_o'])){
					$width = $images_info[$i]['w_o'];
					$height = $images_info[$i]['h_o'];
					$source = $src_im;
					$newwidth = $images_info[$i][0];
					$newheight = $images_info[$i][1];
					
					$thumb = imagecreatetruecolor($newwidth, $newheight);
					imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
					$src_im = $thumb;
				}

				imagecopymerge($dst_im, $src_im, $startX , $startY , 0, 0, $images_info[$i][0] , $images_info[$i][1] , 100);
				$startX += $images_info[$i][0];
				imagedestroy($src_im);
			}
			imagejpeg($dst_im,$out_filename);
		}
	}

	public function image_bind_text($pic,$text_top,$text_bottom,$out_pic_path,$height=60){
		
		$rand_str = rand(11111,99999) . rand(11111,99999);

		$s = getimagesize($pic);

		$w = $s[0];

		$image = imagecreate($w, $height); // width = 800, height = 600
		$bg    = imagecolorallocate($image, 255, 255, 255); // 背景色

		$font  = ROOT_PATH.'/ttf/yahei.ttf'; // 字型
		$black = imagecolorallocate($image, 0, 0, 0); // 字的顏色

		// imagettftext($image, 大小, 旋轉, 與左邊的距離, 與上面的距離, $black, $font, $text);
		imagettftext($image, 12, 0, 10, 25, $black, $font, $text_top);

		$out_1 = ROOT_PATH."/temp/out_pic_1_{$rand_str}.jpeg";

		imagejpeg($image,$out_1);
		
		imagedestroy($image);

		$image = imagecreate($w, $height); // width = 800, height = 600
		$bg    = imagecolorallocate($image, 255, 255, 255); // 背景色

		$font  = ROOT_PATH.'/ttf/yahei.ttf'; // 字型
		$black = imagecolorallocate($image, 0, 0, 0); // 字的顏色

		// imagettftext($image, 大小, 旋轉, 與左邊的距離, 與上面的距離, $black, $font, $text);
		imagettftext($image, 12, 0, 10, 30, $black, $font, $text_bottom);

		$out_2 = ROOT_PATH."/temp/out_pic_2_{$rand_str}.jpeg";

		imagejpeg($image,$out_2);

		imagedestroy($image);

		$images = array($out_1,$pic,$out_2);

		$this->implode_images($images,$out_pic_path);

		unlink($out_1);

		unlink($out_2);

	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章