php給圖片增加中文浮水印

來源:互聯網
上載者:User

增加中文浮水印
<?php
/*-------------------------------------------------------------
**描述:這是用於給指定圖片加底部浮水印(不佔用圖片顯示地區)的自訂類,需建立對象調用
**版本:v1.0
**建立:2007-10-09
**更新:2007-10-09
**人員:老肥牛([email]fatkenme@163.com[/email]  QQ:70177108)
**說明:1、需要gd庫支援,需要iconv支援(php5已經包含不用載入)
        2、只適合三種類型的圖片,jpg/jpeg/gif/png,其它類型不處理
        3、注意圖片所在目錄的屬性必須可寫
        4、調用範例:
            $objImg = new MyWaterDownChinese();
            $objImg->Path = "images/";
            $objImg->FileName = "1.jpg";
            $objImg->Text = "胖胖交友網 [url]www.ppfriend.com[/url]";
            $objImg->Font = "./font/simhei.ttf";
            $objImg->Run();
**成員函數:
----------------------------------------------------------------*/
class MyWaterDownChinese{
          var $Path = "./";  //圖片所在目錄相對於調用此類的頁面的相對路徑
          var $FileName = ""; //圖片的名字,如“1.jpg”
          var $Text = "";   //圖片要加上的浮水印文字,支援中文
          var $TextColor = "#ffffff"; //文字的顏色,gif圖片時,字型顏色只能為黑色
          var $TextBgColor = "#000000"; //文字的背景條的顏色
          var $Font = "c://windows//fonts//simhei.ttf"; //字型的存放目錄,相對路徑
          var $OverFlag = true; //是否要覆蓋原圖,預設為覆蓋,不覆蓋時,自動在原圖檔案名稱後+"_water_down",如“1.jpg”=> "1_water_down.jpg"
          var $BaseWidth = 200; //圖片的寬度至少要>=200,才會加上浮水印文字。
        
//------------------------------------------------------------------
//功能:類的建構函式(php5.0以上的形式)
//參數:無
//返回:無
function __construct(){;}

//------------------------------------------------------------------
//功能:類的解構函式(php5.0以上的形式)
//參數:無
//返回:無
function __destruct(){;}
//------------------------------------------------------------------

//------------------------------------
//功能:對象運行函數,給圖片加上浮水印
//參數:無
//返回:無
function Run()
{
    if($this->FileName == "" || $this->Text == "")
        return;
    //檢測是否安裝GD庫
    if(false == function_exists("gd_info"))
    {
        echo "系統沒有安裝GD庫,不能給圖片加浮水印.";
        return;
    }
    //設定輸入、輸出圖片路徑名
    $arr_in_name = explode(".",$this->FileName);
    //
    $inImg = $this->Path.$this->FileName;
    $outImg = $inImg;
    $tmpImg = $this->Path.$arr_in_name[0]."_tmp.".$arr_in_name[1]; //臨時處理的圖片,很重要
    if(!$this->OverFlag)
        $outImg = $this->Path.$arr_in_name[0]."_water_down.".$arr_in_name[1];
    //檢測圖片是否存在
    if(!file_exists($inImg))
        return ;
    //獲得圖片的屬性
    $groundImageType = @getimagesize($inImg);
    $imgWidth = $groundImageType[0];
    $imgHeight = $groundImageType[1];
    $imgType = $groundImageType[2];
    if($imgWidth < $this->BaseWidth)  //小於基本寬度,不處理
        return;
   
    //圖片不是jpg/jpeg/gif/png時,不處理
    switch($imgType)
    {
         case 1:
              $image = imagecreatefromgif($inImg);
              $this->TextBgColor = "#ffffff"; //gif圖片字型只能為黑,所以背景顏色就設定為白色
              break; 
         case 2:
              $image = imagecreatefromjpeg($inImg);
              break; 
         case 3:
              $image = imagecreatefrompng($inImg);
              break; 
         default:
              return;
              break;
    }
    //建立顏色
    $color = @imagecolorallocate($image,hexdec(substr($this->TextColor,1,2)),hexdec(substr($this->TextColor,3,2)),hexdec(substr($this->TextColor,5,2))); //文字顏色
    //產生一個空的圖片,它的高度在底部增加浮水印高度
    $newHeight = $imgHeight+20;
    $objTmpImg = @imagecreatetruecolor($imgWidth,$newHeight);
    $colorBg = @imagecolorallocate($objTmpImg,hexdec(substr($this->TextBgColor,1,2)),hexdec(substr($this->TextBgColor,3,2)),hexdec(substr($this->TextBgColor,5,2))); //背景顏色   
    //填充圖片的背景顏色
    @imagefill ($objTmpImg,0,0,$colorBg);
    //把原圖copy到臨時圖片中
    @imagecopy($objTmpImg,$image,0,0,0,0,$imgWidth,$imgHeight);
    //建立要寫入的浮水印文字對象
    $objText = $this->createText($this->Text);
    //計算要寫入的浮水印文字的位置
    $x = 5;
    $y = $newHeight-5;
    //寫入文字浮水印
    @imagettftext($objTmpImg,10,0,$x,$y,$color,$this->Font,$objText);   
    //產生新的圖片,臨時圖片
    switch($imgType)
    {
         case 1:
              imagegif($objTmpImg,$tmpImg);
              break; 
         case 2:
              imagejpeg($objTmpImg,$tmpImg);
              break; 
         case 3:
              imagepng($objTmpImg,$tmpImg);
              break; 
         default:
              return;
              break;
    }   
    //釋放資源
    @imagedestroy($objTmpImg);
    @imagedestroy($image);
    //重新命名檔案
    if($this->OverFlag)
    {
        //覆蓋原圖
        @unlink($inImg);
        @rename($tmpImg,$outImg);
    }
    else
    {
        //不覆蓋原圖
        @rename($tmpImg,$outImg);
    }
}

//--------------------------------------
//功能:建立浮水印文字對象
//參數:無
//返回:建立的浮水印文字對象
function createText($instring)
{
   $outstring="";
   $max=strlen($instring);
   for($i=0;$i<$max;$i++)
   {
       $h=ord($instring[$i]);
       if($h>=160 && $i<$max-1)
       {
           $outstring .= "&#".base_convert(bin2hex(iconv("gb2312","ucs-2",substr($instring,$i,2))),16,10).";";
           $i++;
       }
       else
       {
           $outstring .= $instring[$i];
       }
   }
   return $outstring;
}

}//class
?>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.