基于GD2图形库的PHP生成图片缩略图类代码分享


Posted in PHP onFebruary 08, 2015

要使用PHP生成图片缩略图,要保证你的PHP服务器安装了GD2图形库 使用一个类生成图片的缩略图

1.使用方法

$resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址");
//就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高

2. 缩略图类代码

//使用如下类就可以生成图片缩略图,
 
<?php
class resizeimage
{
  //图片类型
  var $type;
  //实际宽度
  var $width;
  //实际高度
  var $height;
  //改变后的宽度
  var $resize_width;
  //改变后的高度
  var $resize_height;
  //是否裁图
  var $cut;
  //源图象
  var $srcimg;
  //目标图象地址
  var $dstimg;
  //临时创建的图象
  var $im;
 
  function resizeimage($img, $wid, $hei,$c,$dstpath)
  {
    $this->srcimg = $img;
    $this->resize_width = $wid;
    $this->resize_height = $hei;
    $this->cut = $c;
    //图片的类型
  
$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
 
    //初始化图象
    $this->initi_img();
    //目标图象地址
    $this -> dst_img($dstpath);
    //--
    $this->width = imagesx($this->im);
    $this->height = imagesy($this->im);
    //生成图象
    $this->newimg();
    ImageDestroy ($this->im);
  }
  function newimg()
  {
    //改变后的图象的比例
    $resize_ratio = ($this->resize_width)/($this->resize_height);
    //实际图象的比例
    $ratio = ($this->width)/($this->height);
    if(($this->cut)=="1")
    //裁图
    {
      if($ratio>=$resize_ratio)
      //高度优先
      {
        $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
        imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
        ImageJpeg ($newimg,$this->dstimg);
      }
      if($ratio<$resize_ratio)
      //宽度优先
      {
        $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
        imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
        ImageJpeg ($newimg,$this->dstimg);
      }
    }
    else
    //不裁图
    {
      if($ratio>=$resize_ratio)
      {
        $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
        imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
        ImageJpeg ($newimg,$this->dstimg);
      }
      if($ratio<$resize_ratio)
      {
        $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
        imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
        ImageJpeg ($newimg,$this->dstimg);
      }
    }
  }
  //初始化图象
  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);
    }
  }
  //图象目标地址
  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;
 
 
//echo $this->dstimg;
  }
}
?>
PHP 相关文章推荐
我的论坛源代码(八)
Oct 09 PHP
用PHP实现多级树型菜单
Oct 09 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(六)
Jun 23 PHP
php中的字符编码转换函数用法示例
Oct 20 PHP
PHP检测字符串是否为UTF8编码的常用方法
Nov 21 PHP
php获得文件大小和文件创建时间的方法
Mar 13 PHP
PHP实现查询两个数组中不同元素的方法
Feb 23 PHP
Laravel如何使用数据库事务及捕获事务失败后的异常详解
Oct 23 PHP
使用 laravel sms 构建短信验证码发送校验功能
Nov 06 PHP
PHPUnit测试私有属性和方法功能示例
Jun 12 PHP
yii框架数据库关联查询操作示例
Oct 14 PHP
如何通过PHP实现Des加密算法代码实例
May 09 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
PHP生成压缩文件实例
Feb 07 #PHP
You might like
使用php显示搜索引擎来的关键词
2014/02/13 PHP
php+mysqli实现批量替换数据库表前缀的方法
2014/12/29 PHP
php将图片文件转换成二进制输出的方法
2015/06/10 PHP
PHP获取Exif缩略图的方法
2015/07/13 PHP
学习php设计模式 php实现命令模式(command)
2015/12/08 PHP
基于Laravel实现的用户动态模块开发
2017/09/21 PHP
Windows平台PHP+IECapt实现网页批量截图并创建缩略图功能详解
2019/08/02 PHP
jQuery 事件队列调整方法
2009/09/18 Javascript
JQuery中操作Css样式的方法
2014/02/12 Javascript
javascript读取Xml文件做一个二级联动菜单示例
2014/03/17 Javascript
jQuery创建DOM元素实例解析
2015/01/19 Javascript
原生JS实现图片轮播切换效果
2016/12/15 Javascript
原生JS实现轮播图效果
2018/10/12 Javascript
在vue中使用G2图表的示例代码
2019/03/19 Javascript
微信小程序云开发实现云数据库读写权限
2019/05/17 Javascript
解决vue请求接口第一次成功,第二次失败问题
2020/09/08 Javascript
[02:36]DOTA2混沌骑士 英雄基础教程
2013/11/26 DOTA
在Django框架中伪造捕捉到的URLconf值的方法
2015/07/18 Python
使用Python的Flask框架来搭建第一个Web应用程序
2016/06/04 Python
微信跳一跳自动运行python脚本
2018/01/08 Python
Python 实现12306登录功能实例代码
2018/02/09 Python
Python操作SQLite数据库过程解析
2019/09/02 Python
Python制作词云图代码实例
2019/09/09 Python
Pytorch 计算误判率,计算准确率,计算召回率的例子
2020/01/18 Python
python中有帮助函数吗
2020/06/19 Python
python中scipy.stats产生随机数实例讲解
2021/02/19 Python
Madewell美德威尔美国官网:美国休闲服饰品牌
2016/11/25 全球购物
全球性的奢侈品梦工厂:Forzieri(福喜利)
2019/02/20 全球购物
介绍一下HDLC(High-Level Data Link Control)高层数据链路协议
2012/01/21 面试题
分公司经理岗位职责
2013/11/11 职场文书
党课培训主持词
2014/04/01 职场文书
商务宴请邀请函范文
2015/02/02 职场文书
python中的plt.cm.Paired用法说明
2021/05/31 Python
JavaWeb Servlet实现网页登录功能
2021/07/04 Java/Android
使用CSS实现一个搜索引擎的原理解析
2021/09/25 HTML / CSS
Hive常用日期格式转换语法
2022/06/25 数据库