<?php 
/* 
* @Program: 生成缩略图
* @author:  cht 
* 调用方法： 
*      $params = array(array('size' => 124, 'file' => $inFolder."/".$t."_310.".$extName));
*      thumbnail_generator($uploadfile, $params);.
*/ 
function thumbnail_calcsize($w, $h, $square){
   $k = $square / max($w, $h);
   return array($w*$k, $h*$k);
}
function thumbnail_generator($srcfile, &$params){
   @list($w, $h) = getimagesize($srcfile);    
   if ($w == false)        
   return false; 
   if (!(is_array($params)&&is_array($params[0])))        
   return false; 
   $arrName  = explode(".",$srcfile);
   $intName = sizeof($arrName);
   $extName = strtolower($arrName[$intName-1]);
   if ($extName=="gif"){
   $src = ImageCreateFromGIF($srcfile);
   }else{
   $src = ImageCreateFromJpeg($srcfile);
   }   
   list($s1_w, $s1_h) = thumbnail_calcsize($w, $h, $params[0]['size']);
   $img_s1 = imagecreatetruecolor($s1_w, $s1_h);    
   imagecopyresampled($img_s1, $src, 0, 0, 0, 0, $s1_w, $s1_h, $w, $h);    
   imagedestroy($src); 
   for($i=1; $i<sizeof($params); $i++)    {
        list($cur_w, $cur_h) = thumbnail_calcsize($w, $h, $params[$i]['size']);
        $img_cur = imagecreatetruecolor($cur_w, $cur_h);        
        imagecopyresampled($img_cur, $img_s1, 0, 0, 0, 0, $cur_w, $cur_h, $s1_w, $s1_h);        
        imagejpeg($img_cur, $params[$i]['file'], 90);        
        imagedestroy($img_cur);    
        }   
   imagejpeg($img_s1, $params[0]['file'], 90);    
   imagedestroy($img_s1);    
   return true;
}
?> 