基于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中使用模板的方法
May 24 PHP
php的POSIX 函数以及进程测试的深入分析
Jun 03 PHP
PHP 利用Mail_MimeDecode类提取邮件信息示例
Jan 26 PHP
Java中final关键字详解
Aug 10 PHP
Smarty保留变量用法分析
May 23 PHP
php基于mcrypt_encrypt和mcrypt_decrypt实现字符串加密解密的方法
Jul 12 PHP
PHP实现的同步推荐操作API接口案例分析
Nov 30 PHP
PHP简单实现模拟登陆功能示例
Sep 15 PHP
PHP上传文件及图片到七牛的方法
Jul 25 PHP
phpMyAdmin通过密码漏洞留后门文件
Nov 20 PHP
php常用经典函数集锦【数组、字符串、栈、队列、排序等】
Aug 23 PHP
解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]
Apr 06 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 简单数组排序实现代码
2009/08/05 PHP
PHP扩展迁移为PHP7扩展兼容性问题记录
2016/02/15 PHP
php中namespace及use用法分析
2016/12/06 PHP
[原创]PHP实现生成vcf vcard文件功能类定义与使用方法详解【附demo源码下载】
2017/09/02 PHP
PHP基于curl post实现发送url及相关中文乱码问题解决方法
2017/11/25 PHP
php实现JWT(json web token)鉴权实例详解
2019/11/05 PHP
jquery 全局AJAX事件使用代码
2010/11/05 Javascript
range 标准化之获取
2011/08/28 Javascript
jQuery function的正确书写方法
2013/08/02 Javascript
用js判断输入是否为中文的函数
2014/03/10 Javascript
jQuery简单实现仿京东商城的左侧菜单效果代码
2015/09/09 Javascript
Bootstrap按钮下拉菜单组件详解
2016/05/10 Javascript
Javascript中return的使用与闭包详解
2017/01/11 Javascript
vue中v-cloak解决刷新或者加载出现闪烁问题(显示变量)
2018/04/20 Javascript
Bootstarp在pycharm中的安装及简单的使用方法
2019/04/19 Javascript
React实现轮播效果
2020/08/25 Javascript
[10:07]2014DOTA2国际邀请赛 实拍选手现场观战DK对阵Titan
2014/07/12 DOTA
[00:43]拉比克至宝魔导师密钥展示
2018/12/20 DOTA
django接入新浪微博OAuth的方法
2015/06/29 Python
解决tensorflow1.x版本加载saver.restore目录报错的问题
2018/07/26 Python
Python清空文件并替换内容的实例
2018/10/22 Python
Pytorch实现将模型的所有参数的梯度清0
2020/06/24 Python
法国隐形眼镜网站:VisionDirect.fr
2020/03/03 全球购物
说出ArrayList,Vector, LinkedList的存储性能和特性
2015/01/04 面试题
应届生如何写自荐信
2014/01/05 职场文书
会计顶岗实习心得
2014/01/25 职场文书
大学生党员承诺书
2014/05/20 职场文书
应届生求职信范文
2014/06/30 职场文书
创先争优活动承诺书
2014/08/30 职场文书
2014最新预备党员思想汇报范文:中国梦,我的梦
2014/10/25 职场文书
2015年组织部工作总结
2015/04/03 职场文书
2015年建筑工作总结报告
2015/05/04 职场文书
2019年员工旷工保证书!
2019/06/28 职场文书
MySQL中VARCHAR与CHAR格式数据的区别
2021/05/26 MySQL
MySQL分库分表详情
2021/09/25 MySQL
JavaScript组合继承详解
2021/11/07 Javascript