-
-
/**
- * 加浮水印類,支援文字圖片浮水印的透明度設定、浮水印圖片背景透明。
- * 使用:(php文字浮水印效果)
- * $obj = new WaterMask($imgFileName); //實例化物件
- * $obj->$waterType = 1; //類型:0為文字浮水印、1為圖片浮水印
- * $obj->$transparent = 45; //浮水印透明度
- * $obj->$waterStr = 'bbs.it-home.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 = 'bbs. it-home.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 $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]);
- 休息1;
- 情況1: //上左
- $this->x = 0;
- $this->y = 0;
- 休息1;
- 情況2: //上中
- $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
- $ this->y = 0;
- 休息1;
- 情況3: //上右
- $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
- $this->y = 0;
- 休息1;
- 情況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;
- 休息1;
- 情況6: //中右
- $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
- $this->y = ( $this->srcImg_info[1]-$this->waterImg_info[1])/2;
- 休息1;
- 情況7: //下左
- $this->x = 0;
- $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
- 休息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;
- default: //下右
- $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
- $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
- 休息1;
- }
- }
- 私有函數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);
- }
- 私人函數waterstr() {
- $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
- $w = abs($ rect[2]-$rect[6]);
- $h = abs($rect[3]-$rect[7]);
- $fontHeight = $this->fontSize;
- $this ->water_im = imagecreatetruecolor($w, $h);
- imagealphablending($this->water_im,false);
- imagesavealpha($this->water_im,true);
- $white_alpha = imagecolor this->water_im,255,255,255,127);
- imagefill($this->water_im,0,0,$white_alpha);
- $color = imageColallocate($this->water_im,$this->fontor[0],$this] $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);
- 休息1;
- default:
- die('添加浮水印失敗! ');
- 休息;
- }
- imagedestroy($this->im);
- imagedestroy($this->water_im);
- }
- }
- ?>
複製程式碼
|