基于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
域名和cookie问题(域名后缀)
Oct 10 PHP
PHP运行环境配置与开发环境的配置(图文教程)
Jun 04 PHP
PHP入门之常量简介和系统常量
May 12 PHP
php mb_substr()函数截取中文字符串应用示例
Jul 29 PHP
PHP获取数组最大值下标的方法
May 12 PHP
深入解析PHP中foreach语句控制数组循环的用法
Nov 30 PHP
关于PHP内置的字符串处理函数详解
Feb 04 PHP
关于php几种字符串连接的效率比较(详解)
Feb 22 PHP
php获取数据库中数据的实现方法
Jun 01 PHP
PHP实现将多个文件中的内容合并为新文件的方法示例
Jun 10 PHP
PHP5.0~5.6 各版本兼容性cURL文件上传功能实例分析
May 11 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制作静态网站的模板框架(四)
2006/10/09 PHP
PHP 一个比较完善的简单文件上传
2010/03/25 PHP
使用PHP会话(Session)实现用户登陆功能
2013/06/29 PHP
PHP实现将优酷土豆腾讯视频html地址转换成flash swf地址的方法
2017/08/04 PHP
jQuery找出网页上最高元素的方法
2015/03/20 Javascript
jquery实现从数组移除指定的值
2015/06/24 Javascript
JS实现自动固定顶部的悬浮菜单栏效果
2015/09/16 Javascript
轻松学习jQuery插件EasyUI EasyUI创建CRUD应用
2015/11/30 Javascript
使用jQuery中的wrap()函数操作HTML元素的教程
2016/05/24 Javascript
javascript正则表达式之分组概念与用法实例
2016/06/16 Javascript
关于javascript事件响应的基础语法总结(必看篇)
2016/12/26 Javascript
js读取json文件片段中的数据实例
2017/03/09 Javascript
jQuery表格(Table)基本操作实例分析
2017/03/10 Javascript
强大的 Angular 表单验证功能详细介绍
2017/05/23 Javascript
NodeJS链接MySql数据库的操作方法
2017/06/27 NodeJs
jquery实现下拉菜单的手风琴效果
2017/07/23 jQuery
js防抖和节流的深入讲解
2018/12/06 Javascript
小程序云函数调用API接口的方法
2019/05/17 Javascript
使用layer弹窗提交表单时判断表单是否输入为空的例子
2019/09/26 Javascript
使用Element的InfiniteScroll 无限滚动组件报错的解决
2020/07/27 Javascript
Python实现的Kmeans++算法实例
2014/04/26 Python
轻松掌握python设计模式之访问者模式
2016/11/18 Python
python字典值排序并取出前n个key值的方法
2018/10/17 Python
Python一句代码实现找出所有水仙花数的方法
2018/11/13 Python
django 利用Q对象与F对象进行查询的实现
2020/05/15 Python
Python爬虫之Selenium库的使用方法
2021/01/03 Python
HTML5 Canvas实现平移/放缩/旋转deom示例(附截图)
2013/07/04 HTML / CSS
Bravofly德国:预订廉价航班和酒店
2019/09/22 全球购物
高三语文教学反思
2014/01/15 职场文书
文秘档案管理岗位职责
2014/03/06 职场文书
房屋买卖协议书范本
2014/04/10 职场文书
春季防火方案
2014/05/10 职场文书
高中综合实践活动总结
2014/07/07 职场文书
2014年学校体育工作总结
2014/12/08 职场文书
杨善洲观后感
2015/06/04 职场文书
Mysql查询时间区间日期列表,不会由于数据表数据影响
2022/04/19 MySQL