支援文字浮水印、圖片浮水印支援浮水印的位置隨機或固定(九宮格)浮水印透明度設定(圖片浮水印和文字浮水印都支援)文字浮水印的字體、顏色、大小設定圖片浮水印的背景透明
-
/**
- * 加浮水印類,支援文字圖片浮水印的透明度設定、浮水印圖片背景透明。
- * 日期:2011-09-27
- * 作者:www.itwhy.org
- * 使用:
- * $obj = new WaterMask($imgFileName); //實例化物件
- * $obj->$waterType = 1; //類型:0為文字浮水印、1為圖片浮水印
- * $obj->$transparent = 45; //水印透明度
- * $obj->$waterStr = 'www.itwhy.org'; //水印文字
- * $obj->$fontSize = 16; //文字字體大小
- * $obj->$fontColor = array(255,0255); / /水印文字顏色(RGB)
- * $obj->$fontFile = = 'AHGBold.ttf'; //字體檔案
- * $obj->output(); //輸出水印圖片檔案覆蓋輸入的圖片檔
- */
- class WaterMask{
- public $waterType = 1; //水印類型:0為文字浮水印、1為圖片浮水印
- public $pos = 0; //浮水印位置
- public $transparent = 45; //水印透明度
-
- public $waterStr = 'www.itwhy.org'; //水印文字
- public $ fontSize = 16; //文字字體大小
- public $fontColor = array(255,0,255); //水印文字顏色(RGB)
- public $fontFile = 'AHGBold.ttf'; //字體檔案
-
- public $waterImg = 'logo.png'; //浮水印圖片
-
- private $srcImg = ''; //需要加浮水印的圖片
- private $im = ''; //圖片句柄
- private $water_im = ''; //水印圖片句柄
- private $srcImg_info = ''; //圖片資訊
- private $waterImg_info = ''; //水印圖片資訊
- private $waterImg_info = ''; //水印圖片資訊
- private $str_w = ''; //浮水印文字寬度
- private $str_h = ''; //浮水印文字高度
- private $x = ''; //水印X座標
- private $y = '' ; //浮水印y座標
-
- function __construct($img) { //析構函數
- $this->srcImg = file_exists($img) ? $img : die('"'.$img .'" 原始檔不存在! ');
- }
- private function imginfo() { //取得需要加入浮水印的圖片的信息,並載入圖片。
- $this->srcImg_info = getimagesize($this->srcImg);
- switch ($this->srcImg_info[2]) {
- case 3:
- $this->im = imagecreate $this->srcImg);
- break 1;
- case 2:
- $this->im = imagecreatefromjpeg($this->srcImg);
- break 1;
- case 1:
- $this->im = imagecreatefromgif($this->srcImg);
- break 1;
- default:
- die('原文圖片('.$this->srcImg.')格式不對,只支援PNG、JPEG、GIF。
- $this->waterImg_info = getimagesize($this->waterImg);
- switch ($this->waterImg_info[2]) {
- case 3:
- $this->water_im = imagecreatefrompng( $this->waterImg);
- break 1;
- case 2:
- $this->water_im = imagecreatefromjpeg($this->waterImg);
- break 1;
- case 1:
- $this->water_im = imagecreatefromgif($this->waterImg);
- break 1;
- default:
- die('水印圖片('.$this->srcImg.')格式不對,只支援PNG、JPEG、GIF。');
- }
- }
- private function waterpos() { //水印位置演算法
- switch ($this->pos) {
- case 0: // 隨機位置
- $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
- $this->y = rand(0,$this->srcImg_info[1] -$this->waterImg_info[1]);
- break 1;
- case 1: //上左
- $this->x = 0;
- $this->y = 0;
- break 1;
- 情況2: //上中
- $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
- $ this->y = 0;
- break 1;
- case 3: //上右
- $this->x = $this-> ;srcImg_info[0]-$this->waterImg_info[0] ;
- $this->y = 0;
- break 1;
- case 4: //中左
- $this->x = 0;
- $this->y = ($ this->srcImg_info[1]-$this->waterImg_info[1])/2;
- 規則1;
- case 5: //中中
- $this->x = ($this-> srcImg_info[0]-$this->waterImg_info[0])/2;
- $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
- Break 1;
- 案例6: //中右
- $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
- $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1] )/2;
- break 1;
- case 7: //下左
- $this->x = 0;
- $this->y = $this->srcImg_info[1 ]-$this->waterImg_info[1];
- break 1;
- case 8: //下方
- $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
- $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
- 中斷1;
- 預設值: //下右
- $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
- $this-> y = $this->srcImg_info[1]-$this->waterImg_info[1];
- break 1;
- }
- }
- private function waterimg() {
- if ($this ->; srcImg_info[0] waterImg_info[0] || $this->srcImg_info[1] waterImg_info[1]){
- die('水印比原圖大!');
- }
- $this->waterpos();
- $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
- imagecopy( $cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1] );
- $pct = $this->透明;
- imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this ->waterImg_info[1]);
- imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0 ],$this->waterImg_info[1],$pct);
- }
- private function waterstr() {
- $rect = imagettfbbox($this->fontSize,0,$this-> ;fontFile,$this->waterStr);
- $w = abs( $矩形[2]-$矩形[6]);
- $h = abs($矩形[3]-$矩形[7]);
- $fontHeight = $this->fontSize;
- $ this->water_im = imagecreatetruecolor($w, $h);
- imagealphablending($this->water_im,false);
- imagesavealpha($this->water_im,true);
- $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
- imagefill($this-00,00,000,000
- $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
- imagettftext($this- >water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
- $this->waterImg_info = array(0=>$ w,1=>$h);
- $this->waterimg();
- }
- 函數輸出() {
- $this->imginfo();
- if ($this ->waterType == 0) {
- $this->waterstr();
- }else {
- $this->waterimginfo();
- $this->waterimg();
- }
- switch ($this->srcImg_info[2]) {
- 情況3:
- imagepng($ this->im,$this->srcImg);
- 中斷1;
- 情況>情況2:
- imagejpeg($this->im,$this->srcImg);
- 規則1;
- 情況1:
- imagegif($this->im,$this->srcImg);
- break 1;
- 預設:
- die('新增浮水印失敗!' );
- 規則;
- }
- imagedestroy($this->im);
- imagedestroy(imagedestroy($this->im);
- imagedestroy(imagedestroy( $this->water_im);
- }
- }
?> ;
複製程式碼
|