[PHP]PHP缩放图片生成保持原图纵横比的缩略图

  1. //by MoreWindows (http://blog.csdn.net/MoreWindows ) 
  2. class CImage 
  3.     /** 
  4.      * 生成保持原图纵横比的缩略图,支持.png .jpg .gif 
  5.      * 缩略图类型统一为.png格式 
  6.      * $srcFile     原图像文件名称 
  7.      * $toW         缩略图宽 
  8.      * $toH         缩略图高 
  9.      * $toFile      缩略图文件名称,为空覆盖原图像文件 
  10.      * @return bool     
  11.     */ 
  12.     public static function CreateThumbnail($srcFile, $toW, $toH, $toFile="")  
  13.     { 
  14.         if ($toFile == "") 
  15.         {  
  16.             $toFile = $srcFile;  
  17.         } 
  18.         $info = ""; 
  19.         //返回含有4个单元的数组,0-宽,1-高,2-图像类型,3-宽高的文本描述。 
  20.         //失败返回false并产生警告。 
  21.         $data = getimagesize($srcFile, $info); 
  22.         if (!$data) 
  23.             return false; 
  24.          
  25.         //将文件载入到资源变量im中 
  26.         switch ($data[2]) //1-GIF,2-JPG,3-PNG 
  27.         { 
  28.         case 1: 
  29.             if(!function_exists("imagecreatefromgif")) 
  30.             { 
  31.                 echo "the GD can't support .gif, please use .jpeg or .png! <href='javascript:history.back();'>back</a>"; 
  32.                 exit(); 
  33.             } 
  34.             $im = imagecreatefromgif($srcFile); 
  35.             break; 
  36.              
  37.         case 2: 
  38.             if(!function_exists("imagecreatefromjpeg")) 
  39.             { 
  40.                 echo "the GD can't support .jpeg, please use other picture! <href='javascript:history.back();'>back</a>"; 
  41.                 exit(); 
  42.             } 
  43.             $im = imagecreatefromjpeg($srcFile); 
  44.             break; 
  45.                
  46.         case 3: 
  47.             $im = imagecreatefrompng($srcFile);     
  48.             break; 
  49.         } 
  50.          
  51.         //计算缩略图的宽高 
  52.         $srcW = imagesx($im); 
  53.         $srcH = imagesy($im); 
  54.         $toWH = $toW / $toH; 
  55.         $srcWH = $srcW / $srcH; 
  56.         if ($toWH <= $srcWH)  
  57.         { 
  58.             $ftoW = $toW; 
  59.             $ftoH = (int)($ftoW * ($srcH / $srcW)); 
  60.         } 
  61.         else  
  62.         { 
  63.             $ftoH = $toH; 
  64.             $ftoW = (int)($ftoH * ($srcW / $srcH)); 
  65.         } 
  66.          
  67.         if (function_exists("imagecreatetruecolor"))  
  68.         { 
  69.             $ni = imagecreatetruecolor($ftoW, $ftoH); //新建一个真彩色图像 
  70.             if ($ni)  
  71.             { 
  72.                 //重采样拷贝部分图像并调整大小 可保持较好的清晰度 
  73.                 imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); 
  74.             }  
  75.             else  
  76.             { 
  77.                 //拷贝部分图像并调整大小 
  78.                 $ni = imagecreate($ftoW, $ftoH); 
  79.                 imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); 
  80.             } 
  81.         } 
  82.         else  
  83.         { 
  84.             $ni = imagecreate($ftoW, $ftoH); 
  85.             imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); 
  86.         } 
  87.  
  88.         //保存到文件 统一为.png格式 
  89.         imagepng($ni, $toFile); //以 PNG 格式将图像输出到浏览器或文件 
  90.         ImageDestroy($ni); 
  91.         ImageDestroy($im); 
  92.         return true; 
  93.     } 
  94. }
posted on 2016-06-13 16:55  xuhuang  阅读(426)  评论(0编辑  收藏  举报