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

首頁 > 語言 > PHP > 正文

php 上傳圖片并按比例生成指定大小圖

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

這是一款圖象縮略函數,把上傳的新圖片給$srcfile然后進行文件按$thumbwidth 縮小圖寬最大尺寸與$thumbheitht 縮小圖高最大尺寸,生成小圖.

圖象縮略函數,參數說明:

$srcfile 原圖地址; $dir  新圖目錄 $thumbwidth 縮小圖寬最大尺寸 $thumbheitht 縮小圖高最大尺寸 $ratio 默認等比例縮放 為1是縮小到固定尺寸.

php 上傳圖片并按比例生成指定大小圖實例代碼如下:

  1. function makethumb($srcfile,$dir,$thumbwidth,$thumbheight,$ratio=0) 
  2. {  
  3.  //判斷文件是否存在  
  4. if (!file_exists($srcfile))return false; 
  5.  //生成新的同名文件,但目錄不同  
  6. $imgname=explode('/',$srcfile);  
  7. $arrcount=count($imgname);  
  8. $dstfile = $dir.$imgname[$arrcount-1];  
  9. //縮略圖大小  
  10. $tow = $thumbwidth;  
  11. $toh = $thumbheight;  
  12. if($tow < 40) $tow = 40;  
  13. if($toh < 45) $toh = 45;     
  14.  //獲取圖片信息  
  15.     $im ='';  
  16.     if($data = getimagesize($srcfile)) {  
  17.         if($data[2] == 1) {  
  18.             $make_max = 0;//gif不處理  
  19.             if(function_exists("imagecreatefromgif")) {  
  20.                 $im = imagecreatefromgif($srcfile);  
  21.             }  
  22.         } elseif($data[2] == 2) {  
  23.             if(function_exists("imagecreatefromjpeg")) {  
  24.                 $im = imagecreatefromjpeg($srcfile);  
  25.             }  
  26.         } elseif($data[2] == 3) {  
  27.             if(function_exists("imagecreatefrompng")) {  
  28.                 $im = imagecreatefrompng($srcfile);  
  29.             }  
  30.         }  
  31.     }  
  32.     if(!$imreturn '';  
  33.     $srcw = imagesx($im);  
  34.     $srch = imagesy($im);  
  35.     $towh = $tow/$toh;  
  36.     $srcwh = $srcw/$srch;  
  37.     if($towh <= $srcwh){  
  38.         $ftow = $tow;  
  39.         $ftoh = $ftow*($srch/$srcw);  
  40.     } else {  
  41.         $ftoh = $toh;  
  42.         $ftow = $ftoh*($srcw/$srch);  
  43.     }  
  44.     if($ratio){  
  45.         $ftow = $tow;  
  46.         $ftoh = $toh;  
  47.     }  
  48.     //縮小圖片  
  49.     if($srcw > $tow || $srch > $toh || $ratio) {  
  50.         if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow$ftoh)) {  
  51.             imagecopyresampled($ni$im, 0, 0, 0, 0, $ftow$ftoh$srcw$srch);  
  52.         } elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow$ftoh)) {  
  53.             imagecopyresized($ni$im, 0, 0, 0, 0, $ftow$ftoh$srcw$srch);  
  54.         } else {  
  55.             return '';  
  56.         }  
  57.         if(function_exists('imagejpeg')) {  
  58.             imagejpeg($ni$dstfile);  
  59.         } elseif(function_exists('imagepng')) {  
  60.             imagepng($ni$dstfile);  
  61.         }  
  62.     }else {  
  63.         //小于尺寸直接復制  
  64.     copy($srcfile,$dstfile);  
  65.     }  
  66.     imagedestroy($im);  
  67.     if(!file_exists($dstfile)) {  
  68.         return '';  
  69.     } else { //開源代碼Vevb.com 
  70.         return $dstfile;  
  71.     }  

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万安县| 竹山县| 重庆市| 黔南| 喀喇沁旗| 周至县| 镇平县| 辽中县| 泸水县| 都江堰市| 思南县| 来宾市| 彰武县| 胶南市| 昌吉市| 城口县| 惠安县| 临沧市| 浙江省| 西乡县| 山西省| 丰顺县| 西藏| 黑龙江省| 赤水市| 于都县| 南城县| 夏邑县| 当雄县| 昭通市| 石首市| 义乌市| 临江市| 南充市| 枣阳市| 平阳县| 玉环县| 镇远县| 汾阳市| 晋中市| 桓仁|