基于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 相关文章推荐
5.PHP的其他功能
Oct 09 PHP
php getsiteurl()函数
Sep 05 PHP
PHP 简单日历实现代码
Oct 28 PHP
PHP基础学习小结
Apr 17 PHP
PHP中基于ts与nts版本- vc6和vc9编译版本的区别详解
Apr 26 PHP
PHP在引号前面添加反斜杠(PHP去除反斜杠)
Sep 28 PHP
Yii使用CLinkPager分页实例详解
Jul 23 PHP
PHP中Fatal error session_start()错误解决步骤
Aug 05 PHP
php实现的CSS更新类实例
Sep 22 PHP
php Session无效分析资料整理
Nov 29 PHP
laravel中命名路由的使用方法
Feb 24 PHP
ThinkPHP 3.2.3实现页面静态化功能的方法详解
Aug 03 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文章内容分页并生成相应的htm静态页面代码
2010/06/07 PHP
Yii框架调试心得--在页面输出执行sql语句
2014/12/25 PHP
php实现的日历程序
2015/06/18 PHP
phpinfo() 中 Local Value(局部变量)Master Value(主变量) 的区别
2016/02/03 PHP
PHP实现超简单的SSL加密解密、验证及签名的方法示例
2017/08/28 PHP
php框架CodeIgniter使用redis的方法分析
2018/04/13 PHP
JavaScript DOM学习第一章 W3C DOM简介
2010/02/19 Javascript
读JavaScript DOM编程艺术笔记
2011/11/15 Javascript
jQuery获得页面元素的绝对/相对位置即绝对X,Y坐标
2014/03/06 Javascript
jquery搜索框效果实现方法
2015/01/16 Javascript
详解Vue-cli代理解决跨域问题
2017/09/27 Javascript
[00:33]2016完美“圣”典风云人物:Sccc宣传片
2016/12/03 DOTA
wxPython学习之主框架实例
2014/09/28 Python
Python异常学习笔记
2015/02/03 Python
django_orm查询性能优化方法
2018/08/20 Python
python pygame实现方向键控制小球
2019/05/17 Python
Python爬虫图片懒加载技术 selenium和PhantomJS解析
2019/09/18 Python
Ubuntu下Python+Flask分分钟搭建自己的服务器教程
2019/11/19 Python
python通用读取vcf文件的类(复制粘贴即可用)
2020/02/29 Python
pandas创建DataFrame的7种方法小结
2020/06/14 Python
pycharm 的Structure界面设置操作
2021/02/05 Python
HTML5本地存储和本地数据库实例详解
2017/09/05 HTML / CSS
亚洲最大的眼镜批发商和零售商之一:Glasseslit
2018/10/08 全球购物
什么是属性访问器
2015/10/26 面试题
幸福家庭事迹材料
2014/02/03 职场文书
爱与责任师德演讲稿
2014/08/26 职场文书
党政领导班子四风问题对照检查材料思想汇报
2014/10/02 职场文书
2014年党员整改措施
2014/10/24 职场文书
2014村党支部书记党建工作汇报材料
2014/11/02 职场文书
2015年医院护理部工作总结
2015/04/23 职场文书
小学生教师节广播稿
2015/08/19 职场文书
青年文明号创建口号大全
2015/12/25 职场文书
详解MongoDB的条件查询和排序
2021/06/23 MongoDB
详解Python中*args和**kwargs的使用
2022/04/07 Python
Oracle锁表解决方法的详细记录
2022/06/05 Oracle
win10此电脑打不开怎么办 win10双击此电脑无响应的解决办法
2022/07/23 数码科技