支持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 获取MSN好友列表的代码(2009-05-14测试通过)
Sep 09 PHP
PHP代码网站如何防范SQL注入漏洞攻击建议分享
Mar 01 PHP
php 判断是否是中文/英文/数字示例代码
Sep 30 PHP
PHP页面实现定时跳转的方法
Oct 31 PHP
PHP实现Javascript中的escape及unescape函数代码分享
Feb 10 PHP
PHP之正则表达式捕获组与非捕获组(详解)
Jul 29 PHP
php如何获取文件的扩展名
Oct 28 PHP
PHP处理Ajax请求与Ajax跨域问题
Feb 13 PHP
laravel 框架配置404等异常页面
Jan 07 PHP
什么是PHP7中的孤儿进程与僵尸进程
Apr 14 PHP
laravel清除视图缓存的代码
Oct 23 PHP
laravel框架使用FormRequest进行表单验证,验证异常返回JSON操作示例
Feb 18 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 magic_quotes_gpc的一点认识与分析
2008/08/18 PHP
无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装(win+linux)
2012/05/05 PHP
CI框架中site_url()和base_url()的区别
2015/01/07 PHP
php 删除一维数组中某一个值元素的操作方法
2018/02/01 PHP
浅谈JavaScript function函数种类
2014/12/29 Javascript
js数组依据下标删除元素
2015/04/14 Javascript
JavaScript判断表单中多选框checkbox选中个数的方法
2015/08/17 Javascript
js实现n秒倒计时后才可以点击的效果
2015/12/20 Javascript
JavaScript设计模式开发中组合模式的使用教程
2016/05/18 Javascript
jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
2016/06/28 Javascript
JS实现放大、缩小及拖拽图片的方法【可兼容IE、火狐】
2016/08/23 Javascript
bootstrap监听滚动实现头部跟随滚动
2016/11/08 Javascript
为你的微信小程序体积瘦身详解
2017/05/20 Javascript
VueJS事件处理器v-on的使用方法
2017/09/27 Javascript
el-select数据过多懒加载的解决(loadmore)
2019/05/29 Javascript
vue项目实现图片上传功能
2019/12/23 Javascript
JS图片懒加载的优点及实现原理
2020/01/10 Javascript
Python企业编码生成系统之主程序模块设计详解
2019/07/26 Python
详解python中自定义超时异常的几种方法
2019/07/29 Python
python 实现二维字典的键值合并等函数
2019/12/06 Python
python 截取XML中bndbox的坐标中的图像,另存为jpg的实例
2020/03/10 Python
Python3开发环境搭建详细教程
2020/06/18 Python
通过实例解析python创建进程常用方法
2020/06/19 Python
通过实例简单了解Python sys.argv[]使用方法
2020/08/04 Python
20佳惊艳的HTML5应用程序示例分享
2011/05/03 HTML / CSS
澳大利亚拥有最佳跳伞降落点和最好服务的跳伞项目运营商:Skydive Australia
2018/03/05 全球购物
利物浦足球俱乐部官方商店(美国):Liverpool FC US
2019/10/09 全球购物
什么是Web Service?
2012/07/25 面试题
laravel使用redis队列实例讲解
2021/03/23 PHP
职工运动会邀请函
2014/02/02 职场文书
环保公益策划方案
2014/08/15 职场文书
安全检查汇报材料
2014/12/26 职场文书
迟到检讨书
2015/01/26 职场文书
2015年世界水日活动总结
2015/02/09 职场文书
2019年最新版见习人员管理制度!
2019/07/08 职场文书
Go语言特点及基本数据类型使用详解
2022/03/21 Golang