支持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 相关文章推荐
Zend公司全球首推PHP认证
Oct 09 PHP
用php写的serv-u的web申请账号的程序
Oct 09 PHP
用PHP获取Google AJAX Search API 数据的代码
Mar 12 PHP
php HandlerSocket的使用
May 02 PHP
基于PHP Socket配置以及实例的详细介绍
Jun 13 PHP
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
Jun 20 PHP
使用gd库实现php服务端图片裁剪和生成缩略图功能分享
Dec 25 PHP
PHP反向代理类代码
Aug 15 PHP
php5.4传引用时报错问题分析
Jan 22 PHP
php中this关键字用法分析
Dec 07 PHP
PHP abstract 抽象类定义与用法示例
May 29 PHP
thinkPHP框架实现的无限回复评论功能示例
Jun 09 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
推荐Discuz!5的PHP代码高亮显示与实现可运行代码
2007/03/15 PHP
PHP连接MySQL查询结果中文显示乱码解决方法
2013/10/25 PHP
CI(CodeIgniter)框架实现图片上传的方法
2017/03/24 PHP
php实现 master-worker 守护多进程模式的实例代码
2019/07/20 PHP
PHP 文件上传限制问题
2019/09/01 PHP
用javascript获得地址栏参数的两种方法
2006/11/08 Javascript
JQuery学习笔记 nt-child的使用
2011/01/17 Javascript
使用js获取地址栏中传递的值
2013/07/02 Javascript
利用javascript打开模态对话框(示例代码)
2014/01/11 Javascript
基于zepto.js简单实现上传图片
2016/06/21 Javascript
jQuery的extend方法【三种】
2016/12/14 Javascript
Angularjs2不同组件间的通信实例代码
2017/05/06 Javascript
AngularJS中scope的绑定策略实例分析
2017/10/30 Javascript
理理Vue细节(推荐)
2019/04/16 Javascript
JS实现的检验身份证格式并输出出生日期,年龄,性别,出生地示例
2019/05/17 Javascript
5分钟教你用nodeJS手写一个mock数据服务器的方法
2019/09/10 NodeJs
Vue 解决路由过渡动画抖动问题(实例详解)
2020/01/05 Javascript
微信小程序点击view动态添加样式过程解析
2020/01/21 Javascript
跟老齐学Python之大话题小函数(1)
2014/10/10 Python
Python切换pip安装源的方法详解
2016/11/18 Python
python 2.7.13 安装配置方法图文教程
2018/09/18 Python
linux查找当前python解释器的位置方法
2019/02/20 Python
Python数据类型之Number数字操作实例详解
2019/05/08 Python
Python进阶:生成器 懒人版本的迭代器详解
2019/06/29 Python
Python的信号库Blinker用法详解
2020/12/31 Python
Shopping happy life西班牙:以最优惠的价格提供最好的时尚配饰
2020/03/13 全球购物
地球鞋加拿大官网:Earth Shoes Canada
2020/11/17 全球购物
摄影实习自我鉴定
2013/09/20 职场文书
硕士研究生自我鉴定范文
2013/12/27 职场文书
幼儿园中班评语大全
2014/04/17 职场文书
信仰心得体会
2014/09/05 职场文书
2014年中班下学期工作总结
2014/12/11 职场文书
市场部经理岗位职责
2015/02/02 职场文书
写给导师的自荐信
2015/03/06 职场文书
师德承诺书2015
2015/04/28 职场文书
SpringBoot整合Redis入门之缓存数据的方法
2021/11/17 Redis