《php 文字水印_.docx》由会员分享,可在线阅读,更多相关《php 文字水印_.docx(6页珍藏版)》请在三一文库上搜索。
1、php 文字水印_ php生成文字水印,并将图片另存,下面是代码,修改了多次,可以防止中文乱码,但是要求字体,依据下面的提示进行配置,这样php文字水印很简单就输出了。 /参数分别是 原始图片 保存图片 位置 文字水印内容 水印数量 文字水印颜色 文字水印字体 /字体在当前文件夹下 function imageWaterMark($groundImage,$savename,$pos=0,$waterText=,$number=1,$textColor=#cccccc,$font = simhei.ttf) $formatMsg = 仅仅能为GIF、JPG、PNG添加水印图片; if($sav
2、ename=)$savename = $groundImage; $font = dirname(_FILE_).$font; $font = str_replace(,/,$font); if(!empty($font) !file_exists($font) die(font not exists); if(!empty($groundImage) file_exists($groundImage) $ground_info = getimagesize($groundImage); $ground_w = $ground_info0;/取得背景图片的宽 $ground_h = $grou
3、nd_info1;/取得背景图片的高 switch($ground_info2)/取得背景图片的格式 case 1:$ground_im = imagecreatefromgif($groundImage);break; case 2:$ground_im = imagecreatefromjpeg($groundImage);break; case 3:$ground_im = imagecreatefrompng($groundImage);break; default:die($formatMsg); else die(需要加文字水印的图片不存在!); $w = $h = 0 ;/这里是
4、文字水印的的宽度和高度,无法定义 for($number;$number1;$number-) switch($pos) case 0:/随机 $posX = rand(50,($ground_w - $w ); $posY = rand(120,($ground_h - $h ); break; case 1:/文字水印顶端居左 $posX = 0; $posY = 0; break; case 2:/文字水印顶端居中 $posX = ($ground_w - $w) / 2; $posY = 0; break; case 3:/文字水印顶端居右 $posX = $ground_w - $w
5、; $posY = 0; break; case 4:/文字水印中部居左 $posX = 0; $posY = ($ground_h - $h) / 2; break; case 5:/文字水印中部居中 $posX = ($ground_w - $w) / 2; $posY = ($ground_h - $h) / 2; break; case 6:/文字水印中部居右 $posX = $ground_w - $w; $posY = ($ground_h - $h) / 2; break; case 7:/文字水印底端居左 $posX = 0; $posY = $ground_h - $h; b
6、reak; case 8:/文字水印底端居中 $posX = ($ground_w - $w) / 2; $posY = $ground_h - $h; break; case 9:/文字水印为底端居右 $posX = $ground_w - $w; $posY = $ground_h - $h; break; case 11: $posX = $ground_w - $w; $posY = $ground_h - $h; break; default:/文字水印随机 $posX = rand(0,($ground_w - $w); $posY = rand(0,($ground_h - $h
7、); break; /设定文字水印图片颜色图像的混色模式 imagealphablending($ground_im, true); if( !empty($textColor) (strlen($textColor)=7) ) $R = hexdec(substr($textColor,1,2); $G = hexdec(substr($textColor,3,2); $B = hexdec(substr($textColor,5); else die(水印文字颜色格式不正确!); imagettftext($ground_im, 20, 0, $posX, $posY, imagecolo
8、rallocate($ground_im, $R, $G, $B), $font, $waterText);/将文字写到图片中 img size angle beginx beginy color fonttype content /unlink($groundImage); switch($ground_info2)/取得背景图片的格式 ,并保存文字水印图片 case 1:imagegif($ground_im,$savename);break; case 2:imagejpeg($ground_im,$savename);break; case 3:imagepng($ground_im,$savename);break; default:die($errorMsg); /释放内存 unset($ground_info); imagedestroy($ground_im); 上面就是php 生成文字水印图片函数,假如用法拿去吧。 更多信息请查看IT技术专栏 .