使用过dedecms的朋友就会发现它可以实现图片上加图片水印也可以直接实现图片上加文本水印了,下面我来给大家介绍的是图片上加文字水印的方法,希望例子能帮助到各位.
php实现图片添加文字水印程序代码如下:
-
-
-
-
-
-
-
-
-
-
-
-
- <?php
- class image_fu{
- private $image;
- private $img_info;
- private $img_width;
- private $img_height;
- private $img_im;
- private $img_text;
- private $img_ttf='';
- private $img_new;
- private $img_text_size;
- private $img_jd;
- function img($img='',$txt='',$ttf='',$size=12,$jiaodu=0){
- if(isset($img)&&file_exists($img)){
- $this->image =$img;
- $this->img_text=$txt;
- $this->img_text_size=$size;
- $this->img_jd=$jiaodu;
- if(file_exists($ttf)){
- $this->img_ttf=$ttf;
- }else{
- exit('字体文件:'.$ttf.'不存在!');
- }
- $this->imgyesno();
- }else{
- exit('图片文件:'.$img.'不存在');
- }
- }
- private function imgyesno(){
- $this->img_info =getimagesize($this->image);
- $this->img_width =$this->img_info[0];
- $this->img_height=$this->img_info[1];
-
- switch($this->img_info[2]){
- case 1:$this->img_im = imagecreatefromgif($this->image);break;
- case 2:$this->img_im = imagecreatefromjpeg($this->image);break;
- case 3:$this->img_im = imagecreatefrompng($this->image);break;
- default:exit('图片格式不支持水印');
- }
- $this->img_text();
- }
- private function img_text(){
- imagealphablending($this->img_im,true);
-
- $color=imagecolorallocate($this->img_im,rand(0,255),rand(0,255),rand(0,255));
- $txt_height=$this->img_text_size;
- $txt_jiaodu=$this->img_jd;
- $ttf_im=imagettfbbox($txt_height,$txt_jiaodu,$this->img_ttf,$this->img_text);
- $w = $ttf_im[2] - $ttf_im[6];
- $h = $ttf_im[3] - $ttf_im[7];
-
-
- unset($ttf_im);
- $txt_y =$this->img_height-$h;
- $txt_x =$this->img_width-$w;
-
-
- $this->img_new=@imagettftext($this->img_im,$txt_height,$txt_jiaodu,$txt_x,$txt_y,$color,
- $this->img_ttf,$this->img_text);
- @unlink($this->image);
- switch($this->img_info[2]) {
- case 1:imagegif($this->img_im,$this->image);break;
- case 2:imagejpeg($this->img_im,$this->image);break;
- case 3:imagepng($this->img_im,$this->image);break;
- default: exit('水印图片失败');
- }
- }
-
- function img_show(){echo '<img src="'.$this->image.'" border="0" alt="'.$this->img_text.'" />';}
-
- private function img_nothing(){
- unset($this->img_info);
- imagedestroy($this->img_im);
- }
- }
- $addwk = new image_fu();
- $addwk->img('2.jpg','testwatermark','华康少女字体.ttf',33,0);
- $addwk->img_show();
|