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

PHP缩略图生成和图片水印制作

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

1.开始

在网站上传图片过程,经常用到缩略图功能。这里我自己写了一个图片处理的Image类,能生成缩略图,并且可以添加水印图。

2.如何生成缩略图

     生成缩略图,关键的是如何计算缩放比率。

     这里,我根据图片等比缩放,宽高的几种常见变化,得出一个算缩放比率算法是,使用新图(即缩略图)的宽高,分别除以原图的宽高,看哪个值大,就取它作为缩放比率:

     缩放比率  = Max( { 新图高度  / 原图高度 ,  新图宽度  / 原图宽度 } )

     也就是:

      If ( (新图高度  / 原图高度)  >  (新图宽度  / 原图宽度 ) )  {

              缩放比率 =  新图高度  / 原图高度;

      }ELSE {

             缩放比率 =  新图宽度 / 原图宽度;

     }

 这里列出场景的图片缩放场景,及处理方法:

 e.g

场景1,原图比新图大的情况, 缩放比率 =  新图宽度 / 原图宽度 :

场景2,原图比新图大的情况,b. 缩放比率 =  新图高度 / 原图高度 :

场景3,原图比新图大的情况,而且新图宽高相等,即新图形状是正方形,那么上面的缩放算法也是适用的。

场景4,如果 “新图宽度 >= 原图宽度”  ,同时  “新图高度 >= 原图高度”,那么不缩放图片,也不放大图片,保持原图。

场景5,如果 “新图宽度 < 原图宽度”,同时  “新图高度 >= 原图高度”  ,那么先设置  “新图高度= 原图高度”,再剪切。

场景6,如果 “新图高度 < 原图高度”,同时  “新图宽度 >= 原图宽度”  ,那么先设置  “新图宽度= 原图宽度”,再剪切。

3.如何添加水印图片
   添加水印很容易,我这里没考虑那么复杂,主要是控制水印位置在图片的右下角,和控制水印在图片中的大小。如,当目标图片与水印图大小接近,那么需要先等比缩放水印图片,再添加水印图片。

左边两幅图,上面是原图,下面是水印图,右边的缩放后加水印的新图。

 4.类图


5.PHP代码

