支持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 相关文章推荐
如何限制访问者的ip(PHPBB的代码)
Oct 09 PHP
Ajax PHP简单入门教程代码
Apr 25 PHP
php 中文处理函数集合
Aug 27 PHP
PHP下escape解码函数的实现方法
Aug 08 PHP
DEDE采集大师官方留后门的删除办法
Jan 08 PHP
php简单实现无限分类树形列表的方法
Mar 27 PHP
WordPress中给媒体文件添加分类和标签的PHP功能实现
Dec 31 PHP
php 获取文件行数的方法总结
Oct 11 PHP
PHP数组生成XML格式数据的封装类实例
Nov 10 PHP
php计算给定日期所在周的开始日期和结束日期示例
Feb 06 PHP
php获取ip及网址的简单方法(必看)
Apr 01 PHP
php多进程并发编程防止出现僵尸进程的方法分析
Feb 28 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函数指定默认值方法的小例子
2013/12/04 PHP
php实现专业获取网站SEO信息类实例
2015/04/02 PHP
分享PHP函数实现数字与文字分页代码
2015/07/28 PHP
PHP实现数组根据某个单元字段排序操作示例
2018/08/01 PHP
深入理解Javascript闭包 新手版
2010/12/28 Javascript
jquery选择器之内容过滤选择器详解
2014/01/27 Javascript
js控制href内容的连接内容的变化示例
2014/04/30 Javascript
基于Css3和JQuery实现打字机效果
2015/08/11 Javascript
Angularjs中使用Filters详解
2016/03/11 Javascript
Bootstrap每天必学之级联下拉菜单
2016/03/27 Javascript
jQuery中fadein与fadeout方法用法示例
2016/09/16 Javascript
JS封装的选项卡TAB切换效果示例
2016/09/20 Javascript
获取jqGrid中选择的行的数据
2016/11/30 Javascript
angular实现表单验证及提交功能
2017/02/01 Javascript
React 使用recharts实现散点地图的示例代码
2018/12/07 Javascript
[01:02:06]LGD vs Mineski Supermajor 胜者组 BO3 第二场 6.5
2018/06/06 DOTA
Python中设置变量访问权限的方法
2015/04/27 Python
python生成excel的实例代码
2017/11/08 Python
浅谈Python2获取中文文件名的编码问题
2018/01/09 Python
python添加菜单图文讲解
2019/06/04 Python
Python学习笔记之装饰器
2020/08/06 Python
新加坡网上化妆品店:Best Buy World
2018/05/18 全球购物
印度尼西亚手表和包包商店:Urban Icon
2019/12/12 全球购物
Deux par Deux官方网站:设计师童装
2020/01/03 全球购物
PHP开发的一般流程
2013/08/13 面试题
初三政治教学反思
2014/01/30 职场文书
设计专业毕业生求职信
2014/06/25 职场文书
陈安之励志演讲稿
2014/08/21 职场文书
学雷锋的心得体会
2014/09/04 职场文书
乡领导班子四风问题对照检查材料
2014/09/25 职场文书
教师党的群众路线教育实践活动个人整改措施
2014/11/04 职场文书
2015年党建工作目标责任书
2015/05/08 职场文书
2015小学教师德育工作总结
2015/05/12 职场文书
2016保送生自荐信范文
2016/01/29 职场文书
goland 恢复已更改文件的操作
2021/04/28 Golang
Java 在线考试云平台的实现
2021/11/23 Java/Android