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 相关文章推荐
深入探讨PHP中的内存管理问题
Aug 31 PHP
yii框架表单模型使用及以数组形式提交表单数据示例
Apr 30 PHP
php中curl和file_get_content的区别
May 10 PHP
教你如何解密 “ PHP 神盾解密工具 ”
Jun 20 PHP
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)
Nov 08 PHP
php获取英文姓名首字母的方法
Jul 13 PHP
thinkPHP框架可添加js事件的分页类customPage.class.php完整实例
Mar 16 PHP
yii2使用gridView实现下拉列表筛选数据
Apr 10 PHP
PHP实现的DES加密解密封装类完整实例
Apr 29 PHP
PHP读取CSV大文件导入数据库的实例
Jul 24 PHP
php如何把表单内容提交到数据库
Jul 08 PHP
详解使用php-cs-fixer格式化代码
Sep 16 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
PHP4引用文件语句的对比
2006/10/09 PHP
php页面跳转代码 输入网址跳转到你定义的页面
2013/03/28 PHP
Mysql中分页查询的两个解决方法比较
2013/05/02 PHP
PHP autoload与spl_autoload自动加载机制的深入理解
2013/06/05 PHP
dedecms中使用php语句指南
2014/11/13 PHP
js中判断文本框是否为空的两种方法
2011/07/31 Javascript
20个非常棒的 jQuery 幻灯片插件和教程分享
2011/08/23 Javascript
js函数获取html中className所在的内容并去除标签
2013/09/08 Javascript
js 采用delete实现继承示例代码
2014/05/20 Javascript
基于jQuery实现网页进度显示插件
2015/03/04 Javascript
jQuery添加删除DOM元素方法详解
2016/01/18 Javascript
原生JS查找元素的方法(推荐)
2016/11/22 Javascript
Angular.JS判断复选框checkbox是否选中并实时显示
2016/11/30 Javascript
Bootstrap 3 按钮标签实例代码
2017/02/21 Javascript
Vue中添加过渡效果的方法
2017/03/16 Javascript
在React 组件中使用Echarts的示例代码
2017/11/08 Javascript
关于vue中的ajax请求和axios包问题
2018/04/19 Javascript
简单了解常用的JavaScript 库
2020/07/16 Javascript
Element-ui树形控件el-tree自定义增删改和局部刷新及懒加载操作
2020/08/31 Javascript
python使用线程封装的一个简单定时器类实例
2015/05/16 Python
详谈Python高阶函数与函数装饰器(推荐)
2017/09/30 Python
Python实现的圆形绘制(画圆)示例
2018/01/31 Python
pandas求两个表格不相交的集合方法
2018/12/08 Python
python 批量解压压缩文件的实例代码
2019/06/27 Python
Python:type、object、class与内置类型实例
2019/12/25 Python
Foot Locker德国官方网站:美国运动服和鞋类零售商
2018/11/01 全球购物
办公室文秘自我评价
2013/09/21 职场文书
自我鉴定怎么写
2013/12/05 职场文书
毕业生个人求职信范例分享
2013/12/17 职场文书
电钳专业个人求职信
2014/01/04 职场文书
小学运动会口号
2014/06/07 职场文书
故意伤害人身损害赔偿协议书
2014/11/19 职场文书
个人总结与自我评价
2015/02/14 职场文书
南京大屠杀观后感
2015/06/02 职场文书
学会Python数据可视化必须尝试这7个库
2021/06/16 Python
数据设计之权限的实现
2022/08/05 MySQL