基于GD2图形库的PHP生成图片缩略图类代码分享

 更新时间:2015年02月08日 14:26:47   投稿:junjie  
这篇文章主要介绍了基于GD2图形库的PHP生成图片缩略图类代码分享,本文直接给出实现代码和使用方法,需要的朋友可以参考下

要使用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实现异步数据调用的方法

    这篇文章主要介绍了php实现异步数据调用的方法,分享了4种PHP异步执行的常用方式,感兴趣的小伙伴们可以参考一下
    2015-12-12
  • 详解PHP的抽象类和抽象方法以及接口总结

    详解PHP的抽象类和抽象方法以及接口总结

    这篇文章主要介绍了PHP的抽象类和抽象方法以及接口总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • 基于php编程规范(详解)

    基于php编程规范(详解)

    下面小编就为大家带来一篇基于php编程规范(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • PHP实现简单数字分页效果

    PHP实现简单数字分页效果

    我们平时在开发中,经常需要用到分页,在项目中要用到分页。分页功能是经常使用的一个功能,下面我们就来简单分享个数字的分页效果
    2015-07-07
  • php flush无效,IIS7下php实时输出的方法

    php flush无效,IIS7下php实时输出的方法

    这篇文章主要介绍了php flush无效,IIS7下php实时输出的方法,需要的朋友可以参考下
    2016-08-08
  • PHP实现数组转JSon和JSon转数组的方法示例

    PHP实现数组转JSon和JSon转数组的方法示例

    这篇文章主要介绍了PHP实现数组转JSon和JSon转数组的方法,结合实例形式分析了php数组与json相互转换实现方法与操作技巧,需要的朋友可以参考下
    2018-06-06
  • 关于crontab的使用详解

    关于crontab的使用详解

    本篇文章是对crontab的使用进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • PHP开发不能违背的安全规则 过滤用户输入

    PHP开发不能违背的安全规则 过滤用户输入

    作为PHP程序员,特别是新手,对于互联网的险恶总是知道的太少,对于外部的入侵有很多时候是素手无策的,他们根本不知道黑客是如何入侵的、提交入侵、上传漏洞、sql 注入、跨脚本攻击等等。
    2011-05-05
  • php 从数据库提取二进制图片的处理代码

    php 从数据库提取二进制图片的处理代码

    形式上类似UCH 只是存储方式不一样 本人比较愚钝 这个问题困惑了我半天 希望对有同样问题的phper有所帮助 高手们别见笑!
    2009-09-09
  • 浅析php如何实现爬取数据原理

    浅析php如何实现爬取数据原理

    在本篇文章中,小编给大家分享了关于php如何实现爬取数据的原理知识点,有兴趣的朋友们参考下。
    2018-09-09

最新评论