阅读背景:

PHP 给上传到网页的图片添加水印(文字和图像)

来源:互联网 

首先构建一个添加文字水印的类:

<?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{



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: