国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

PHP水印類,支持添加圖片、文字、填充顏色區(qū)域的實(shí)現(xiàn)

2024-05-04 23:55:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

*自己整理的一個(gè)水印類*

支持添加圖片、文字、填充顏色區(qū)域

<?php/** * 圖片加水印類,支持文字水印、透明度設(shè)置、自定義水印位置等。 * 使用示例: *   $obj = new WaterMask($imgFileName);       //實(shí)例化對(duì)象 *   $obj->$waterType = 1;           //類型:0為文字水印、1為圖片水印 *   $obj->$transparent = 45;         //水印透明度 *   $obj->$waterStr = 'icp.niufee.com';        //水印文字 *   $obj->$fontSize = 18;           //文字字體大小 *   $obj->$fontColor = array(255,255,255);        //水印文字顏色(RGB) *   $obj->$fontFile = 'AHGBold.ttf';       //字體文件 *   …… *   $obj->output();              //輸出水印圖片文件覆蓋到輸入的圖片文件 * @modify liuzp111 */class WaterMask{  public $waterTypeImage   = false;                //水印類型:?jiǎn)⒂脠D片水印  public $waterTypeStr    = false;          //水印類型:?jiǎn)⒂梦淖炙? public $pos        = 0;          //水印位置  public $transparent    = 45;         //水印透明度(0---100)數(shù)值越大越不透明  public $waterStr      = 'icp.niufee.com';      //水印文字  public $fontSize      = 14;         //文字字體大小  public $fontColor     = array(0,0,0);          //水印文字顏色(RGB) 默認(rèn)黑色  public $fontFile      = './font/simfang.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坐標(biāo)  private $y         = '';         //水印y坐標(biāo)  public $output_img     = '';                  //存儲(chǔ)輸出圖片到哪里  public $is_draw_rectangle = false;                  //是否繪制矩形區(qū)域 (暫不支持自定義位置)  //public $rectange_color   = '';                  //繪制矩形區(qū)域的顏色    private $result_array    = array();               //結(jié)果數(shù)組   public function __construct($img) {    //析構(gòu)函數(shù)    //$this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');    if(file_exists($img)){      $this->srcImg = $img;    }else{      return array('data'=>'','info'=>'源文件不存在!','status'=>0);    }  }  private function imginfo() {  //獲取需要添加水印的圖片的信息,并載入圖片。    $this->srcImg_info = getimagesize($this->srcImg);    switch ($this->srcImg_info[2]) {      case 3:        $this->im = imagecreatefrompng($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.')格式不對(duì),只支持PNG、JPEG、GIF。');        return array('data'=>'','info'=>'原圖片('.$this->srcImg.')格式不對(duì),只支持PNG、JPEG、GIF。','status'=>0);    }  }  private function waterimginfo() {  //獲取水印圖片的信息,并載入圖片。    $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.')格式不對(duì),只支持PNG、JPEG、GIF。');         return array('data'=>'','info'=>'水印圖片('.$this->srcImg.')格式不對(duì),只支持PNG、JPEG、GIF。','status'=>0);    }  }  private function waterpos() {  //水印位置算法    switch ($this->pos) {      case 0:   //隨機(jī)位置        $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;      case 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;        break 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;      case 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];        break 1;      case 9:   //下中偏上100px        $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;        $this->y = $this->srcImg_info[1]-$this->waterImg_info[1] - 100;        break 1;      default:  //下右        $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];        $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];        break 1;    }  }  /**   * 水印文字圖片位置,根據(jù)需求調(diào)整   */  private function waterposStr() {    $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;    $this->y = $this->srcImg_info[1]-$this->waterImg_info[1] - 3;      }  private function waterimg($type='') {    if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]){      //die('水印比原圖大!');      return array('data'=>'','info'=>'水印比原圖大!','status'=>0);    }    if($type == 'waterstr'){      $this->waterposStr();    }else{      $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->transparent;    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($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 = imagecolorallocatealpha($this->water_im,255,255,255,127);    imagefill($this->water_im,0,0,$white_alpha);    $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($type='waterstr');  }  /**   * 繪制矩形區(qū)   * bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )   * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )   * @author liuzp111   */  public function drawRectangle()  {    //imagefill($im,0,0,$gray);//填充資源,填充的坐標(biāo)(類似PS魔棒),顏色    /*     *  1--------------畫(huà)長(zhǎng)方形--------------     *  bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )     *  參數(shù): 畫(huà)布資源, 左上角x坐標(biāo),左上y坐標(biāo),右下x坐標(biāo),右下y坐標(biāo),顏色     */    $color = imagecolorallocate($this->im,255,255,255);//創(chuàng)建矩形邊框顏色和填充顏色    //=========================================================================    //繪制矩形區(qū)域并填充    // 參數(shù)說(shuō)明:    //bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )    // im:為將圖像載入為圖像資源    // $x1:表示矩形左上角的X坐標(biāo)    // $y1:表示矩形左上角的Y坐標(biāo)    // $x2:表示矩形右下角的X坐標(biāo)    // $y2:表示矩形右下角的Y坐標(biāo)    // $color:為填充的RGB顏色    //    imagefilledrectangle($this->im,3,$this->srcImg_info[1] - 20,$this->srcImg_info[0]-3,$this->srcImg_info[1]-3,$color);    //不要使用下方的函數(shù)填充,下方填充函數(shù)為魔棒填充,容易導(dǎo)致填充不完整    //imagefill($this->im,$this->srcImg_info[0]/2,$this->srcImg_info[1]-8,$color);//填充資源,填充的坐標(biāo)(魔棒),顏色  }  function output() {    $this->imginfo();    //是否創(chuàng)建矩形區(qū)域    if($this->is_draw_rectangle){      $this->drawRectangle();    }    if ($this->waterTypeStr ) {      $this->waterstr();    }     if($this->waterTypeImage )    {      $this->waterimginfo();      $this->waterimg();    }    switch ($this->srcImg_info[2]) {      case 3:        $res_output = imagepng($this->im,$this->output_img);        break 1;      case 2:        $res_output = imagejpeg($this->im,$this->output_img);        break 1;      case 1:        $res_output = imagegif($this->im,$this->output_img);        break 1;      default:        // die('添加水印失敗!');        return array('data'=>'','info'=>'添加水印失敗!','status'=>0);        break;    }    imagedestroy($this->im);    imagedestroy($this->water_im);    return array('data'=>$res_output,'info'=>'添加水印成功!','status'=>1);  }}

使用方式:

$file = '58368dddc8c51_22';//需要加水印的圖片$file_ext = '.jpeg';//擴(kuò)展名$imgFileName = './'.$file.$file_ext;//需要加水印圖片路徑$obj = new WaterMask($imgFileName); //實(shí)例化對(duì)象$obj->waterTypeStr = true;     //開(kāi)啟文字水印      $obj->waterTypeImage = true;    //開(kāi)啟圖片水印  $obj->pos = 9;         //定義水印圖片位置 $obj->waterImg = './water.png';      //水印圖片     $obj->transparent = 100;          //水印透明度$obj->waterStr = '保險(xiǎn)經(jīng)紀(jì)人:劉測(cè)試 電話:02052552';       //水印文字$obj->fontSize = 9;            //文字字體大小$obj->fontColor = array(0,0,0);        //水印文字顏色(RGB)$obj->fontFile = './font/msyh.ttc';    //字體文件,這里是微軟雅黑$obj->is_draw_rectangle = TRUE;      //開(kāi)啟繪制矩形區(qū)域$obj ->output_img = './'.$file.'_n'.$file_ext;//輸出的圖片路徑$obj->output();  

以上這篇PHP水印類,支持添加圖片、文字、填充顏色區(qū)域的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 邛崃市| 铁岭市| 汉沽区| 满洲里市| 美姑县| 友谊县| 项城市| 华亭县| 余庆县| 集安市| 无为县| 邳州市| 聂拉木县| 连平县| 新疆| 芮城县| 焉耆| 海门市| 铁岭县| 甘洛县| 南江县| 阳新县| 江川县| 洪雅县| 丹阳市| 安远县| 新乐市| 河间市| 阿拉尔市| 桂林市| 河池市| 凉山| 衡水市| 通州区| 瑞丽市| 合水县| 沅陵县| 霍林郭勒市| 昭平县| 抚顺市| 嵊泗县|