废话不多说,直接上代码,一贯作风不写注释
<?php
class WaterMark
{
private $dest;
private $color;
private $x_size;
private $y_size;
public function __construct($_x_size,$_y_size)
{
if(is_numeric($_x_size) && is_numeric($_y_size))
{
$this->x_size = $_x_size;
$this->y_size = $_y_size;
$this->createImages($_x_size,$_y_size);
}
}
private function createImages($x_size,$y_size)
{
if(is_numeric($x_size) && is_numeric($y_size))
{
$this->dest = imagecreatetruecolor($x_size,$y_size);
imagealphablending($this->dest,false);
imagesavealpha($this->dest,true);
return true;
}
return false;
}
public function setColor($red=0,$green=0,$blue=0)
{
if(is_numeric($red) && is_numeric($green) && is_numeric($blue))
{
if(is_resource($this->dest) && get_resource_type($this->dest)=='gd')
$this->color = imagecolorallocate($this->dest,$red,$green,$blue);
return $this->color;
}
return false;
}
public function setAlphaColor($red=0,$green=0,$blue=0,$alpha=80)
{
if(is_numeric($red) && is_numeric($green) && is_numeric($blue))
{
if(is_resource($this->dest) && get_resource_type($this->dest)=='gd')
$this->color = imagecolorclosestalpha($this->dest,$red,$green,$blue,$alpha);
return $this->color;
}
return false;
}
public function setTXT($text,$x=0,$y=0,$size=10,$angle=0,$color='',$fontfile='')
{
if(!empty($text) && is_resource($this->dest) && get_resource_type($this->dest)=='gd')
{
if(empty($color))$color = $this->setColor(0,0,0);
if(empty($fontfile))
$fontfile = dirname(__FILE__).'/font/simsun.ttc';
else
$fontfile = dirname(__FILE__).'/font/'.$fontfile;
if(file_exists($fontfile))
{
return imagettftext($this->dest,$size,$angle,$x,$y,$color,$fontfile,$text);
}
}
return false;
}
public function setCenterTXT($text,$size=10,$color='',$fontfile='')
{
if(!empty($text) && is_resource($this->dest) && get_resource_type($this->dest)=='gd')
{
if(empty($color))$color = $this->setColor(0,0,0);
if(empty($fontfile))
$fontfile = dirname(__FILE__).'/font/simsun.ttc';
else
$fontfile = dirname(__FILE__).'/font/'.$fontfile;
if(file_exists($fontfile))
{
$imgsize = imagettfbbox($size,0,$fontfile,$text);
$TextWidth = $imgsize[4];
$TextHeight = abs($imgsize[7]);
$pic_w = abs(($this->x_size-$TextWidth)/2);
$pic_h = abs(($this->y_size-$TextHeight)/2+$size-20);
imagealphablending($this->dest,true);
return imagettftext($this->dest,$size,0,$pic_w,$pic_h,$color,$fontfile,$text);
}
}
return false;
}
public function copyIMG($filename,$percent=1,$dst_x=0,$dst_y=0,$src_x=0,$src_y=0)
{
if(file_exists($filename) && is_resource($this->dest) && get_resource_type($this->dest)=='gd')
{
if(is_numeric($dst_x) && is_numeric($dst_y) && is_numeric($src_x) && is_numeric($src_y))
{
$info = getimagesize($filename);
$width = $info['0'];
$height = $info['1'];
$newwidth = $width * $percent;
$newheight = $height * $percent;
switch($info['mime'])
{
case "image/gif":
$source = imagecreatefromgif($filename);
imagecopyresized($this->dest,$source,$dst_x,$dst_y,$src_x,$src_y,$newwidth,$newheight,$width,$height);
break;
case "image/jpeg":
$source = imagecreatefromjpeg($filename);
imagecopyresized($this->dest,$source,$dst_x,$dst_y,$src_x,$src_y,$newwidth,$newheight,$width,$height);
break;
case "image/png":
$source = imagecreatefrompng($filename);
imagecopyresized($this->dest,$source,$dst_x,$dst_y,$src_x,$src_y,$newwidth,$newheight,$width,$height);
break;
}
}
}
return false;
}
public function fillBG($color,$x=0,$y=0)
{
if(is_numeric($this->color) && is_numeric($x) && is_numeric($y))
{
if(empty($color))$color = $this->setColor(0,0,0);
if(is_resource($this->dest) && get_resource_type($this->dest)=='gd')
return imagefill($this->dest,$x,$y,$color);
}
return false;
}
public function echoIMG($name='',$path='',$type='jpg',$quality=100)
{
if(is_resource($this->dest) && get_resource_type($this->dest)=='gd')
{
empty($path) ? $filepath = './' : $filepath = $path;
$filepath = rtrim($filepath,'/');
$type = strtolower($type);
if(file_exists($filepath) && !empty($name))
{
switch($type)
{
case 'jpg':
imagejpeg($this->dest,$filepath.'/'.$name.'.jpg',$quality);
break;
case 'png':
imagepng($this->dest,$filepath.'/'.$name.'.png');
break;
case 'gif':
imagejpeg($this->dest,$filepath.'/'.$name.'.gif');
break;
default:
imagejpeg($this->dest,$filepath.'/'.$name,$quality);
}
}
}
return false;
}
public function __destruct()
{
if(is_resource($this->dest) && get_resource_type($this->dest)=='gd')
imagedestroy($this->dest);
}
}
?>
createImages 用来创建一个真彩色底层
setColor 设置颜色(RGB)
setAlphaColor 设置透明度
setTXT 设置图片中文字
setCenterTXT 同上,只是会自动居中
copyIMG 获取某张图片用来做背景
fillBG 填充颜色
echoIMG 输出图片
具体参数看代码就知道;
$img = new WaterMark(225,170);
$img->copyIMG('./temp/bg.png');
$img->setCenterTXT($name,30,$img->setColor(0,0,0),'STXIHEI.TTF');
$img->echoIMG('./images/ImgfileName','','png');