封装PHP的图片水印的类,供大家参考,具体内容如下

<"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;
  }
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com