支持png透明图片的php生成缩略图类分享


Posted in PHP onFebruary 08, 2015

注:此功能依赖GD2图形库

最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图

试用了一下后,发现有这样几个问题:

1、png图片生成的缩略图是jpg格式的

2、png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景)

3、代码语法比较老

因此,在这个版本的基础上简单修改优化了一下。

PHP生成缩略图类

<?php
  /*
   * desc: Resize Image(png, jpg, gif)
   * author: 十年后的卢哥哥
   * date: 2014.11.13
   */
  class ResizeImage {
    //图片类型
    private $type;
    //实际宽度
    private $width;
    //实际高度
    private $height;
    //改变后的宽度
    private $resize_width;
    //改变后的高度
    private $resize_height;
    //是否裁图
    private $cut;
    //源图象
    private $srcimg;
    //目标图象地址
    private $dstimg;
    //临时创建的图象
    private $im;

    function __construct($imgPath, $width, $height, $isCut, $savePath) {
      $this->srcimg = $imgPath;
      $this->resize_width = $width;
      $this->resize_height = $height;
      $this->cut = $isCut;
      //图片的类型

      $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));

      //初始化图象
      $this->initi_img();
      //目标图象地址
      $this -> dst_img($savePath);
      //--
      $this->width = imagesx($this->im);
      $this->height = imagesy($this->im);
      //生成图象
      $this->newimg();
      ImageDestroy ($this->im);
    }

    private function newimg() {
      //改变后的图象的比例
      $resize_ratio = ($this->resize_width)/($this->resize_height);
      //实际图象的比例
      $ratio = ($this->width)/($this->height);
      if($this->cut) {
        //裁图
        $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
        if($this->type=="png") {
          imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
        }
        if($ratio>=$resize_ratio) {
          //高度优先
          imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
        } else {
          //宽度优先
          imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
        }
      } else {
        //不裁图
        if($ratio>=$resize_ratio) {
          $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
          if($this->type=="png") {
            imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
          }
          imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
        } else {
          $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
          if($this->type=="png") {
            imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
          }
          imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
        }
      }
      if($this->type=="png") {
        imagesavealpha($newimg, true);
        imagepng ($newimg,$this->dstimg);
      } else {
        imagejpeg ($newimg,$this->dstimg);
      }
    }

    //初始化图象
    private function initi_img() {
      if($this->type=="jpg") {
        $this->im = imagecreatefromjpeg($this->srcimg);
      }
      if($this->type=="gif") {
        $this->im = imagecreatefromgif($this->srcimg);
      }
      if($this->type=="png") {
        $this->im = imagecreatefrompng($this->srcimg);
      }
    }

    //图象目标地址
    private function dst_img($dstpath) {
      $full_length = strlen($this->srcimg);

      $type_length = strlen($this->type);
      $name_length = $full_length-$type_length;


      $name     = substr($this->srcimg,0,$name_length-1);
      $this->dstimg = $dstpath;
    }
  }
?>

使用

使用时,直接调用类的构造函数即可,构造函数如下:

$resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);

参数
$imgPath:原图片地址

$width:缩略图宽

$height:缩略图高

$isCut:是否裁剪,bool值

$savePath:缩略图地址(可以跟原图片地址相同)

示例

<?php
  include "ResizeImage.php";

  //jpg
  $jpgResize = new ResizeImage("img/test_1920_1200.jpg", 320, 240, false, "img/test_320_240.jpg");

  //png
  $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");

?>

效果

支持png透明图片的php生成缩略图类分享支持png透明图片的php生成缩略图类分享

PHP 相关文章推荐
PHP提取中文首字母
Apr 09 PHP
php递归列出所有文件和目录的代码
Sep 10 PHP
PHP 日常开发小技巧
Sep 23 PHP
Yii入门教程之目录结构、入口文件及路由设置
Nov 25 PHP
PHP实现一维数组转二维数组的方法
Feb 25 PHP
php实现的操作excel类详解
Jan 15 PHP
深入浅析yii2-gii自定义模板的方法
Apr 26 PHP
php数组冒泡排序算法实例
May 06 PHP
基于Laravel实现的用户动态模块开发
Sep 21 PHP
php新建文件的方法实例
Sep 26 PHP
如何在Laravel5.8中正确地应用Repository设计模式
Nov 26 PHP
PHPstorm激活码2020年5月13日亲测有效
Sep 17 PHP
基于GD2图形库的PHP生成图片缩略图类代码分享
Feb 08 #PHP
php中get_object_vars()方法用法实例
Feb 08 #PHP
php面向对象中static静态属性与方法的内存位置分析
Feb 08 #PHP
php面向对象中static静态属性和静态方法的调用
Feb 08 #PHP
php延迟静态绑定实例分析
Feb 08 #PHP
PHP调用Linux命令权限不足问题解决方法
Feb 07 #PHP
PHP处理大量表单字段的便捷方法
Feb 07 #PHP
You might like
PHP4(windows版本)中的COM函数
2006/10/09 PHP
PHP中PDO基础教程 入门级
2011/09/04 PHP
Codeigniter实现处理用户登录验证后的URL跳转
2014/06/12 PHP
初识php MVC
2014/09/10 PHP
PHP中的命名空间详细介绍
2015/07/02 PHP
jquery 插件开发方法小结
2009/10/23 Javascript
eval的两组性能测试数据
2012/08/17 Javascript
jquery选择器之基本过滤选择器详解
2014/01/27 Javascript
jQuery圆形统计图开发实例
2015/01/04 Javascript
js代码验证手机号码和电话号码是否合法
2015/07/30 Javascript
jQuery垂直多级导航菜单代码分享
2015/08/18 Javascript
Vue键盘事件用法总结
2017/04/18 Javascript
vue 多入口文件搭建 vue多页面搭建的实例讲解
2018/03/12 Javascript
VUE写一个简单的表格实例
2019/08/06 Javascript
jQuery单页面文字搜索插件jquery.fullsearch.js的使用方法
2020/02/04 jQuery
Python 正则表达式的高级用法
2016/12/04 Python
python爬虫headers设置后无效的解决方法
2017/10/21 Python
Python基于OpenCV库Adaboost实现人脸识别功能详解
2018/08/25 Python
python-opencv颜色提取分割方法
2018/12/08 Python
Ubuntu18.04中Python2.7与Python3.6环境切换
2019/06/14 Python
python 搜索大文件的实例代码
2019/07/08 Python
Python 绘制酷炫的三维图步骤详解
2019/07/12 Python
Python实现加密的RAR文件解压的方法(密码已知)
2020/09/11 Python
CSS3 text shadow字体阴影效果
2016/01/08 HTML / CSS
土耳其家居建材网站:Koçtaş
2016/11/22 全球购物
澳大利亚工具仓库:Tools Warehouse
2018/10/15 全球购物
.NET remoting的两种通道是什么
2016/05/31 面试题
应聘教师推荐信
2013/10/31 职场文书
商务英语大学生职业生涯规划书范文
2014/01/01 职场文书
创业融资计划书
2014/04/25 职场文书
养成教育经验材料
2014/05/26 职场文书
普通党员个人剖析材料
2014/10/08 职场文书
古诗之感恩老师
2019/10/24 职场文书
Go语言基础切片的创建及初始化示例详解
2021/11/17 Golang
pandas中关于apply+lambda的应用
2022/02/28 Python
mysql数据插入覆盖和时间戳的问题及解决
2022/03/25 MySQL