【旧代码整理】一个处理图片的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);

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值