支持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 相关文章推荐
用Apache反向代理设置对外的WWW和文件服务器
Oct 09 PHP
php5.2.0内存管理改进
Jan 22 PHP
php结合表单实现一些简单功能的例子
Jun 04 PHP
shopex中集成的站长统计功能的代码简单分析
Aug 11 PHP
PHP获取文件相对路径的方法
Feb 26 PHP
PHP 常用的header头部定义汇总
Jun 19 PHP
浅谈PHP中try{}catch{}的使用方法
Dec 09 PHP
PHP与JavaScript针对Cookie的读写、交互操作方法详解
Aug 07 PHP
php获取ajax的headers方法与内容实例
Dec 27 PHP
PHP中rename()函数的妙用讲解
Feb 28 PHP
Laravel 创建指定表 migrate的例子
Oct 09 PHP
利用ajax+php实现商品价格计算
Mar 31 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去掉字符串的最后一个字符附substr()的用法
2011/03/23 PHP
php将图片保存为不同尺寸图片的图片类实例
2015/03/30 PHP
php 截取GBK文档某个位置开始的n个字符方法
2017/03/08 PHP
Laravel下生成验证码的类
2017/11/15 PHP
用javascript getComputedStyle获取和设置style的原理
2008/10/10 Javascript
jQuery点击后一组图片左右滑动的实现代码
2012/08/16 Javascript
javascript通过navigator.userAgent识别各种浏览器
2013/10/25 Javascript
JS操作CSS随机改变网页背景实现思路
2014/03/10 Javascript
JavaScript中使用stopPropagation函数停止事件传播例子
2014/08/27 Javascript
jQuery后代选择器用法实例
2014/12/23 Javascript
JavaScript实现查找字符串中第一个不重复的字符
2014/12/29 Javascript
用Move.js配合创建CSS3动画的入门指引
2015/07/22 Javascript
基于AngularJs + Bootstrap + AngularStrap相结合实现省市区联动代码
2016/05/30 Javascript
基本DOM节点操作
2017/01/17 Javascript
在JS中如何把毫秒转换成规定的日期时间格式实例
2017/05/11 Javascript
angular 实时监听input框value值的变化触发函数方法
2018/08/31 Javascript
了解javascript中的Dom操作
2019/05/27 Javascript
javascript设计模式 ? 策略模式原理与用法实例分析
2020/04/21 Javascript
解决vue+router路由跳转不起作用的一项原因
2020/07/19 Javascript
[50:50]完美世界DOTA2联赛PWL S3 Galaxy Racer vs Phoenix 第一场 12.10
2020/12/13 DOTA
python中的一些类型转换函数小结
2013/02/10 Python
Python最基本的数据类型以及对元组的介绍
2015/04/14 Python
简单谈谈Python中的几种常见的数据类型
2017/02/10 Python
python实现名片管理系统
2018/11/29 Python
Python字符串逆序的实现方法【一题多解】
2019/02/18 Python
python使用Matplotlib改变坐标轴的默认位置
2019/10/18 Python
Django框架ORM数据库操作实例详解
2019/11/07 Python
Python3打包exe代码2种方法实例解析
2020/02/17 Python
Pycharm中配置远程Docker运行环境的教程图解
2020/06/11 Python
美国知名的在线旅游服务网站:Priceline
2016/07/23 全球购物
Elemental Herbology官网:英国美容品牌
2019/04/27 全球购物
会计电算化毕业生自荐信
2014/03/03 职场文书
中国梦团日活动总结
2014/07/07 职场文书
学习实践科学发展观心得体会
2014/09/10 职场文书
Python机器学习三大件之一numpy
2021/05/10 Python
Matplotlib可视化之添加让统计图变得简单易懂的注释
2021/06/11 Python