支持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 中include()与require()的对比
Oct 09 PHP
利用discuz自带通行证整合dedecms的方法以及文件下载
Mar 06 PHP
php 引用(&amp;)详解
Nov 20 PHP
又一个php 分页类实现代码
Dec 03 PHP
PHP制作3D扇形统计图以及对图片进行缩放操作实例
Oct 23 PHP
举例讲解PHP面对对象编程的多态
Aug 12 PHP
php使用ZipArchive函数实现文件的压缩与解压缩
Oct 27 PHP
PHP使用数组依次替换字符串中匹配项
Jan 08 PHP
PHP类的声明与实例化及构造方法与析构方法详解
Jan 26 PHP
PHP-FPM实现性能优化
Mar 31 PHP
Linux下安装Memcached服务器和客户端与PHP使用示例
Apr 15 PHP
Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址方法详解
Apr 06 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
php 无限级缓存的类的扩展
2009/03/16 PHP
php版微信开发Token验证失败或请求URL超时问题的解决方法
2016/09/23 PHP
PHP中__set()实例用法和基础讲解
2019/07/23 PHP
jValidate 基于jQuery的表单验证插件
2009/12/12 Javascript
JS代码同步文本框内容的实例方法
2013/07/12 Javascript
jquery淡化版banner异步图片文字效果切换图片特效
2014/04/08 Javascript
JS实现简易图片轮播效果的方法
2015/03/25 Javascript
谈谈AngularJs中的隐藏和显示
2015/12/09 Javascript
AngularJS页面访问时出现页面闪烁问题的解决
2016/03/06 Javascript
mui开发中获取单选按钮、复选框的值(实例讲解)
2017/07/24 Javascript
详解使用element-ui table组件的筛选功能的一个小坑
2018/11/02 Javascript
简单了解JavaScript中常见的反模式
2019/06/21 Javascript
js实现秒表计时器
2019/12/16 Javascript
js DOM的事件常见操作实例详解
2019/12/16 Javascript
JavaScript日期库date-fn.js使用方法解析
2020/09/09 Javascript
python里大整数相乘相关技巧指南
2014/09/12 Python
在Python的web框架中编写创建日志的程序的教程
2015/04/30 Python
python读取二进制mnist实例详解
2017/05/31 Python
利用python爬取散文网的文章实例教程
2017/06/18 Python
Python爬虫包BeautifulSoup异常处理(二)
2018/06/17 Python
Python日期时间Time模块实例详解
2019/04/15 Python
python进程和线程用法知识点总结
2019/05/28 Python
python自动发邮件总结及实例说明【推荐】
2019/05/31 Python
python opencv 二值化 计算白色像素点的实例
2019/07/03 Python
Python工程师必考的6个经典面试题
2020/06/28 Python
Python如何设置指定窗口为前台活动窗口
2020/08/12 Python
有750多个顶级品牌的瑞士时尚在线:ABOUT YOU
2017/01/04 全球购物
互动出版网:专业书籍
2017/03/21 全球购物
Java如何读取CLOB字段
2013/10/10 面试题
2014年高考决心书
2014/03/11 职场文书
农村婚礼主持词
2014/03/13 职场文书
幼儿园招生广告
2014/03/19 职场文书
团委工作总结2015
2015/04/02 职场文书
离职信范文
2015/06/23 职场文书
《雪域豹影》读后感:父爱的伟大
2019/12/23 职场文书
微信小程序APP的生命周期及页面的生命周期
2022/04/19 Javascript