支持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 相关文章推荐
在mysql数据库原有字段后增加新内容
Nov 26 PHP
php旋转图片90度的方法
Nov 07 PHP
php查询mssql出现乱码的解决方法
Dec 29 PHP
ThinkPHP控制器详解
Jul 27 PHP
php数字运算验证码的实现代码
Jul 30 PHP
php无限分类使用concat如何实现
Nov 05 PHP
在WordPress中使用wp-cron插件来设置定时任务
Dec 10 PHP
PHP实现搜索时记住状态的方法示例
May 11 PHP
PHP获取访问设备信息的方法示例
Feb 20 PHP
PHP文件上传小程序 适合初学者学习!
May 23 PHP
在thinkphp5.0路径中实现去除index.php的方式
Oct 16 PHP
TP5框架实现上传多张图片的方法分析
Mar 29 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
这东西价格,可以买几台TECSUN S-2000
2021/03/02 无线电
php microtime获取浮点的时间戳
2010/02/21 PHP
为IP查询添加GOOGLE地图功能的代码
2010/08/08 PHP
javascript下利用arguments实现string.format函数
2010/08/24 Javascript
extjs grid设置某列背景颜色和字体颜色的实现方法
2010/09/06 Javascript
Mac地址验证的javascript代码
2013/11/09 Javascript
JavaScript使用concat连接数组的方法
2015/04/06 Javascript
JS中获取函数调用链所有参数的方法
2015/05/07 Javascript
jquery特效 点击展示与隐藏全文
2015/12/09 Javascript
微信小程序 数据封装,参数传值等经验分享
2017/01/09 Javascript
jQuery图片瀑布流的简单实现代码
2017/03/15 Javascript
jQuery Ajax自定义分页组件(jquery.loehpagerv1.0)实例详解
2017/05/01 jQuery
vue项目常用组件和框架结构介绍
2017/12/24 Javascript
一个Vue视频媒体多段裁剪组件的实现示例
2018/08/09 Javascript
使用wxapp-img-loader自定义组件实现微信小程序图片预加载功能
2018/10/18 Javascript
关于vue里页面的缓存详解
2019/11/04 Javascript
html中创建并调用vue组件的几种方法汇总
2020/11/17 Javascript
python解析xml文件实例分享
2013/12/04 Python
python实现简单的socket server实例
2015/04/29 Python
Python在groupby分组后提取指定位置记录方法
2018/04/20 Python
cmd运行python文件时对结果进行保存的方法
2018/05/16 Python
把csv文件转化为数组及数组的切片方法
2018/07/04 Python
Pycharm设置utf-8自动显示方法
2019/01/17 Python
Python爬虫实战之12306抢票开源
2019/01/24 Python
Python学习笔记之列表和成员运算符及列表相关方法详解
2019/08/22 Python
Pycharm debug调试时带参数过程解析
2020/02/03 Python
viagogo英国票务平台:演唱会、体育比赛、戏剧门票
2017/03/24 全球购物
意大利香水和彩妆护肤品购物网站:Ditano
2017/08/13 全球购物
Sixt美国租车:高端豪华车型自驾体验
2017/09/02 全球购物
Looking4Parking美国:全球排名第一的机场停车比较品牌
2019/08/26 全球购物
教师个人自我鉴定
2014/02/08 职场文书
新闻工作者先进事迹
2014/05/26 职场文书
三八妇女节超市活动方案
2014/08/18 职场文书
Redis5之后版本的高可用集群搭建的实现
2021/04/27 Redis
MYSQL 表的全面总结
2021/11/11 MySQL
安装Ruby和 Rails的详细步骤
2022/04/19 Ruby