支持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 PDO中文乱码解决办法
Jul 20 PHP
PHP 内存缓存加速功能memcached安装与用法
Sep 03 PHP
PHP中开发XML应用程序之基础篇 添加节点 删除节点 查询节点 查询节
Jul 09 PHP
深入浅析yii2-gii自定义模板的方法
Apr 26 PHP
php 如何获取文件的后缀名
Jun 05 PHP
jQuery+php简单实现全选删除的方法
Nov 28 PHP
php实现36进制与10进制转换功能示例
Jan 10 PHP
php中分页及SqlHelper类用法实例
Jan 12 PHP
详谈phpAdmin修改密码后拒绝访问的问题
Apr 03 PHP
Laravel框架自定义验证过程实例分析
Feb 01 PHP
ThinkPHP框架实现的微信支付接口开发完整示例
Apr 10 PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
Mar 30 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
收集的DedeCMS一些使用经验
2007/03/17 PHP
PHP下用rmdir实现删除目录的三种方法小结
2008/04/20 PHP
php xml留言板 xml存储数据的简单例子
2009/08/24 PHP
php中判断文件存在是用file_exists还是is_file的整理
2012/09/12 PHP
php多个字符串替换成同一个的解决方法
2013/06/18 PHP
Ajax,UTF-8还是GB2312 eval 还是execScript
2008/11/13 Javascript
JavaScript中对象属性的添加和删除示例
2014/05/12 Javascript
jQuery给div,Span, a ,button, radio 赋值与取值
2016/06/24 Javascript
jQuery中slidedown与slideup方法用法示例
2016/09/16 Javascript
JQuery控制DIV的选取实现方法
2016/09/18 Javascript
js 获取本地文件及目录的方法(推荐)
2016/11/10 Javascript
基于BootstrapValidator的Form表单验证(24)
2016/12/12 Javascript
jQuery的ajax中使用FormData实现页面无刷新上传功能
2017/01/16 Javascript
javascript内存分配原理实例分析
2017/04/10 Javascript
jQuery实现的弹幕效果完整实例
2017/09/06 jQuery
AngularJS中控制器函数的定义与使用方法示例
2017/10/10 Javascript
JavaScript图片处理与合成总结
2018/03/04 Javascript
webpack-mvc 传统多页面组件化开发详解
2019/05/07 Javascript
使用jQuery如何写一个含验证码的登录界面
2019/05/13 jQuery
弱类型语言javascript中 a,b 的运算实例小结
2019/08/07 Javascript
解决layui调用自定义方法提示未定义的问题
2019/09/14 Javascript
javascript 使用sleep函数的常见方法详解
2020/04/26 Javascript
python通过scapy获取局域网所有主机mac地址示例
2014/05/04 Python
python实现udp数据报传输的方法
2014/09/26 Python
Django应用程序中如何发送电子邮件详解
2017/02/04 Python
Python编程判断这天是这一年第几天的方法示例
2017/04/18 Python
微信公众号token验证失败解决方案
2019/07/22 Python
您附近的水疗和健康场所:Spafinder(美国)
2019/07/05 全球购物
医院护士见习期自我鉴定
2014/04/10 职场文书
人大代表选举标语
2014/10/07 职场文书
致接力运动员加油稿
2015/07/21 职场文书
Python包管理工具pip的15 个使用小技巧
2021/05/17 Python
python 中yaml文件用法大全
2021/07/04 Python
PostGIS的安装与入门使用指南
2022/01/18 PostgreSQL
Java 使用类型为Object的变量指向任意类型的对象
2022/04/13 Java/Android
超越Nginx的Web服务器caddy优雅用法
2022/06/21 Servers