亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP添加PNG圖片背景透明水印操作類定義與用法示例

 更新時間:2019年03月12日 08:45:00   作者:tangming20160613  
這篇文章主要介紹了PHP添加PNG圖片背景透明水印操作類定義與用法,涉及php操作圖片的顯示、保存、壓縮、水印添加等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP添加PNG圖片背景透明水印操作類定義與用法。分享給大家供大家參考,具體如下:

圖片相關(guān)操作類

class ImageTool
{
  private $imagePath;//圖片路徑
  private $outputDir;//輸出文件夾
  public $memoryImg;//內(nèi)存圖像
  public $path;
  public function __construct($imagePath, $outputDir = null)
  {
    $this->imagePath = $imagePath;
    $this->outputDir = $outputDir;
    $this->memoryImg = null;
    $this->path = null;
  }
  /**
   * 顯示內(nèi)存中的圖片
   * @param $image
   */
  public function showImage()
  {
    if ($this->memoryImg != null) {
      $info = getimagesize($this->imagePath);
      $type = image_type_to_extension($info[2], false);
      header('Content-type:' . $info['mime']);
      $funs = "image{$type}";
      $funs($this->memoryImg);
      imagedestroy($this->memoryImg);
      $this->memoryImg = null;
    }
  }
  /**
   * 保存圖片
   * @param $image  圖片路徑
   * @return string
   */
  private function saveImage($image)
  {
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $funs = "image{$type}";
    if (empty($this->outputDir)) {
      $funs($image, md5($this->imagePath) . '.' . $type);
      return md5($this->imagePath) . '.' . $type;
    } else {
      $funs($image, $this->outputDir . md5($this->imagePath) . '.' . $type);
      return $this->outputDir . md5($this->imagePath) . '.' . $type;
    }
  }
  /**
   * 壓縮圖片
   * @param $width 壓縮后寬度
   * @param $height 壓縮后高度
   * @param bool $output 是否輸出文件
   * @return resource
   */
  public function compressImage($width, $height, $output = false)
  {
    $image = null;
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $fun = "imagecreatefrom{$type}";
    $image = $fun($this->imagePath);
    imagesavealpha($image,true);//
    $thumbnail = imagecreatetruecolor($width, $height);
    imagealphablending($thumbnail,false);//這里很重要,意思是不合并顏色,直接用$img圖像顏色替換,包括透明色;
    imagesavealpha($thumbnail,true);//
    imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
    imagedestroy($image);
    if ($output) {
      $path = $this->saveImage($thumbnail);
      $this->path = $path;
    }
    $this->memoryImg = $thumbnail;
    return $this;
  }
  /**
   * 為圖像添加文字標(biāo)記
   *
   * @param $content 文本內(nèi)容
   * @param $size 字體大小
   * @param $font 字體樣式
   * @param bool $output 是否輸出文件
   * @return $this
   */
  public function addTextmark($content, $size, $font, $output = false)
  {
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $fun = "imagecreatefrom{$type}";
    $image = $fun($this->imagePath);
    $color = imagecolorallocatealpha($image, 0, 0, 0, 80);
    $posX = imagesx($image) - strlen($content) * $size / 2;
    $posY = imagesy($image) - $size / 1.5;
    imagettftext($image, $size, 0, $posX, $posY, $color, $font, $content);
    if ($output) {
      $this->saveImage($image);
    }
    $this->memoryImg = $image;
    return $this;
  }
  /**
   * 為圖片添加水印
   *
   * @param $watermark 水印圖片路徑
   * @param $alpha 水印透明度(0-100)
   * @param bool $output 是否輸出文件
   * @return $this
   */
  public function addWatermark($watermark, $alpha, $output = false)
  {
    $image_info = getimagesize($this->imagePath);
    $image_type = image_type_to_extension($image_info[2], false);
    $image_fun = "imagecreatefrom{$image_type}";
    $image = $image_fun($this->imagePath);
    $mark_info = getimagesize($watermark);
    $mark_type = image_type_to_extension($mark_info[2], false);
    $mark_fun = "imagecreatefrom{$mark_type}";
    $mark = $mark_fun($watermark);
    $posX = imagesx($image) - imagesx($mark);
    $posY = imagesy($image) - imagesy($mark);
    imagesavealpha($mark, true);
    imagecopymerge($image, $mark, $posX, $posY, 0, 0, $mark_info[0], $mark_info[1], $alpha);
    imagesavealpha($mark, true);
    if ($output) {
      $path = $this->saveImage($image);
      $this->path = $path;
    }
    $this->memoryImg = $image;
    return $this;
  }
  //用給定角度旋轉(zhuǎn)圖像,以jpeg圖像格式為例
  /**
   * 水印圖片旋轉(zhuǎn)
   * @param $degrees     旋轉(zhuǎn)角度
   * @param bool $output   是否保存圖片
   * @return $this
   */
  function rotateImage($degrees, $output = false)
  {
    $info = getimagesize($this->imagePath);
    $type = image_type_to_extension($info[2], false);
    $fun = "imagecreatefrom{$type}";
    $image = $fun($this->imagePath);
    $block = imagecreatetruecolor(170,170);//建立一個畫板
    $bg = imagecolorallocatealpha($block , 0 , 0 , 0 , 127);//拾取一個完全透明的顏色
    $image = imagerotate($image, $degrees, $bg ,0);
    imagesavealpha($image, true);
    header("Content-type: image/{$type}");
    //旋轉(zhuǎn)后的圖片保存
    if ($output) {
      $path = $this->saveImage($image);
      $this->path = $path;
    }
    $this->memoryImg = $image;
    return $this;
  }
  /**
  * 添加PNG透明圖片
  * $bigImgPath 目標(biāo)圖片路徑
  * $smallImgPath 水印圖片路徑
  * $width 相對于目標(biāo)圖的x軸放置位置 左上角為 0
  * $height 相對于目標(biāo)圖的y軸放置位置 左上角為0
  * $bigImgPaths 合成后的圖片路徑 若路徑名與第一張或第二張路徑相同 直接覆蓋原圖
  */
  public function mergerImg($bigImgPath, $smallImgPath, $width, $height, $bigImgPaths)
  {
    $image_kuang = imagecreatefromstring(file_get_contents($smallImgPath));
    $image_photo = imagecreatefromstring(file_get_contents($bigImgPath));
    //創(chuàng)建一個新的,和大圖一樣大的畫布
    $image_3 = imageCreatetruecolor(imagesx($image_photo), imagesy($image_photo));
    //為真彩色畫布創(chuàng)建白色背景,再設(shè)置為透明
    $color = imagecolorallocate($image_3, 255, 255, 255);
    imagefill($image_3, 0, 0, $color);
    imageColorTransparent($image_3, $color);
    /**
     * 先copy圖片,再copy畫框,實(shí)現(xiàn)png的透明效果,將圖片嵌入到畫框里
     * imagecopymerge與imagecopy的不同:
     * imagecopymerge 函數(shù)可以支持兩個圖像疊加時,設(shè)置疊加層的透明度。imagecopymerge比imagecopy多一個參數(shù),來設(shè)置透明度
     * PHP內(nèi)部源碼里,imagecopymerge在透明度參數(shù)為100時,直接調(diào)用imagecopy函數(shù)。
     * imagecopy 函數(shù)則不支持疊加透明,但拷貝時可以保留png圖像的原透明信息,而imagecopymerge卻不支持圖片的本身的透明拷貝
     * 即:使用imagecopymerge函數(shù),可以實(shí)現(xiàn)打上透明度為30%的淡淡的水印圖標(biāo),但圖片本身的png就會變得像IE6不支持png透明那樣,背景不透明了。
     * 如果使用imagecopy函數(shù),可以保留圖片本身的透明信息,但無法實(shí)現(xiàn)30%的淡淡水印疊加,
     */
   imagecopyresampled($image_3,$image_photo,0,0,0,0,imagesx($image_photo),imagesy($image_photo),imagesx($image_photo),imagesy($image_photo));
    imagecopy($image_3,$image_kuang, $width,$height,0,0,imagesx($image_kuang),imagesy($image_kuang));
    //存儲圖片路徑
    imagejpeg($image_3, $bigImgPaths);
    return $bigImgPaths;
  }
}

控制器調(diào)用方法

public function test()
{
  $bigImgPath = 'ren.jpg';//原圖路徑
  $waterImgPath = 'tae.png';//水印圖路徑
  $imageTool = new ImageTool($waterImgPath, 'tmp/');//圖片路徑、輸出文件夾
  $smallImgPath = $imageTool->rotateImage(45, true)->path;//旋轉(zhuǎn)
  $width = 0;//水印所在X坐標(biāo)
  $height = 0;//水印所在Y坐標(biāo)
  $bigImgPaths = 'new.png';//生成原圖加水印新圖路徑
  $path = $this->mergerImg($bigImgPath, $smallImgPath, $width, $height, $bigImgPaths);
  return view('image', compact('path'));
}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論