封装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;
  }
  
}

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

标签:
PHP图片水印类,PHP图片水印,PHP水印类

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“PHP图片水印类的封装”

暂无“PHP图片水印类的封装”评论...