基于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.NET的入门教程
Oct 09 PHP
PHP Ajax中文乱码问题解决方法
Feb 27 PHP
php SQL之where语句生成器
Mar 24 PHP
php中函数的形参与实参的问题说明
Sep 01 PHP
简单的PHP多图上传小程序代码
Jul 17 PHP
php通过文件流方式复制文件的方法
Mar 13 PHP
PHP的时间戳与具体时间转化的简单实现
Jun 13 PHP
php 的反射详解及示例代码
Aug 25 PHP
Zend Framework上传文件重命名的实现方法
Nov 25 PHP
DWZ+ThinkPHP开发时遇到的问题分析
Dec 12 PHP
thinkphp中U方法按路由规则生成url的方法
Mar 12 PHP
Laravel框架Eloquent ORM修改数据操作示例
Dec 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
长波知识介绍
2021/03/01 无线电
php 表单提交大量数据发生丢失的解决方法
2014/03/03 PHP
遍历指定目录,并存储目录内所有文件属性信息的php代码
2016/10/28 PHP
thinkPHP分页功能实例详解
2017/05/05 PHP
laravel实现分页样式替换示例代码(增加首、尾页)
2017/09/22 PHP
JavaScript 学习笔记(十四) 正则表达式
2010/01/22 Javascript
使用jQuery避免鼠标双击的解决方案
2013/08/21 Javascript
js跑步算法的实现代码
2013/12/04 Javascript
form.submit()不能提交表单的错误原因及解决方法
2014/10/13 Javascript
JS实现从表格中动态删除指定行的方法
2015/03/31 Javascript
javascript实现检验的各种规则
2015/07/31 Javascript
基于JQuery实现图片上传预览与删除操作
2016/05/24 Javascript
JS基础随笔(菜鸟必看篇)
2016/07/13 Javascript
JS之获取样式的简单实现方法(推荐)
2016/09/13 Javascript
jQuery图片轮播实现并封装(一)
2016/12/03 Javascript
JS简单实现获取元素的封装操作示例
2017/04/07 Javascript
Angular ng-animate和ng-cookies用法详解
2018/04/18 Javascript
vue+springmvc导出excel数据的实现代码
2018/06/27 Javascript
Vuex的初探与实战小结
2018/11/26 Javascript
JS/jQuery实现简单的开关灯效果【案例】
2019/02/19 jQuery
vue项目打包上传github并制作预览链接(pages)
2019/04/19 Javascript
小程序使用分包的示例代码
2020/03/23 Javascript
原生js canvas实现鼠标跟随效果
2020/08/02 Javascript
vue内置组件keep-alive事件动态缓存实例
2020/10/30 Javascript
python将图片文件转换成base64编码的方法
2015/03/14 Python
python中使用 xlwt 操作excel的常见方法与问题
2019/01/13 Python
基于PyTorch的permute和reshape/view的区别介绍
2020/06/18 Python
python 抓取知乎指定回答下视频的方法
2020/07/09 Python
JAVA程序员自荐书
2014/01/30 职场文书
微电影大赛策划方案
2014/06/05 职场文书
终止或解除劳动合同及劳动关系的证明书
2014/10/06 职场文书
环卫工作个人总结
2015/03/04 职场文书
2015年老干部工作总结
2015/04/23 职场文书
办公用品质量保证书
2015/05/11 职场文书
预备党员转正意见
2015/06/01 职场文书
创业计划书之烤红薯
2019/09/26 职场文书