php實用圖片浮水印效果代碼
<?php
define('Water',1);//浮水印方式(0,文字浮水印,1圖片浮水印)
define('WaterImg','./water.jpg');//浮水印圖片地址
define('WaterNum',0);//浮水印位置,0為隨機,1-9分別為頂左頂中頂右中左中中中右底左底中底右
define('WaterX',81);//浮水印長
define('WaterY',81);//浮水印高
define('WaterType','image/jpeg');//浮水印圖片類型
define('WaterText','愛搜羅');//浮水印文字(必須先轉換成UTF-8才能實現中文浮水印)
define('WaterTextColor','#000000');//浮水印文字顏色
define('WaterTextSize',50);//浮水印文字大小 px
define('WaterTextFont','C:WINDOWSFontsSIMFANG.TTF');//浮水印文字字型 (仿宋)
define('WaterPtc',50);//浮水印透明度
//////////////////////浮水印預設設定完畢
class water
{
private $img;//圖片地址
private $img_type;//圖片類型
//private $img_mime;//圖片實際類型
private $img_x;//圖片長
private $img_y;//圖片高
private $is_water;//是否可以添加浮水印,布爾
private $water_img;//浮水印圖片
private $water_text;//圖片文字(UTF-8)
private $water_num;//浮水印位置(可能用不著)
private $water_w = WaterX;//water的寬
private $water_h = WaterY ;//water的長
private $water_x;//浮水印的x位置
private $water_y;//浮水印的y位置
private $src_x;//畫布與water兩圖的長度差
private $src_y;//畫布與water兩圖的高度差
private $err_msg;//圖片地址
public function __construct($img,$img_type)
{
if (file_exists($img) == FALSE)
{
$this->Err_Img('No_Img');
}
$this->img = $img;
$this->img_type = $img_type;//寫入圖片類型
$this->Check_GD();//檢查GD庫
$this->Do_Water();//判斷浮水印方式
$this->Type_Img();//判斷圖片類型
}
/*
* 檢查GD庫
*/
public function Check_GD(){
if (function_exists("gd_info") == FALSE)
{
$this->Err_Img('No_Gd');
}
}
/*
* 處理浮水印方式及water檔案類型
*/
public function Do_Water()
{
if (Water == 1)//圖片浮水印
{
$this->Img_Water();//處理water圖片
}else{//文字浮水印
$this->Text_xy();//處理文字浮水印圖片
}
}
/*
* 處理water圖片
*/
public function Img_Water()
{
if (file_exists(WaterImg) == FALSE)
{
$this->Err_Img('No_WaterImg');
//使用文字浮水印
}
switch (WaterType)
{
case 'image/jpeg':
$this->water_img = imagecreatefromjpeg(WaterImg);
break;
}
}
/*
* 獲得文字的長寬 並 計算 兩圖的長寬差
*/
public function Text_xy()
{
$str = iconv('GB2312','UTF-8',WaterText);
$temp = imagettfbbox(50,0,WaterTextFont,$str);//取得使用 TrueType 字型的文本的範圍
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
$this->src_x = $this->img_x - $w;
$this->src_y = $this->img_y - $h;
}
/*
* 判斷圖片是否能夠添加浮水印,比較大小
*/
public function Is_Water()
{
if (Water == 1)//圖片浮水印
{
$this->src_x = $this->img_x - WaterX;
$this->src_y = $this->img_y - WaterY;
if ($this->src_x <= 0 && $this->src_y <= 0)
{
$this->Err_Img('Too_Small');
}
}else{//文字浮水印
if ($this->src_x <= 0 && $this->src_y <=0)
{
$this->Err_Img('Too_Small');
}
}
}
/*
* 處理浮水印位置
* 得到 浮水印位置的座標
*/
public function Water_Num()
{
switch(WaterNum)
{
case 0://隨機
$this->water_x = rand(0,($this->img_x - $this->water_w));
$this->water_y = rand(0,($this->img_y - $this->water_h));
break;
case 1://頂端居左
$this->water_x = 0;
$this->water_y = 0;
break;
case 2://頂端置中
$this->water_x = ($this->img_x - $this->water_w)/2;
$this->water_y = 0;
break;
case 3://頂端居右
$this->water_x = $this->img_x - $this->water_w;
$this->water_y = 0;
break;
case 4://中間居左
$this->water_x = 0;
$this->water_y = ($this->img_y - $this->water_h)/2;
break;
case 5://中間置中
$this->water_x = ($this->img_x - $this->water_w)/2;
$this->water_y = ($this->img_y - $this->water_h)/2;
break;
case 6://中間居右
$this->water_x = $this->img_x - $this->water_w;
$this->water_y = ($this->img_y - $this->water_h)/2;
break;
case 7://底部居左
$this->water_x = 0;
$this->water_y = $this->img_y - $this->water_h;
break;
case 8://底部置中
$this->water_x = ($this->img_x - $this->water_w)/2;
$this->water_y = $this->img_y - $this->water_h;
break;
case 9://底部居右
$this->water_x = $this->img_x - $this->water_w;
$this->water_y = $this->img_y - $this->water_h;
break;
default://隨機
$this->water_x = rand(0,($this->img_x - $this->water_w));
$this->water_y = rand(0,($this->img_y - $this->water_h));
}
}
/*
* 判斷圖片類型
*/
public function Type_Img()
{
switch ($this->img_type)
{
case 'image/jpeg':
header("Content-type: image/jpeg");
$this->img = imagecreatefromjpeg($this->img);
break;
}
$this->Img_Water();//處理water圖片
$this->img_x = imagesx($this->img);
$this->img_y = imagesy($this->img);
$this->Water_Num();//判斷浮水印位置
$this->Is_Water();
$this->Make_img();
}
/*
* 製作圖片浮水印
*/
public function Make_img(){
imagecopymerge($this->img,$this->water_img,$this->water_x,$this->water_y,0,0,WaterX,WaterY,WaterPtc);
imagejpeg($this->img);
}
/*
* 製作文字浮水印
*/
public function Make_Text()
{
echo '測試';
}
/*
* 返回錯誤資訊
*/
public function Err_Img($msg)
{
switch($msg)
{
case 'No_Gd':
$this->err_msg = '沒有安裝GD庫';
break;
case 'No_Img':
$this->err_msg = '圖片不存在';
break;
case 'No_WaterImg':
$this->err_msg = 'water圖片不存在';
break;
case 'Too_Small':
$this->err_msg = '圖片過小';
break;
}
die($this->err_msg);//輸出錯誤資訊
}
public function __destruct()
{
imagedestroy($this->img);
imagedestroy($this->img_water);
}
}
$img = new water('./100.jpg','image/jpeg');//執行個體化
?>