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 相关文章推荐
别人整理的服务器变量:$_SERVER
Oct 20 PHP
PHP4 与 MySQL 数据库操作函数详解
Dec 06 PHP
有关JSON以及JSON在PHP中的应用
Apr 09 PHP
PHP取二进制文件头快速判断文件类型的实现代码
Aug 05 PHP
ThinkPHP中自定义错误页面和提示页面实例
Nov 22 PHP
php树型类实例
Dec 05 PHP
PHP和MySql中32位和64位的整形范围是多少
Feb 18 PHP
thinkphp自带验证码全面解析
Sep 18 PHP
PHPStrom 新建FTP项目以及在线操作教程
Oct 16 PHP
PHP输出图像imagegif、imagejpeg与imagepng函数用法分析
Nov 14 PHP
PHP验证码类ValidateCode解析
Jan 07 PHP
php计数排序算法的实现代码(附四个实例代码)
Mar 31 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 array_merge函数
2014/08/31 PHP
对laravel的session获取与存取方法详解
2019/10/08 PHP
两个Javascript小tip资料
2010/11/23 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(一)让静态人物动起来
2013/01/23 Javascript
JS随机生成不重复数据的实例方法
2013/07/17 Javascript
jquery与prototype框架的详细对比
2013/11/21 Javascript
jquery动态改变form属性提交表单
2014/06/03 Javascript
JS根据年月获得当月天数的实现代码
2014/07/03 Javascript
jQuery()方法的第二个参数详解
2015/04/29 Javascript
javascript实现table表格隔行变色的方法
2015/05/13 Javascript
javascript给span标签赋值的方法
2015/11/26 Javascript
你所未知的3种Node.js代码优化方式
2016/02/25 Javascript
AngularJS 应用身份认证的技巧总结
2016/11/07 Javascript
一种angular的方法级的缓存注解(装饰器)
2018/03/13 Javascript
详解jquery和vue对比
2019/04/16 jQuery
微信小游戏之使用three.js 绘制一个旋转的三角形
2019/06/10 Javascript
[03:51]吞吞映像 每周精彩击杀top10第二弹
2014/06/25 DOTA
Python函数式编程指南(二):从函数开始
2015/06/24 Python
读取json格式为DataFrame(可转为.csv)的实例讲解
2018/06/05 Python
python pygame模块编写飞机大战
2018/11/20 Python
华为2019校招笔试题之处理字符串(python版)
2019/06/25 Python
详解python调用cmd命令三种方法
2019/07/08 Python
python3安装crypto出错及解决方法
2019/07/30 Python
Python接口测试环境搭建过程详解
2020/06/29 Python
选购国际女性时装设计师品牌:IFCHIC(支持中文)
2018/04/12 全球购物
KELLER SPORTS荷兰:在线订购最好的运动产品
2020/10/13 全球购物
客服端调用EJB对象的几个基本步骤
2012/01/15 面试题
公司会议策划方案
2014/05/17 职场文书
基层党员群众路线整改措施及努力方向
2014/10/28 职场文书
丧事答谢词
2015/01/05 职场文书
英语复习计划
2015/01/19 职场文书
六一文艺汇演开幕词
2015/01/29 职场文书
永不妥协观后感
2015/06/10 职场文书
培训计划通知
2015/07/15 职场文书
担保公司2015年终工作总结
2015/10/14 职场文书
导游词之临安白水涧
2019/11/05 职场文书