栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

一个经典实用的PHP图像处理类分享

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一个经典实用的PHP图像处理类分享

本图像处理类可以完成对图片的缩放、加水印和裁剪的功能,支持多种图片类型的处理,缩放时进行优化等。

path = rtrim($path,"/")."/";
  }
 
  
  function thumb($name, $width, $height,$qz="th_"){
    
    $imgInfo = $this->getInfo($name);
    
    $srcImg = $this->getImg($name, $imgInfo);
    
    $size = $this->getNewSize($name,$width, $height,$imgInfo);
    
    $newImg = $this->kidOfImage($srcImg, $size,$imgInfo);
    
    return $this->createNewImage($newImg, $qz.$name,$imgInfo);
  }
 
  
  function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){
    
    $curpath = rtrim($this->path,"/")."/";
    $dir = dirname($waterName);
    if($dir == "."){
      $wpath = $curpath;
    }else{
      $wpath = $dir."/";
      $waterName = basename($waterName);
    }
 
    
    if(file_exists($curpath.$groundName) && file_exists($wpath.$waterName)){
      $groundInfo = $this->getInfo($groundName); //获取背景信息
      $waterInfo = $this->getInfo($waterName, $dir);      //获取水印图片信息
      
      if(!$pos = $this->position($groundInfo, $waterInfo, $waterPos)){
 echo '水印不应该比背景图片小!';
 return false;
      }
 
      $groundImg = $this->getImg($groundName, $groundInfo);  //获取背景图像资源
      $waterImg = $this->getImg($waterName, $waterInfo, $dir); //获取水印图片资源
 
      
      $groundImg = $this->copyImage($groundImg, $waterImg, $pos, $waterInfo);
      
      return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);
 
    }else{
      echo '图片或水印图片不存在!';
      return false;
    }
  }
 
  
  function cut($name, $x, $y, $width, $height, $qz="cu_"){
    $imgInfo=$this->getInfo($name);  //获取图片信息
    
    if( (($x+$width) > $imgInfo['width']) || (($y+$height) > $imgInfo['height'])){
      echo "裁剪的位置超出了背景图片范围!";
      return false;
    }
 
    $back = $this->getImg($name, $imgInfo);     //获取图片资源
    
    $cutimg = imagecreatetruecolor($width, $height);
    
    imagecopyresampled($cutimg, $back, 0, 0, $x, $y, $width, $height, $width, $height);
    imagedestroy($back);
    
    return $this->createNewImage($cutimg, $qz.$name,$imgInfo);
  }
 
  
  private function position($groundInfo, $waterInfo, $waterPos){
    
    if( ($groundInfo["width"]<$waterInfo["width"]) || ($groundInfo["height"]<$waterInfo["height"]) ) {
      return false;
    }
    switch($waterPos) {
      case 1:     //1为顶端居左
 $posX = 0;
 $posY = 0;
 break;
      case 2:     //2为顶端居中
 $posX = ($groundInfo["width"] - $waterInfo["width"]) / 2;
 $posY = 0;
 break;
      case 3:     //3为顶端居右
 $posX = $groundInfo["width"] - $waterInfo["width"];
 $posY = 0;
 break;
      case 4:     //4为中部居左
 $posX = 0;
 $posY = ($groundInfo["height"] - $waterInfo["height"]) / 2;
 break;
      case 5:     //5为中部居中
 $posX = ($groundInfo["width"] - $waterInfo["width"]) / 2;
 $posY = ($groundInfo["height"] - $waterInfo["height"]) / 2;
 break;
      case 6:     //6为中部居右
 $posX = $groundInfo["width"] - $waterInfo["width"];
 $posY = ($groundInfo["height"] - $waterInfo["height"]) / 2;
 break;
      case 7:     //7为底端居左
 $posX = 0;
 $posY = $groundInfo["height"] - $waterInfo["height"];
 break;
      case 8:     //8为底端居中
 $posX = ($groundInfo["width"] - $waterInfo["width"]) / 2;
 $posY = $groundInfo["height"] - $waterInfo["height"];
 break;
      case 9:     //9为底端居右
 $posX = $groundInfo["width"] - $waterInfo["width"];
 $posY = $groundInfo["height"] - $waterInfo["height"];
 break;
      case 0:
      default:    //随机
 $posX = rand(0,($groundInfo["width"] - $waterInfo["width"]));
 $posY = rand(0,($groundInfo["height"] - $waterInfo["height"]));
 break;
    }
    return array("posX"=>$posX, "posY"=>$posY);
  }
 
  
  private function getInfo($name, $path=".") {
    $spath = $path=="." ? rtrim($this->path,"/")."/" : $path.'/';
 
    $data = getimagesize($spath.$name);
    $imgInfo["width"]  = $data[0];
    $imgInfo["height"] = $data[1];
    $imgInfo["type"]  = $data[2];
 
    return $imgInfo;
  }
 
  
  private function getImg($name, $imgInfo, $path='.'){
 
    $spath = $path=="." ? rtrim($this->path,"/")."/" : $path.'/';
    $srcPic = $spath.$name;
 
    switch ($imgInfo["type"]) {
      case 1:  //gif
 $img = imagecreatefromgif($srcPic);
 break;
      case 2:  //jpg
 $img = imagecreatefromjpeg($srcPic);
 break;
      case 3:  //png
 $img = imagecreatefrompng($srcPic);
 break;
      default:
 return false;
 break;
    }
    return $img;
  }
 
  
  private function getNewSize($name, $width, $height, $imgInfo){
    $size["width"] = $imgInfo["width"];     //原图片的宽度
    $size["height"] = $imgInfo["height"];    //原图片的高度
 
    if($width < $imgInfo["width"]){
      $size["width"]=$width;   //缩放的宽度如果比原图小才重新设置宽度
    }
 
    if($height < $imgInfo["height"]){
      $size["height"] = $height; //缩放的高度如果比原图小才重新设置高度
    }
    
    if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){
      $size["height"] = round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
    }else{
      $size["width"] = round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
    }
 
    return $size;
  }
 
  
  private function createNewImage($newImg, $newName, $imgInfo){
    $this->path = rtrim($this->path,"/")."/";
    switch ($imgInfo["type"]) {
      case 1://gif
 $result = imageGIF($newImg, $this->path.$newName);
 break;
      case 2://jpg
 $result = imageJPEG($newImg,$this->path.$newName);
 break;
      case 3://png
 $result = imagePng($newImg, $this->path.$newName);
 break;
    }
    imagedestroy($newImg);
    return $newName;
  }
 
  
  private function copyImage($groundImg, $waterImg, $pos, $waterInfo){
    imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"],$waterInfo["height"]);
    imagedestroy($waterImg);
    return $groundImg;
  }
 
  
  private function kidOfImage($srcImg, $size, $imgInfo){
    $newImg = imagecreatetruecolor($size["width"], $size["height"]);
    $otsc = imagecolortransparent($srcImg);
    if( $otsc >= 0 && $otsc < imagecolorstotal($srcImg)) {
      $transparentcolor = imagecolorsforindex( $srcImg, $otsc );
      $newtransparentcolor = imagecolorallocate(
      $newImg,
      $transparentcolor['red'],
      $transparentcolor['green'],
      $transparentcolor['blue']
      );
      imagefill( $newImg, 0, 0, $newtransparentcolor );
      imagecolortransparent( $newImg, $newtransparentcolor );
    }
    imagecopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );
    imagedestroy($srcImg);
    return $newImg;
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/46965.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号