您的位置:首页 > 编程语言

【旧代码整理】一个处理图片的class,拼接图片,在图片上下加文字

2016-07-15 09:57 393 查看
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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片