溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP如何實現文字水印,縮略圖,圖片水印功能

發布時間:2021-07-08 09:54:29 來源:億速云 閱讀:156 作者:小新 欄目:開發技術

這篇文章主要介紹PHP如何實現文字水印,縮略圖,圖片水印功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

具體如下:

1.實現類ImageToTest.class.php參考代碼

class ImageToTest {
  /**
   * 圖片的基本信息
   */
  private $info;
  private $image;
  public function __construct($src){
    $info = getimagesize($src);
    $this->info = array(
      'width'=> $info[0],
      'height'=> $info[1],
      'type'=> image_type_to_extension($info[2],false),
      'mime'=>$info['mime']
    );
    $fun = "imagecreatefrom{$this->info['type']}";
    $this->image = $fun($src);
  }
  /**
   * 操作圖片 (壓縮)
   */
  public function thumb($width,$height){
    $image_thumb = imagecreatetruecolor($width,$height);
    imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,
      $this->info['width'],$this->info['height']);
    imagedestroy($this->image);
    $this->image = $image_thumb;
  }
  /**
   * 操作圖片(添加文字水?。?
   */
  public function fontMark($content,$font_url,$size,$color,$local,$angle){
    $col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
    imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);
  }
  /**
   * 操作圖片(添加水印圖片)
   */
  public function imageMark($source,$local,$alpha){
    //1.獲取水印圖片的基本信息
    $info2 = getimagesize($source);
    //2.通過水印的圖片編號來獲取水印的圖片類型
    $type2 = image_type_to_extension($info2[2],false);
    //3.在內存中創建一個和我們的水印圖像一致的圖像類型
    $func2 = "imagecreatefrom{$type2}";
    //4.把水印圖片復制到內存中
    $water = $func2($source);
    //5.合并圖片
    imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);
    //6.銷毀水印圖片
    imagedestroy($water);
  }
  /**
   * 在瀏覽器中輸出圖片
   */
  public function show(){
    header("Content-type:".$this->info['mime']);
    $funs = "image{$this->info['type']}";
    $funs($this->image);
  }
  /**
   * 把圖片保存到硬盤里
   */
  public function save($newName){
    $funs = "image{$this->info['type']}";
    $funs($this->image,'./outPut/'.$newName.'.'.$this->info['type']);
  }
  /**
   * 銷毀圖片 使用析構函數
   */
  public function __destruct()
  {
    imagedestroy($this->image);
  }
}

2.測試參考代碼

require_once('ImageToTest.class.php');
/*$src = './image/wbg.jpg';
$image = new ImageToTest($src);
$image->thumb(700,550);
$image->show();*/
/*$src2 = './image/wbg.jpg';
$content = 'SGC';
$font_url = './image/YGYcuhei.ttf';
$size = 33;
$color = array(
  0=>2,
  1=>222,
  2=>222,
  3=>60
);
$local = array(
  'x'=>20,
  'y'=>100
);
$angle = 10;
$image2 = new ImageToTest($src2);
$image2->fontMark($content,$font_url,$size,$color,$local,$angle);
$image2->show();
$image2->save('hahahah');*/
$src3 = './image/wbg.jpg';
$source = './image/water.jpg';
$local = array(
  'x'=>20,
  'y'=>100
);
$font_url = './image/YGYcuhei.ttf';
$size = 38;
$color = array(
  0=>2,
  1=>222,
  2=>222,
  3=>60
);
$alpha = 60;
$angle = 50;
$image3 = new ImageToTest($src3);
$image3->imageMark($source,$local,$alpha);
$image3->thumb(700,550);
$image3->fontMark('Hello',$font_url,$size,$color,$local,$angle);
$image3->show();
$image3->save('WAWAWAWAWA');

以上是“PHP如何實現文字水印,縮略圖,圖片水印功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女