VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP生成等比缩略图类和自定义函数分享

这篇文章主要介绍了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. ?> 
  55.  

出处:http://www.phpfensi.com/php/20210302/13800.html


相关教程