在图片上添加文字水印

 1 <?php
 2     /**
 3         打开任何一种格式的图片 在图片的中间加上一个文字水印 保存
 4         只是保存下来 并不会输出到浏览器
 5     */
 6     function imagewater($filename,$string){
 7         //获得图片的属性
 8         list($width,$height,$type) = getimagesize($filename);
 9         //可以处理的照片的类型
10         $types = array(1=>"gif",2=>"jpeg",3=>"png");
11         //通过图片类型去组合变量函数
12         $createfrom = "imagecreatefrom".$types[$type];
13         //用变量函数去打开图片
14         $image = $createfrom($filename);
15         //设定加入文字的位置是在中间
16         $x = ($width - imagefontwidth(5) * strlen($string)) / 2;
17         $y = ($height - imagefontwidth(5)) / 2;
18         //设置文字颜色
19         $textcolor = imagecolorallocate($image,255,0,0);
20         //将字符串画在图片上
21         imagestring($image,5,$x,$y,$string,$textcolor);
22         //根据图片类型组合变量函数
23         $output = "image".$types[$type];
24         //通过变量函数保存到原类型的图片
25         $output($image,$filename);
26         //$output($image);
27         imagedestroy($image);
28     }
29     
30     image("mm2.jpg","banbanban");
31 ?>
imagewater
原文地址:https://www.cnblogs.com/banshaohuan/p/4991249.html