标题 | php 文字水印 |
内容 | php生成文字水印,并将图片另存,下面是代码,修改了多次,可以防止中文乱码,但是要求字体,根据下面的提示进行配置,这样php文字水印很容易就输出了。 //参数分别是 原始图片 保存图片 位置 文字水印内容 水印数量 文字水印颜色 文字水印字体 //字体在当前文件夹下 function imageWaterMark($groundImage,$savename,$pos=0,$waterText="",$number=1,$textColor="#cccccc",$font = "simhei.ttf") { $formatMsg = "仅仅能为GIF、JPG、PNG添加水印图片"; if($savename=="")$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_info[0];//取得背景图片的宽 $ground_h = $ground_info[1];//取得背景图片的高 switch($ground_info[2])//取得背景图片的格式 { 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 ;//这里是文字水印的的宽度和高度,无法定义 for($number;$number>1;$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; $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; break; 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)); 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, imagecolorallocate($ground_im, $R, $G, $B), $font, $waterText);//将文字写到图片中 img size angle beginx beginy color fonttype content } //@unlink($groundImage); switch($ground_info[2])//取得背景图片的格式 ,并保存文字水印图片 { 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 生成文字水印图片函数,如果使用拿去吧。 |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。