PHP生成等比缩略图类和自定义函数分享


Posted in PHP onJune 25, 2014

共有两种等比例缩略图方法可以借鉴
一、为类文件,实例化之后即可使用
二、为自定义方法,比较轻巧

类文件

$resizeimage = new resizeimage("./shawn.jpg", "200", "100", "0","../pic/shawnsun.jpg");

//实例化下面的类,就能生成缩略图

//其中,源文件和缩略图地址可以相同,200,100分别代表宽和高,第四个参数为可选 0不截图,1为截图

<?php

class resizeimage{

 

    //图片类型

    public $type;

    //实际宽度

    public $width;

    //实际高度

    public $height;

    //改变后的宽度

    public $resize_width;

    //改变后的高度

    public $resize_height;

    //是否裁图

    public $cut;

    //源图象

    public $srcimg;

    //目标图象地址

    public $dstimg;

    //临时创建的图象

    public $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);

          //W & H

          $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;

    }

}

 

?>

自定义方法

thumbs('shawn.jpg','shawnsun.jpg',100,100);

//参数属性类似于方法一
<?php

 

function thumbs($FileName,$SaveTo,$SetW,$SetH){

    $IMGInfo= getimagesize($FileName);

    if(!$IMGInfo) return false;

         

    if($IMGInfo['mime']== "image/pjpeg" || $IMGInfo['mime']=="image/jpeg"){

        $ThisPhoto= imagecreatefromjpeg($FileName);

    }elseif($IMGInfo['mime']== "image/x-png" || $IMGInfo['mime']== "image/png"){

        $ThisPhoto= imagecreatefrompng($FileName);   

    }elseif($IMGInfo['mime']== "image/gif"){

        $ThisPhoto=imagecreatefromgif($FileName); 

    } 

     

    $width=$IMGInfo[0];

    $height=$IMGInfo[1];   

    $scalc = max($width/$SetW,$height/$SetH);

    $nw = intval($width/$scalc);

    $nh = intval($height/$scalc);

    echo "缩略大小:w$nw,h$nh <br /-->";

     

    if($SetW-$nw == 1){$nw = $SetW;}

    if($SetH-$nh == 1){$nh = $SetH;}

    echo "+修正1像素: w$nw,h$nh<br>";

     

    //补宽

    if($SetW-$nw > 0){

        $nh = $nh +(($nh/$nw) * ($SetW-$nw));

        echo "*需补宽".($SetW-$nw).",陪补高".(($nh/$nw) * ($SetW-$nw))."  <br>";  

        $nw = $SetW;

    }

    //补高

    if($SetH-$nh > 0){

        $nw = $nw + (($nw/$nh) * ($SetH-$nh));

        echo "*需补高".($SetH-$nh).",陪补宽". (($nw/$nh) * ($SetH-$nh)) ."<br>";

        $nh = $SetH;

    }

    $nw = intval($nw);

    $nh = intval($nh);

    echo "+修正大小:w$nw,h$nh<br>";

     

    $px = ($SetW - $nw)/2;

    $py = ($SetH - $nh)/2;

    echo "窗口大小:w$SetW,h$SetH <br>";

    echo "+偏移修正:x$px,y$py <br>";

     

    $NewPhoto=imagecreatetruecolor($SetW,$SetH);

    imagecopyresized($NewPhoto,$ThisPhoto,$px,$py,0,0,$nw,$nh,$width,$height);

    ImageJpeg ($NewPhoto,$SaveTo);

    return true;

}

     

?>
PHP 相关文章推荐
一个从别的网站抓取信息的例子(域名查询)
Oct 09 PHP
php array_slice函数的使用以及参数详解
Aug 30 PHP
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
May 08 PHP
php采用ajax数据提交post与post常见方法总结
Nov 10 PHP
php实现refresh刷新页面批量导入数据的方法
Dec 23 PHP
PHP 常用的header头部定义汇总
Jun 19 PHP
PHP常用的排序和查找算法
Aug 06 PHP
php修改数组键名的方法示例
Apr 15 PHP
PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别分析
Jul 03 PHP
购物车实现的几种方式优缺点对比
May 02 PHP
Laravel 读取 config 下的数据方法
Oct 13 PHP
PHP命令行与定时任务
Apr 01 PHP
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
Jun 25 #PHP
PHP内置过滤器FILTER使用实例
Jun 25 #PHP
PHP生成图片验证码、点击切换实例
Jun 25 #PHP
PHP生成随机密码类分享
Jun 25 #PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十二)
Jun 25 #PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
Jun 25 #PHP
JavaScript创建命名空间的5种写法
Jun 24 #PHP
You might like
PHP中查询SQL Server或Sybase时TEXT字段被截断的解决方法
2009/03/10 PHP
初品cakephp 入门基础
2012/02/16 PHP
thinkPHP中create方法与令牌验证实例浅析
2015/12/08 PHP
Zend Framework框架路由机制代码分析
2016/03/22 PHP
Yii2框架制作RESTful风格的API快速入门教程
2016/11/08 PHP
清华大学出版的事半功倍系列 javascript全部源代码
2007/05/04 Javascript
IE无法设置短域名下Cookie
2010/09/23 Javascript
JQuery获取当前屏幕的高度宽度的实现代码
2011/07/12 Javascript
jQuery 复合选择器应用的几个例子
2014/09/11 Javascript
jquery操作 iframe的方法
2014/12/03 Javascript
jquery+css实现动感的图片切换效果
2015/11/25 Javascript
详解JS正则replace的使用方法
2016/03/06 Javascript
深入理解js promise chain
2016/05/05 Javascript
AngularJS 单元测试(一)详解
2016/09/21 Javascript
AngularJS使用angular.bootstrap完成模块手动加载的方法分析
2017/01/19 Javascript
老生常谈angularjs中的$state.go
2017/04/24 Javascript
vuex actions传递多参数的处理方法
2018/09/18 Javascript
VUE脚手架具体使用方法
2019/05/20 Javascript
微信小程序吸底区域适配iPhoneX的实现
2020/04/09 Javascript
echarts柱状图背景重叠组合而非并列的实现代码
2020/12/10 Javascript
python中OrderedDict的使用方法详解
2017/05/05 Python
python TKinter获取文本框内容的方法
2018/10/11 Python
anaconda如何查看并管理python环境
2019/07/05 Python
python使用socket实现的传输demo示例【基于TCP协议】
2019/09/24 Python
在PyCharm中遇到pip安装 失败问题及解决方案(pip失效时的解决方案)
2020/03/10 Python
HTML5 embed 标签使用方法介绍
2013/08/13 HTML / CSS
互动出版网:专业书籍
2017/03/21 全球购物
印度在线购物网站:Paytmmall
2019/07/24 全球购物
编程用JAVA解析XML的方式
2013/07/07 面试题
自考生自我评价分享
2014/01/18 职场文书
餐厅经理岗位职责和岗位目标
2014/02/13 职场文书
材料专业毕业生求职信
2014/02/26 职场文书
道歉的话语大全
2015/05/12 职场文书
离婚纠纷代理词
2015/05/23 职场文书
高三物理教学反思
2016/02/20 职场文书
Html5调用企业微信的实现
2021/04/16 HTML / CSS