日期:2014-05-17  浏览次数:20405 次

PHP给图片增加水印得类
<?php
/*
+--------------------------------------
| 生成加水印的图片类 (支持水印为图片或者文字)
| ============================
| by JackMing (感谢Dash和其他不知道姓名的朋友支持,本类在这些朋友作品的基础上创建)
+--------------------------------------
*/

Class Gimage{
var $src_image_name = ""; //输入图片的文件名(必须包含路径名)
var $jpeg_quality = 90; //jpeg图片质量
var $save_image_file = ''; //输出文件名
var $wm_image_name = ""; //水印图片的文件名(必须包含路径名)
var $wm_image_pos = 1; //水印图片放置的位置
// 0 = middle
// 1 = top left
// 2 = top right
// 3 = bottom right
// 4 = bottom left
// 5 = top middle
// 6 = middle right
// 7 = bottom middle
// 8 = middle left
//other = 3
var $wm_image_transition = 20; //水印图片与原图片的融合度 (1=100)

var $wm_text = ""; //水印文字(支持中英文以及带有\r\n的跨行文字)
var $wm_text_size = 20; //水印文字大小
var $wm_text_angle = 4; //水印文字角度,这个值尽量不要更改
var $wm_text_pos = 3; //水印文字放置位置
var $wm_text_font = ""; //水印文字的字体
var $wm_text_color = "#cccccc"; //水印字体的颜色值

function create($filename="")
{
if ($filename) $this->src_image_name = strtolower(trim($filename));

$src_image_type = $this->get_type($this->src_image_name);
$src_image = $this->createImage($src_image_type,$this->src_image_name);
if (!$src_image) return;
$src_image_w=ImageSX($src_image);
$src_image_h=ImageSY($src_image);


if ($this->wm_image_name){
$this->wm_image_name = strtolower(trim($this->wm_image_name));
$wm_image_type = $this->get_type($this->wm_image_name);
$wm_image = $this->createImage($wm_image_type,$this->wm_image_name);
$wm_image_w=ImageSX($wm_image);
$wm_image_h=ImageSY($wm_image);
$temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);
$wm_image_x = $temp_wm_image["dest_x"];
$wm_image_y = $temp_wm_image["dest_y"];
imageCopyMerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);
}

if ($this->wm_text){
$this->wm_text = $this->gb2utf8($this->wm_text);
$temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);
$wm_text_x = $temp_wm_text["dest_x"];
$wm_text_y = $temp_wm_text["dest_y"];
if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color))
{
$red = hexdec($color[1]);
$green = hexdec($color[2]);
$blue = hexdec($color[3]);
$wm_text_color = imagecolorallocate($src_image, $red,$green,$blue);
}else{
$wm_text_color = imagecolorallocate($src_image, 255,255,255);
}

imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color,$this->wm_text_font,$this->wm_text);
}

if ($this->save_file)
{
switch ($this->output_type){
case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;
case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;
default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
}
}
else
{
if ($src_image_type = "jpg") $src_image_type="jpeg";
header("Content-type: image/{$src_image_type}");
switch ($src_image_type){
case 'gif':$src_img=ImagePNG($src_image); break;
case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
case 'png':$src_img=ImagePNG($src_image);break;
default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
}
}
imagedestroy($src_image);
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
createImage 根据文件名和类型创建图片
内部函数

$type: 图片的类型,包括gif,jpg,png
$img_name:图片文件名,包括路径名,例如 " ./mouse.jpg"
*/
function createImage($type,$img_name){
if (!$type){
$type = $this->get_type($img_name);
}

switch ($type){
case 'gif':
if (function_exists('imagecreatefromgif'))
$tmp_img=@ImageCreateFromGIF($img_name);
break;
case 'jpg':
$tmp_img=ImageCreateFromJPEG($img_name);
break;
case 'png':
$tmp_img=ImageCreateFromPNG($img_name);
break;
default:
$tmp_img=ImageCreateFromString($img_name);
break;
}
return $tmp_img;
}

/*++++++++++++