首先构建一个添加文字水印的类:
<?php
class AddWaterPress{ //定义类文件
function getExtendsName($fileName){ //获取上传图片后缀
return strtolower(strstr($fileName, ".")); //返回图片后缀
}
function getImageRes($extendsName, $imageUrl){ //根据上传图片的后缀,和上传文件的路径新建图像
switch($extendsName){ //根据上传图片的后缀进行判断
case '.gif': //如果后缀为gif
$img =imagecreatefromgif($imageUrl); //则根据路径创建一个GIF图像
break;
case '.jpg': //如果后缀为jpg
$img =imagecreatefromjpeg($imageUrl); //则根据路径创建一个JPG图像
break;
case '.png':
$img =imagecreatefrompng($imageUrl);
break;
case '.bmp':
$img =imagecreatefromwbmp($imageUrl);
break;
}
return $img; //返回创建图像的标识
}
function add($imageUrl, $watherImageUrl){ //定义添加方法
$img = @$this->getImageRes($this->getExtendsName($imageUrl), $imageUrl); //获取被操作的图像标识
$textcolor=imagecolorallocate($img,0,0,0); //设置字体颜色为蓝色,值为RGB颜色值
//imagestring($img, 20, 30, 100, "$watherImageUrl", $textcolor);
$fnt="c:/windows/fonts/simhei.ttf"; //定义字体
$text =iconv("gb2312", "utf-8", $watherImageUrl); //将中文转换为UTF-8格式
imagettftext($img,20,0,30,100,$textcolor,$fnt,$text); //写TTF文字到图中
//根据图像标识符、后缀和路径,执行outputImage方法,输出图像
$this->outputImage($img, $this->getExtendsName($imageUrl), $imageUrl);
imagedestroy($img); //销毁图像
}
function outputImage($img, $extendsName, $imageUrl){ //根据图像标识、图片后缀和路径输出图像
switch($extendsName){ //判断图像后缀
case '.gif': //如果后缀为gif
imagegif($img, $imageUrl); //则输出img图像
break;
case '.jpg':
imagejpeg($img, $imageUrl);
break;
case '.png':
imagepng($img, $imageUrl);
break;
case '.bmp':
imagewbmp($img, $imageUrl);
break;
}
}
}
也可以构建一个添加指定图像到上传图像上,类为:
<?php
class AddWaterPress{ //定义类文件
function getExtendsName($fileName){ //获取上传图片后缀
return strtolower(strstr($fileName, ".")); //返回图片后缀
}
function getImageRes($extendsName, $imageUrl){ //根据上传图片的后缀,和上传文件的路径新建图像
switch($extendsName){ //根据上传图片的后缀进行判断
case '.gif': //如果后缀为gif
$img =imagecreatefromgif($imageUrl); //则根据路径创建一个GIF图像
break;
case '.jpg': //如果后缀为jpg
$img =imagecreatefromjpeg($imageUrl); //则根据路径创建一个JPG图像
break;
case '.png':
$img =imagecreatefrompng($imageUrl);
break;
case '.bmp':
$img =imagecreatefromwbmp($imageUrl);
break;
}
return $img; //返回创建图像的标识
}
function add($imageUrl, $watherImageUrl, $x, $y){ //定义添加方法
$img = @$this->getImageRes($this->getExtendsName($imageUrl), $imageUrl); //获取被添加的图像标识
$img1 = @$this->getImageRes($this->getExtendsName($watherImageUrl), $watherImageUrl); //获取指定的水印图片的图像标识
$size = getimagesize($imageUrl); //获取图像大小
$size1 = getimagesize($watherImageUrl); //获取水印图片的大小
if($x==null && $y==null){ //判断参数是否为空
$x1 = ($size[0]-$size1[0])/2; //根据图像大小数组中返回的值,计算图像的横坐标
$y1 = ($size[1]-$size1[1])/2; //根据图像大小数组中返回的值,计算图像的纵坐标
}else{
$x1 = $x; //如果不为空,则直接使用坐标数据
$y1 = $y; //如果不为空,则直接使用坐标数据
}
imagecopy($img, $img1, $x1, $y1, 0, 0, $size1[0], $size1[1]); //将img1的一部分拷贝到img的指定位置
//根据图像标识符、后缀和路径,执行outputImage方法,输出图像
$this->outputImage($img, $this->getExtendsName($imageUrl), $imageUrl);
imagedestroy($img1); //销毁图像
imagedestroy($img); //销毁图像
}
function outputImage($img, $extendsName, $imageUrl){ //根据图像标识、图片后缀和路径输出图像
switch($extendsName){ //判断图像后缀
case '.gif': //如果后缀为gif
imagegif($img, $imageUrl); //则输出img图像
break;
case '.jpg':
imagejpeg($img, $imageUrl);
break;
case '.png':
imagepng($img, $imageUrl);
break;
case '.bmp':
imagewbmp($img, $imageUrl);
break;
}
}
}
下面是主文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>上传图片添加水印</title>
<style type="text/css">
<!--
.STYLE1 {
color: #990100;
font-size: 18px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<table width="900" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="149" colspan="2" align="center" background="images/bg(1).jpg"><table width="890" height="145" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="296"> </td>
<td width="468"> </td>
<td width="126"> </td>
</tr>
<tr>
<td> </td>
<td>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
<span class="STYLE1">上传图片:</span>
<input type="file" name="file" class="input">
<input type="hidden" name="flag" value="1">
<input type="image" name="imageField" src="images/sc.jpg" />
</form></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="213" valign="top"><img src="images/bg(2).jpg" width="213" height="349" /></td>
<td width="685" height="431" background="images/bg_05.jpg"><table width="680" height="430" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="286" height="305" align="center" valign="bottom"><img src="upfiles/waterpress.jpg" width="259" height="248" /></td>
<td width="394"> </td>
</tr>
<tr>
<td height="123"> </td>
<td> </td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2"><img src="images/bg.jpg" width="895" height="202" /></td>
</tr>
</table>
</body>
</html>
<?php
if ($_FILES["file"]["name"]!="" && $_POST['flag']=='1'){ //判断提交内容是否为空
$type = strstr($_FILES["file"]["name"], '.'); //获取上传图片后缀
if($_FILES["file"]["name"]==''){ //判断上传图片名称是否为空
echo "<script>alert('图片不能为空!');</script>";
exit();
}else if(!($type=='.gif' || $type == '.png' || $type == '.bmp' || $type == '.jpg')){ //判断上传图片格式是否正确
echo "<script>alert('图片格式不正确!');</script>";
exit();
}
function getUpfileName($fileName){ //定义上传文件在服务器中存储的名称
return 'waterpress'.strstr($fileName, "."); //使用固定名称(也可以通过时间戳、随机数定义)
}
if(!is_dir('upfiles')){ //判断上传文件存储文件夹是否存在
mkdir('upfiles'); //如果不存在,则创建文件夹
}
$saveDir = "upfiles/" . getUpfileName($_FILES["file"]["name"]); //定义上传文件存储路径
if(move_uploaded_file($_FILES["file"]["tmp_name"], $saveDir)){ //执行文件上传操作
require_once 'AddWaterPress.php'; //包含添加水印操作的文件
$addWaterPress = new AddWaterPress(); //类的实例化
$addWaterPress->add($saveDir, "WWW.MRBCCD.CN"); //执行添加方法,传递参数,指定水印文字
echo "<script>alert('图片添加成功');</script>";
}
}
?>