【旧代码整理】一个处理图片的class,拼接图片,在图片上下加文字
2016-07-15 09:57
393 查看
function是网上找来改改的,然后做成了一个Class
字体文件是 微软雅黑 yahei.ttf 搜一搜可以下载到。
字体文件是 微软雅黑 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); } }
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- upload上传单张图片
- 图片引发的溢出危机(图)
- C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法
- C#实现把彩色图片灰度化代码分享
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- 如何使用C#从word文档中提取图片
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- C#图片添加水印的实现代码
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法
- C#实现图片加相框的方法
- 超级经典一套鼠标控制左右滚动图片带自动翻滚
- 用css实现图片垂直居中的使用技巧
- 一起动手编写Android图片加载框架
- C++实现读取图片长度和宽度