侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 7年前   165

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

path = rtrim($path,"/")."/";  }   /**   * 对指定的图像进行缩放   * @param  string $name  是需要处理的图片名称   * @param  int $width   缩放后的宽度   * @param  int $height   缩放后的高度   * @param  string $qz   是新图片的前缀   * @return mixed      是缩放后的图片名称,失败返回false;   */  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);    /* 通过本类的私有方法,保存缩略图并返回新缩略图的名称,以"th_"为前缀 */    return $this->createNewImage($newImg, $qz.$name,$imgInfo);  }   /**   * 为图片添加水印   * @param  string $groundName 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式   * @param  string $waterName 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式   * @param  int $waterPos    水印位置,有10种状态,0为随机位置;   *    1为顶端居左,2为顶端居中,3为顶端居右;   *    4为中部居左,5为中部居中,6为中部居右;   *    7为底端居左,8为底端居中,9为底端居右;   * @param  string $qz     加水印后的图片的文件名在原文件名前面加上这个前缀   * @return  mixed        是生成水印后的图片名称,失败返回false   */  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);      /* 通过本类的私有方法,保存加水图片并返回新图片的名称,默认以"wa_"为前缀 */      return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);     }else{      echo '图片或水印图片不存在!';      return false;    }  }   /**   * 在一个大的背景图片中剪裁出指定区域的图片   * @param  string $name  需要剪切的背景图片   * @param  int $x     剪切图片左边开始的位置   * @param  int $y     剪切图片顶部开始的位置   * @param  int $width   图片剪裁的宽度   * @param  int $height   图片剪裁的高度   * @param  string $qz   新图片的名称前缀   * @return  mixed      裁剪后的图片名称,失败返回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()函数对图片进行裁剪 */    imagecopyresampled($cutimg, $back, 0, 0, $x, $y, $width, $height, $width, $height);    imagedestroy($back);    /* 通过本类的私有方法,保存剪切图并返回新图片的名称,默认以"cu_"为前缀 */    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;  }   /*内部使用的私有方法, 用于创建支持各种图片格式(jpg,gif,png三种)资源 */  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;  }}

您可能感兴趣的文章:

  • 9段PHP实用功能的代码推荐
  • 几个实用的PHP内置函数使用指南
  • 简单实用的PHP防注入类实例
  • 一款简单实用的php操作mysql数据库类
  • 非常实用的PHP常用函数汇总
  • 制作安全性高的PHP网站的几个实用要点
  • Thinkphp中的curd应用实用要点
  • 9个实用的PHP代码片段分享
  • PHP实用函数分享之去除多余的0
  • 7个鲜为人知却非常实用的PHP函数
  • PHP实现简单实用的验证码类
  • 6个超实用的PHP代码片段
  • PHP判断字符串长度的两种方法很实用
  • PHPStrom中实用的功能和快捷键大全
  • 四个PHP非常实用的功能


  • 上一条:
    一个经典的PHP验证码类分享
    下一条:
    PHP中一些可以替代正则表达式函数的字符串操作函数
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客