5.1. 构造函数 __construct()

     在Image类中,除了构造函数__construct()是public,其它函数都为private.也就是在函数__construct()中,直接完成了生成缩略图和添加水印图的功能。如果,只生成缩略图而不需要添加水印,那么直接在__construct()的参数$markPath,设置为null即可。

     其中,“$this->quality = $quality ? $quality : 75;” 控制输出为JPG图片时,控制图片质量(0-100),默认值为75;

  /**   * Image constructor.   * @param string $imagePath 图片路径   * @param string $markPath 水印图片路径   * @param int $new_width 缩略图宽度   * @param int $new_height 缩略图高度   * @param int $quality JPG图片格输出质量   */  public function __construct(string $imagePath,    string $markPath = null,    int $new_width = null,    int $new_height = null,    int $quality = 75)  {    $this->imgPath = $_SERVER['DOCUMENT_ROOT'] . $imagePath;    $this->waterMarkPath = $markPath;    $this->newWidth = $new_width ? $new_width : $this->width;    $this->newHeight = $new_height ? $new_height : $this->height;    $this->quality = $quality ? $quality : 75;    list($this->width, $this->height, $this->type) = getimagesize($this->imgPath);    $this->img = $this->_loadImg($this->imgPath, $this->type);    //生成缩略图    $this->_thumb();    //添加水印图片    if (!empty($this->waterMarkPath)) $this->_addWaterMark();    //输出图片    $this->_outputImg();  }

 Note: 先生成缩略图,再在新图上添加水印 图片。 

5.2. 生成缩略图函数_thumb()

   /**   * 缩略图(按等比例,根据设置的宽度和高度进行裁剪)   */  private function _thumb()  {    //如果原图本身小于缩略图,按原图长高    if ($this->newWidth > $this->width) $this->newWidth = $this->width;    if ($this->newHeight > $this->height) $this->newHeight = $this->height;    //背景图长高    $gd_width = $this->newWidth;    $gd_height = $this->newHeight;    //如果缩略图宽高,其中有一边等于原图的宽高,就直接裁剪    if ($gd_width == $this->width || $gd_height == $this->height) {      $this->newWidth = $this->width;      $this->newHeight = $this->height;    } else {      //计算缩放比率      $per = 1;      if (($this->newHeight / $this->height) > ($this->newWidth / $this->width)) {        $per = $this->newHeight / $this->height;      } else {        $per = $this->newWidth / $this->width;      }      if ($per < 1) {        $this->newWidth = $this->width * $per;        $this->newHeight = $this->height * $per;      }    }    $this->newImg = $this->_CreateImg($gd_width, $gd_height, $this->type);    imagecopyresampled($this->newImg, $this->img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height);  }

生成缩略图函数_thumb() ,是按照前面的分析来进行编码。 

5.3. 添加水印图片函数 _addWaterMark()    

   /**   * 添加水印   */  private function _addWaterMark()  {    $ratio = 1 / 5; //水印缩放比率    $Width = imagesx($this->newImg);    $Height = imagesy($this->newImg);    $n_width = $Width * $ratio;    $n_height = $Width * $ratio;    list($markWidth, $markHeight, $markType) = getimagesize($this->waterMarkPath);    if ($n_width > $markWidth) $n_width = $markWidth;    if ($n_height > $markHeight) $n_height = $markHeight;    $Img = $this->_loadImg($this->waterMarkPath, $markType);    $Img = $this->_thumb1($Img, $markWidth, $markHeight, $markType, $n_width, $n_height);    $markWidth = imagesx($Img);    $markHeight = imagesy($Img);    imagecopyresampled($this->newImg, $Img, $Width - $markWidth - 10, $Height - $markHeight - 10, 0, 0, $markWidth, $markHeight, $markWidth, $markHeight);    imagedestroy($Img);  }

在添加水印图片中,用到一个_thumb1()函数来缩放水印图片:

  /**   * 缩略图(按等比例)   * @param resource $img 图像流   * @param int $width   * @param int $height   * @param int $type   * @param int $new_width   * @param int $new_height   * @return resource   */  private function _thumb1($img, $width, $height, $type, $new_width, $new_height)  {    if ($width < $height) {      $new_width = ($new_height / $height) * $width;    } else {      $new_height = ($new_width / $width) * $height;    }    $newImg = $this->_CreateImg($new_width, $new_height, $type);    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);    return $newImg;  }

5.4. 完整代码:

imgPath = $_SERVER['DOCUMENT_ROOT'] . $imagePath;  $this->waterMarkPath = $markPath;  $this->newWidth = $new_width ? $new_width : $this->width;  $this->newHeight = $new_height ? $new_height : $this->height;  $this->quality = $quality ? $quality : 75;  list($this->width, $this->height, $this->type) = getimagesize($this->imgPath);  $this->img = $this->_loadImg($this->imgPath, $this->type);  //生成缩略图  $this->_thumb();  //添加水印图片  if (!empty($this->waterMarkPath)) $this->_addWaterMark();  //输出图片  $this->_outputImg(); } /**  *图片输出  */ private function _outputImg() {  switch ($this->type) {   case 1: // GIF    imagegif($this->newImg, $this->imgPath);    break;   case 2: // JPG    if (intval($this->quality) < 0 || intval($this->quality) > 100) $this->quality = 75;    imagejpeg($this->newImg, $this->imgPath, $this->quality);    break;   case 3: // PNG    imagepng($this->newImg, $this->imgPath);    break;  }  imagedestroy($this->newImg);  imagedestroy($this->img); } /**  * 添加水印  */ private function _addWaterMark() {  $ratio = 1 / 5; //水印缩放比率  $Width = imagesx($this->newImg);  $Height = imagesy($this->newImg);  $n_width = $Width * $ratio;  $n_height = $Width * $ratio;  list($markWidth, $markHeight, $markType) = getimagesize($this->waterMarkPath);  if ($n_width > $markWidth) $n_width = $markWidth;  if ($n_height > $markHeight) $n_height = $markHeight;  $Img = $this->_loadImg($this->waterMarkPath, $markType);  $Img = $this->_thumb1($Img, $markWidth, $markHeight, $markType, $n_width, $n_height);  $markWidth = imagesx($Img);  $markHeight = imagesy($Img);  imagecopyresampled($this->newImg, $Img, $Width - $markWidth - 10, $Height - $markHeight - 10, 0, 0, $markWidth, $markHeight, $markWidth, $markHeight);  imagedestroy($Img); } /**  * 缩略图(按等比例,根据设置的宽度和高度进行裁剪)  */ private function _thumb() {  //如果原图本身小于缩略图,按原图长高  if ($this->newWidth > $this->width) $this->newWidth = $this->width;  if ($this->newHeight > $this->height) $this->newHeight = $this->height;  //背景图长高  $gd_width = $this->newWidth;  $gd_height = $this->newHeight;  //如果缩略图宽高,其中有一边等于原图的宽高,就直接裁剪  if ($gd_width == $this->width || $gd_height == $this->height) {   $this->newWidth = $this->width;   $this->newHeight = $this->height;  } else {   //计算缩放比率   $per = 1;   if (($this->newHeight / $this->height) > ($this->newWidth / $this->width)) {    $per = $this->newHeight / $this->height;   } else {    $per = $this->newWidth / $this->width;   }   if ($per < 1) {    $this->newWidth = $this->width * $per;    $this->newHeight = $this->height * $per;   }  }  $this->newImg = $this->_CreateImg($gd_width, $gd_height, $this->type);  imagecopyresampled($this->newImg, $this->img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); } /**  * 缩略图(按等比例)  * @param resource $img 图像流  * @param int $width  * @param int $height  * @param int $type  * @param int $new_width  * @param int $new_height  * @return resource  */ private function _thumb1($img, $width, $height, $type, $new_width, $new_height) {  if ($width < $height) {   $new_width = ($new_height / $height) * $width;  } else {   $new_height = ($new_width / $width) * $height;  }  $newImg = $this->_CreateImg($new_width, $new_height, $type);  imagecopyresampled($newImg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  return $newImg; } /**  * 加载图片  * @param string $imgPath  * @param int $type  * @return resource  */ private function _loadImg($imgPath, $type) {  switch ($type) {   case 1: // GIF    $img = imagecreatefromgif($imgPath);    break;   case 2: // JPG    $img = imagecreatefromjpeg($imgPath);    break;   case 3: // PNG    $img = imagecreatefrompng($imgPath);    break;   default: //其他类型    Tool::alertBack('不支持当前图片类型.' . $type);    break;  }  return $img; } /**  * 创建一个背景图像  * @param int $width  * @param int $height  * @param int $type  * @return resource  */ private function _CreateImg($width, $height, $type) {  $img = imagecreatetruecolor($width, $height);  switch ($type) {   case 3: //png    imagecolortransparent($img, 0); //设置背景为透明的    imagealphablending($img, false);    imagesavealpha($img, true);    break;   case 4://gif    imagecolortransparent($img, 0);    break;  }  return $img; }}

6.调用

调用非常简单,在引入类后,直接new 并输入对应参数即可:

e.g.

new Image($_path, MARK, 400, 200, 100);

7.小结
这个Image 类能够生成缩略图,不出现黑边,添加水印图,能根据图片的大小缩放水印图。当然有个缺点,就是不能缩放GIF的动画,因为涉及到帧的处理,比较麻烦。

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

您可能感兴趣的文章:

  • thinkPHP实现上传图片及生成缩略图功能示例
  • PHP基于ffmpeg实现转换视频,截图及生成缩略图的方法
  • PHP基于GD库实现的生成图片缩略图函数示例
  • PHP基于正则批量替换Img中src内容实现获取缩略图的功能示例
  • 使用ThinkPHP生成缩略图及显示
  • php生成图片缩略图功能示例
  • PHP生成图片缩略图类示例
  • php生成缩略图质量较差解决方法代码示例


  • 上一条:
    PHP验证码类ValidateCode解析
    下一条:
    php使用preg_match()函数验证ip地址的方法
  • 昵称:

    邮箱:

    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交流群

    侯体宗的博客