【www.gdgbn.com--php常用代码】

 代码如下

function bigtosmallimg($file,$path,$w=120,$h=90)
{
 $img=$path.$file;
 $imgarr=getimagesize($img);
 $sw=$imgarr[0];//原图宽
 $sh=$imgarr[1];//原图高
 $stype=$imgarr[2];
 //按比例缩放
 if($sw/$sh>$w/$h){
  $mw=$w;
  $mh=(int)$sh*($w/$sw);
 }
 else{
  $mw=(int)$sw*($h/$sh);
  $mh=$h;
 }

 switch($stype){//根据上传好的图形文件类型新建一个用来生成缩略图的源文件。
   case 1:
    $srcf = imagecreatefromgif($img);
    break;
   case 2:
    $srcf = imagecreatefromjpeg($img);
    break;
   case 3:
    $srcf = imagecreatefrompng($img);
    break;
   default:
    showmsg("程序调用错误。");
    break;
 }

 $desf =imagecreatetruecolor($mw,$mh);

 imagecopyresampled($desf,$srcf,0,0,0,0,$mw,$mh,$sw,$sh);
 $sm_name=$path."s_".$file;
 switch($stype){
  case 1:
   imagegif($desf,$sm_name);
   break;
  case 2:
   imagejpeg($desf,$sm_name);
   break;
  case 3:
   imagepng($desf,$sm_name);
   break;
  default:
   showmsg("无法生成www.111cn.net" . $stype . "的缩略图。");
   break;
 }
 imagedestroy($desf);
 imagedestroy($srcf);

}

//此缩略图调用方法

 代码如下
bigtosmallimg($file,$path,$w=120,$h=90);
/*

$file = 图片的路径
$path = 生成后保存的路径
$w =图片宽度
$h =图片高度
*/

本文来源:http://www.gdgbn.com/jiaocheng/26038/