国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP生成等比縮略圖類和自定義函數分享

2024-09-04 11:43:16
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了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 "+修正大?。簑$nw,h$nh<br>"
  43.      
  44.     $px = ($SetW - $nw)/2; 
  45.     $py = ($SetH - $nh)/2; 
  46.     echo "窗口大?。簑$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. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 诸暨市| 临朐县| 鄯善县| 抚宁县| 东乌珠穆沁旗| 平武县| 永春县| 新郑市| 大丰市| 无为县| 柘城县| 瑞丽市| 贞丰县| 富宁县| 卫辉市| 诸暨市| 大田县| 江源县| 哈密市| 威海市| 鄂伦春自治旗| 仁怀市| 湘潭县| 新民市| 永丰县| 贵定县| 丽江市| 县级市| 深州市| 湘西| 阜新市| 阳新县| 库伦旗| 平阳县| 阿图什市| 伽师县| 奉节县| 清涧县| 呼和浩特市| 南充市| 泸溪县|