基于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 相关文章推荐
第八节 访问方式 [8]
Oct 09 PHP
一个用php实现的获取URL信息的类
Jan 02 PHP
在PHP中养成7个面向对象的好习惯
Jan 28 PHP
php处理复杂xml数据示例
Jul 11 PHP
Zend Framework使用Zend_Loader组件动态加载文件和类用法详解
Dec 09 PHP
ThinkPHP实现静态缓存和动态缓存示例代码
May 02 PHP
PHP/ThinkPHP实现批量打包下载文件的方法示例
Jul 31 PHP
thinkPHP5项目中实现QQ第三方登录功能
Oct 20 PHP
Laravel框架实现定时发布任务的方法
Aug 16 PHP
php遍历目录下文件并按修改时间排序操作示例
Jul 12 PHP
laravel5.6 框架操作数据 Eloquent ORM用法示例
Jan 26 PHP
PHP设计模式(七)组合模式Composite实例详解【结构型】
May 02 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
可快速识别放射性物质-国外大神教你diy一个开放式辐射探测器
2020/03/12 无线电
php中模拟POST传递数据的两种方法分享
2011/09/16 PHP
2款PHP无限级分类实例代码
2015/11/11 PHP
php伪静态验证码不显示的解决方案
2019/09/26 PHP
IE 缓存策略的BUG的解决方法
2007/07/21 Javascript
浅谈jQuery事件绑定原理
2015/01/02 Javascript
javascript下拉列表菜单的实现方法
2015/11/18 Javascript
jQuery Validate表单验证入门学习
2015/12/18 Javascript
基于javascript实现窗口抖动效果
2016/01/03 Javascript
全面解析DOM操作和jQuery实现选项移动操作代码分享
2016/06/07 Javascript
JS实现对中文字符串进行utf-8的Base64编码的方法(使其与Java编码相同)
2016/06/21 Javascript
JavaScript实现256色转灰度图
2017/02/22 Javascript
详解HTTPS 的原理和 NodeJS 的实现
2017/07/04 NodeJs
Vue项目中quill-editor带样式编辑器的使用方法
2017/08/08 Javascript
Webpack 服务器端代码打包的示例代码
2017/09/19 Javascript
Vue之Vue.set动态新增对象属性方法
2018/02/23 Javascript
微信小程序使用gitee进行版本管理
2018/09/20 Javascript
vue + any-touch实现一个iscroll 实现拖拽和滑动动画效果
2019/04/08 Javascript
浅谈bootstrap layer.open中end的使用方法
2019/09/12 Javascript
js通过循环多张图片实现动画效果
2019/12/19 Javascript
jQuery HTML css()方法与css类实例详解
2020/05/20 jQuery
vue等两个接口都返回结果再执行下一步的实例
2020/09/08 Javascript
Go语言基于Socket编写服务器端与客户端通信的实例
2016/02/19 Python
python测试mysql写入性能完整实例
2018/01/18 Python
python使用Tkinter实现在线音乐播放器
2018/01/30 Python
Python创建普通菜单示例【基于win32ui模块】
2018/05/09 Python
Win10下Python3.7.3安装教程图解
2019/07/08 Python
html5 canvas简单封装一个echarts实现不了的饼图
2018/06/12 HTML / CSS
如何设定的weblogic的热启动模式(开发模式)与产品发布模式
2012/09/08 面试题
你经历的项目中的SCM配置项主要有哪些?什么是配置项?
2013/11/04 面试题
土木工程专业推荐信
2014/02/19 职场文书
中学生家长评语大全
2014/04/16 职场文书
企业党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
信访工作汇报材料
2014/10/27 职场文书
2016年大学生暑期社会实践方案
2015/11/26 职场文书
详解缓存穿透击穿雪崩解决方案
2021/05/28 Redis