www.adminn.cn
站长正能量分享网!

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

AD:阿里云服务器企业会员更优惠 腾讯云香港,韩国免备案服务器1.8折优惠

这篇文章主要介绍了PHP生成等比缩略图类和自定义函数分享,分别封装成了一个类和自定义函数,需要的朋友可以参考下。

共有两种等比例缩略图方法可以借鉴

一、为类文件,实例化之后即可使用

二、为自定义方法,比较轻巧

类文件代码如下:

  1. $resizeimage = new resizeimage("./shawn.jpg""200""100""0","../pic/shawnsun.jpg"); 
  2. //实例化下面的类,就能生成缩略图 
  3. //其中,源文件和缩略图地址可以相同,200,100分别代表宽和高,第四个参数为可选 0不截图,1为截图 
  4.  
  5. <?php 
  6. class resizeimage{ 
  7.  
  8.     //图片类型 
  9.     public $type
  10.     //实际宽度 
  11.     public $width
  12.     //实际高度 
  13.     public $height
  14.     //改变后的宽度 
  15.     public $resize_width
  16.     //改变后的高度 
  17.     public $resize_height
  18.     //是否裁图 
  19.     public $cut
  20.     //源图象 
  21.     public $srcimg
  22.     //目标图象地址 
  23.     public $dstimg
  24.     //临时创建的图象 
  25.     public $im
  26.      
  27.     function resizeimage($img$wid$hei,$c,$dstpath){ 
  28.      
  29.           $this—>srcimg = $img
  30.           $this->resize_width = $wid
  31.           $this->resize_height = $hei
  32.           $this->cut = $c
  33.      
  34.           //图片的类型 
  35.           $this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); 
  36.           //初始化图象 
  37.           $this->initi_img(); 
  38.           //目标图象地址 
  39.           $this->dst_img($dstpath); 
  40.           //W & H 
  41.           $this->width  = imagesx($this->im); 
  42.           $this->height = imagesy($this->im); 
  43.           //生成图象 
  44.           $this->newimg(); 
  45.           ImageDestroy ($this->im); 
  46.      } 
  47.      
  48.     function newimg(){ 
  49.      
  50.         //改变后的图象的比例 
  51.         $resize_ratio = ($this->resize_width)/($this->resize_height); 
  52.         //实际图象的比例 
  53.         $ratio = ($this->width)/($this->height); 
  54.          
  55.         if(($this->cut)=="1"
  56.         //裁图 
  57.         { 
  58.             if($ratio>=$resize_ratio
  59.             //高度优先 
  60.             { 
  61.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
  62.                 imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width, 
  63.                                    $this->resize_height, (($this->height)*$resize_ratio), 
  64.                                    $this->height 
  65.                 ); 
  66.                 ImageJpeg ($newimg,$this->dstimg); 
  67.             } 
  68.             if($ratio<$resize_ratio
  69.             //宽度优先 
  70.             { 
  71.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
  72.                 imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width, 
  73.                                    $this->resize_height, $this->width, 
  74.                                    (($this->width)/$resize_ratio
  75.                 ); 
  76.                 ImageJpeg ($newimg,$this->dstimg); 
  77.             } 
  78.               } 
  79.         else 
  80.         //不裁图 
  81.         { 
  82.             if($ratio>=$resize_ratio
  83.             { 
  84.                 $newimg = imagecreatetruecolor($this->resize_width, 
  85.                                                ($this->resize_width)/$ratio 
  86.                 ); 
  87.                 imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width, 
  88.                                    ($this->resize_width)/$ratio$this->width, 
  89.                                    $this->height 
  90.                 ); 
  91.                 ImageJpeg ($newimg,$this->dstimg); 
  92.             } 
  93.             if($ratio<$resize_ratio
  94.             { 
  95.                 $newimg = imagecreatetruecolor(($this->resize_height)*$ratio
  96.                                                 $this->resize_height 
  97.                 ); 
  98.                 imagecopyresampled($newimg$this->im, 0, 0, 0, 0, 
  99.                                    ($this->resize_height)*$ratio
  100.                                    $this->resize_height, $this->width, 
  101.                                    $this->height 
  102.                 ); 
  103.                 ImageJpeg ($newimg,$this->dstimg); 
  104.             } 
  105.         } 
  106.    } 
  107.      
  108.     //初始化图象 
  109.     function initi_img(){ 
  110.  
  111.         if($this->type=="jpg"
  112.         { 
  113.             $this->im = imagecreatefromjpeg($this->srcimg); 
  114.         } 
  115.         if($this->type=="gif"
  116.         { 
  117.             $this->im = imagecreatefromgif($this->srcimg); 
  118.         } 
  119.         if($this->type=="png"
  120.         { 
  121.             $this->im = imagecreatefrompng($this->srcimg); 
  122.         } 
  123.     } 
  124.     //图象目标地址 
  125.     function dst_img($dstpath){ 
  126.      
  127.         $full_length  = strlen($this->srcimg); 
  128.         $type_length  = strlen($this->type); 
  129.         $name_length  = $full_length$type_length
  130.  
  131.         $name = substr($this->srcimg,0,$name_length-1); 
  132.         $this->dstimg = $dstpath
  133.  
  134.         //echo $this->dstimg; 
  135.     } 
  136.  
  137. ?> 

自定义方法,代码如下:

  1. thumbs('shawn.jpg','shawnsun.jpg',100,100); 
  2.  
  3. <?php 
  4.  
  5. function thumbs($FileName,$SaveTo,$SetW,$SetH){ 
  6.     $IMGInfogetimagesize($FileName); 
  7.     if(!$IMGInforeturn false; 
  8.          
  9.     if($IMGInfo['mime']== "image/pjpeg" || $IMGInfo['mime']=="image/jpeg"){ 
  10.         $ThisPhoto= imagecreatefromjpeg($FileName); 
  11.     }elseif($IMGInfo['mime']== "image/x-png" || $IMGInfo['mime']== "image/png"){ 
  12.         $ThisPhoto= imagecreatefrompng($FileName);   
  13.     }elseif($IMGInfo['mime']== "image/gif"){ 
  14.         $ThisPhoto=imagecreatefromgif($FileName); 
  15.     } 
  16.      
  17.     $width=$IMGInfo[0]; 
  18.     $height=$IMGInfo[1];   
  19.     $scalc = max($width/$SetW,$height/$SetH); 
  20.     $nw = intval($width/$scalc); 
  21.     $nh = intval($height/$scalc); 
  22.     echo "缩略大小:w$nw,h$nh <br /–>"
  23.      
  24.     if($SetW$nw == 1){$nw = $SetW;} 
  25.     if($SetH$nh == 1){$nh = $SetH;} 
  26.     echo "+修正1像素: w$nw,h$nh<br>"
  27.      
  28.     //补宽 
  29.     if($SetW$nw > 0){ 
  30.         $nh = $nh +(($nh/$nw) * ($SetW$nw)); 
  31.         echo "*需补宽".($SetW$nw).",陪补高".(($nh/$nw) * ($SetW$nw))."  <br>";  
  32.         $nw = $SetW
  33.     } 
  34.     //补高 
  35.     if($SetH$nh > 0){ 
  36.         $nw = $nw + (($nw/$nh) * ($SetH$nh)); 
  37.         echo "*需补高".($SetH$nh).",陪补宽". (($nw/$nh) * ($SetH$nh)) ."<br>"
  38.         $nh = $SetH
  39.     } 
  40.     $nw = intval($nw); 
  41.     $nh = intval($nh); 
  42.     echo "+修正大小:w$nw,h$nh<br>"
  43.      
  44.     $px = ($SetW – $nw)/2; 
  45.     $py = ($SetH – $nh)/2; 
  46.     echo "窗口大小:w$SetW,h$SetH <br>"
  47.     echo "+偏移修正:x$px,y$py <br>"
  48.      
  49.     $NewPhoto=imagecreatetruecolor($SetW,$SetH); 
  50.     imagecopyresized($NewPhoto,$ThisPhoto,$px,$py,0,0,$nw,$nh,$width,$height); 
  51.     ImageJpeg ($NewPhoto,$SaveTo); 
  52.     return true; 
  53.      
  54. ?> 

模板优惠价: (点击购买)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《PHP生成等比缩略图类和自定义函数分享》
文章链接:https://www.adminn.cn/news/8708.html
本站资源模板仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。2021.5月起,网站调整,暂不再分享免费模板。谢谢理解

Adminn.Cn 站长分享圈

帝国CMS精品模板腾讯云优惠券,代金券

2次开发,学习研究使用!扫码支付后联系QQ677123发货!

支付宝扫一扫打赏