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

首頁 > 語言 > PHP > 正文

PHP圖片水印類的封裝

2024-05-04 23:58:34
字體:
來源:轉載
供稿:網友

封裝PHP的圖片水印的類,供大家參考,具體內容如下

<?phpheader('Content-type:text/html;charset=utf8');$img = new Image();// $img->water('2a.jpg','logo.gif',0);class Image{  //路徑  protected $path;  //是否啟用隨機名字  protected $isRandName;  //要保存的圖像類型  protected $type;    //通過構造方法隊成員屬性進行初始化  function __construct($path='./',$isRandName=true,$type='png'){    $this->path = $path;    $this->isRandName = $isRandName;    $this->type = $type;  }  //對外公開的水印方法    /**   * @param char $image  原圖   * @param char $water  水印圖片   * @param char $postion 位置   * @param int $tmp   透明度   * @param char $prefix 前綴   */  function water($image,$water,$postion,$tmp=100,$prefix='water_'){    //判斷這兩個圖片是否存在    if(!file_exists($image)||!file_exists($water)){      die('圖片資源不存在');    }    //得到原圖和水印圖片的寬高    $imageInfo = self::getImageInfo($image);    $waterInfo = self::getImageInfo($water);    //判斷水印圖片是否能貼上來    if (!$this->checkImage($imageInfo,$waterInfo)){      die('水印圖片太大');    }    //打開圖片    $imageRes = self::openAnyImage($image);    $waterRes = self::openAnyImage($water);    //根據水印圖片的位置計算水印圖片的坐標    $pos = $this->getPosition($postion,$imageInfo,$waterInfo);    //將水印圖片貼過來    imagecopymerge($imageRes, $waterRes, $pos['x'], $pos['y'], 0, 0, $waterInfo["width"], $waterInfo["height"], $tmp);    //得到要保存圖片的文件名    $newName = $this->createNewName($image,$prefix);    //得到保存圖片的路徑,也就是文件的全路徑    $newPath = rtrim($this->path,'/').'/'.$newName;    //保存圖片    $this->saveImage($imageRes,$newPath);    //銷毀資源    imagedestroy($imageRes);    imagedestroy($waterRes);        //返回路徑    return $newPath;  }  //保存圖像資源  protected function saveImage($imageRes,$newPath){    $func = 'image'.$this->type;    //通過變量函數進行保存    $func($imageRes,$newPath);  }  //得到文件名函數  protected function createNewName($imagePath,$prefix){    if ($this->isRandName){      $name = $prefix.uniqid().'.'.$this->type;    }else {      $name = $prefix.pathinfo($imagePath)['filename'].'.'.$this->type;    }    return $name;  }  //根據位置計算水印圖片的坐標  protected function getPosition($postion,$imageInfo,$waterInfo){    switch ($postion){      case 1:        $x = 0;        $y = 0;        break;      case 2:        $x = ($imageInfo['width']-$waterInfo["width"])/2;        $y = 0;        break;      case 3:        $x = $imageInfo["width"]- $waterInfo["width"];        $y = 0;        break;      case 4:        $x = 0;        $y = ($imageInfo["height"]-$waterInfo["height"])/2;        break;      case 5:        $x = ($imageInfo['width']-$waterInfo["width"])/2;        $y = ($imageInfo["height"]-$waterInfo["height"])/2;        break;      case 6:        $x = $imageInfo["width"]- $waterInfo["width"];        $y = ($imageInfo["height"]-$waterInfo["height"])/2;        break;      case 7:        $x = 0;        $y = $imageInfo['height'] - $waterInfo["height"];        break;      case 8:        $x = ($imageInfo['width']-$waterInfo["width"])/2;        $y = $imageInfo['height'] - $waterInfo["height"];        break;      case 9:        $x = $imageInfo["width"]- $waterInfo["width"];        $y = $imageInfo['height'] - $waterInfo["height"];        break;      case 0:        $x = mt_rand(0, $imageInfo["width"]- $waterInfo["width"]);        $y = mt_rand(0, $imageInfo['height'] - $waterInfo["height"]);        break;    }    return ['x'=>$x , 'y'=>$y];  }  protected function checkImage($imageInfo,$waterInfo){    if (($waterInfo['width'] > $imageInfo['width'])||($waterInfo['height'] > $imageInfo['height'])){      return false;    }    return true;  }  //靜態方法。根據圖片的路徑得到圖片的信息,寬度,高度、mime類型  static function getImageInfo($imagePath){    $info = getimagesize($imagePath);    $data['width']=$info[0];    $data['height']=$info[1];    $data['mime'] = $info['mime'];    return $data;  }  static function openAnyImage($imagePath){    //得到圖像的mime類型    $mime = self::getImageInfo($imagePath)['mime'];    //根據不同的mime類型打開不同的圖像    switch ($mime){      case 'image/png':          $image = imagecreatefrompng($imagePath);          break;      case 'image/gif':          $image = imagecreatefromgif($imagePath);          break;      case 'image/jpeg':          $image = imagecreatefromjpeg($imagePath);          break;      case 'image/wbmp':          $image = imagecreatefromwbmp($imagePath);          break;    }    return $image;  }  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 彭阳县| 漳平市| 光泽县| 五寨县| 越西县| 宝山区| 阳春市| 塔河县| 五莲县| 汉源县| 南川市| 太白县| 铁岭县| 沛县| 平遥县| 普格县| 农安县| 武清区| 渝中区| 芜湖县| 佛山市| 眉山市| 平邑县| 乌鲁木齐县| 邯郸市| 永丰县| 马公市| 滦南县| 许昌县| 岳西县| 如皋市| 民勤县| 沅江市| 秀山| 化州市| 台湾省| 桂阳县| 青海省| 霍州市| 美姑县| 滨州市|