基于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 相关文章推荐
PHP新手上路(三)
Oct 09 PHP
PHP开启gzip页面压缩实例代码
Mar 11 PHP
PHP Warning: PHP Startup: Unable to load dynamic library \ D:/php5/ext/php_mysqli.dll\
Jun 17 PHP
PHP常用开发函数解析之数组篇[未完结]
Jul 30 PHP
php中使用临时表查询数据的一个例子
Feb 03 PHP
ThinkPHP3.1新特性之动态设置自动完成和自动验证示例
Jun 19 PHP
php实现阿拉伯数字和罗马数字相互转换的方法
Apr 17 PHP
PHP异常处理浅析
May 12 PHP
Linux(CentOS)下PHP扩展PDO编译安装的方法
Apr 07 PHP
PHP简单预防sql注入的方法
Sep 27 PHP
PHP操作MongoDB实现增删改查功能【附php7操作MongoDB方法】
Apr 24 PHP
关于Yii中模型场景的一些简单介绍
Sep 22 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
cache_lite试用
2007/02/14 PHP
php中选择什么接口(mysql、mysqli)访问mysql
2013/02/06 PHP
ThinkPHP中html:list标签用法分析
2016/01/09 PHP
推荐40个非常优秀的jQuery插件和教程【系列三】
2011/11/09 Javascript
Ajax搜索结果页面下方的分页按钮的生成
2012/04/05 Javascript
IE浏览器不支持getElementsByClassName的解决方法
2014/08/27 Javascript
Json实现异步请求提交评论无需跳转其他页面
2014/10/11 Javascript
JavaScript中标识符提升问题
2015/06/11 Javascript
jquery操作angularjs对象
2015/06/26 Javascript
jQuery实现图片轮播特效代码分享
2015/09/15 Javascript
JS实现的跨浏览器解析XML文件实例
2016/06/21 Javascript
JavaScript Ajax编程 应用篇
2016/07/02 Javascript
IOS中safari下的select下拉菜单文字过长不换行的解决方法
2016/09/26 Javascript
AngularJS的依赖注入实例分析(使用module和injector)
2017/01/19 Javascript
angular实现表单验证及提交功能
2017/02/01 Javascript
Vue2.0父组件与子组件之间的事件发射与接收实例代码
2017/09/19 Javascript
vue2.0 路由模式mode=&quot;history&quot;的作用
2018/10/18 Javascript
iview通过Dropdown(下拉菜单)实现的右键菜单
2018/10/26 Javascript
Python实现可设置持续运行时间、线程数及时间间隔的多线程异步post请求功能
2018/01/11 Python
对numpy中布尔型数组的处理方法详解
2018/04/17 Python
使用APScheduler3.0.1 实现定时任务的方法
2019/07/22 Python
html5文本内容_动力节点Java学院整理
2017/07/11 HTML / CSS
美国隐形眼镜销售网站:ContactsDirect
2017/10/28 全球购物
美国折扣香水网站:The Perfume Spot
2020/12/12 全球购物
商务英语应届生自我鉴定
2013/12/08 职场文书
2014婚礼司仪主持词
2014/03/14 职场文书
科学发展观标语
2014/10/08 职场文书
教师个人事迹材料
2014/12/17 职场文书
美术教师个人工作总结
2015/02/06 职场文书
党支部书记岗位职责
2015/02/15 职场文书
2016年中秋祝酒词
2015/11/26 职场文书
2019年“红色之旅”心得体会1000字(3篇)
2019/09/27 职场文书
导游词之云南省玉龙雪山
2019/12/19 职场文书
CSS布局之浮动(float)和定位(position)属性的区别
2021/09/25 HTML / CSS
python神经网络学习 使用Keras进行回归运算
2022/05/04 Python
python中 Flask Web 表单的使用方法
2022/05/20 Python