<?php
#ImageCopyResized($dst,$src,0,0,0,0,$x,$y,$width,$height);
#ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
#ImageCopyResized()函数在所有GD版本中有效,但其缩放图像的算法比较粗糙,
#可能会导致图像边缘的锯齿.
#GD 2.x中新增了一个ImageCopyResampled()函数,其像素插值算法得到的图像边缘比较平滑,
#但该函数的速度比ImageCopyResized()慢.function ImageResize($srcFile,$toW,$toH,$toFile="")
{
   if($toFile==""){ $toFile = $srcFile; }
   $info = "";
   $data = GetImageSize($srcFile,$info);
   switch ($data[2])
   {
    case 1:
      if(!function_exists("imagecreatefromgif")){
       echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
       exit();
      }
      $src = ImageCreateFromGIF($srcFile);
      break;
    case 2:
      if(!function_exists("imagecreatefromjpeg")){
       echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";
       exit();
      }
      $src = ImageCreateFromJpeg($srcFile);   
      break;
    case 3:
      $src = ImageCreateFromPNG($srcFile);   
      break;
  }
  $width=ImageSX($src);
  $height=ImageSY($src);
  $ratio_w=1.0 * $toW / $width;
  $ratio_h=1.0 * $toH / $height;
  $ratio=1.0;
    if($width>$toW||$height>$toH)
  {
     if(function_exists("imagecreatetruecolor")){
        @$dst = ImageCreateTrueColor($x,$y);
        if($dst) ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
        else{
         $dst=ImageCreate($x,$y);
          ImageCopyResized($dst,$src,0,0,0,0,$x,$y,$width,$height);
        }
     }else{
        $dst=ImageCreate($x,$y);
        ImageCopyResized($dst,$src,0,0,0,0,$x,$y,$width,$height);
     }
     if(function_exists('imagejpeg')) ImageJpeg($dst,'',100);
     else ImagePNG($dst,'',100);
     ImageDestroy($dst);
  }
// 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了)
if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
if($ratio_w < $ratio_h) {
$ratio = $ratio_h ; // 情况一,宽度的比例比高度方向的小,按照高度的比例标准来裁剪或放大
}else {
$ratio = $ratio_w ;
}
// 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求
$inter_w=(int)($toW / $ratio);
$inter_h=(int) ($toH / $ratio);
$inter_img=ImageCreate($inter_w , $inter_h);
#$inter_img=imagecreatetruecolor($inter_w , $inter_h);
//ImageCopyResampled
ImageCopyResized($inter_img, $src, 0,0,0,0,$inter_w,$inter_h);
#imagecopy($inter_img, $src, 0,0,0,0,$inter_w,$inter_h);// 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
// 定义一个新的图像
$new_img=ImageCreate($toW,$toH);
#$new_img=imagecreatetruecolor($toW,$toH);
ImageCopyResampled($new_img,$inter_img,0,0,0,0,$toW,$toH,$inter_w,$inter_h);
 if(function_exists('imagejpeg')) ImageJpeg($new_img,'',100);
     elseif(function_exists('imagepng')) Imagepng($new_img,'',100); 
 else ImagePNG($new_img,'',100);
} // end if 1
// 2 目标图像 的一个边大于原图,一个边小于原图 ,先放大平普图像,然后裁剪
// =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) )
else{
$ratio=$ratio_h>$ratio_w? $ratio_h : $ratio_w; //取比例大的那个值
// 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大
$inter_w=(int)($width * $ratio);
$inter_h=(int) ($height * $ratio);
$inter_img=imagecreatetruecolor($inter_w , $inter_h);
//将原图缩放比例后裁剪
imagecopyresampled($inter_img,$src,0,0,0,0,$inter_w,$inter_h,$width,$height);
// 定义一个新的图像
$new_img=imagecreatetruecolor($toW,$toH);
imagecopy($new_img, $inter_img, 0,0,0,0,$toW,$toH);
switch($type) {
case IMAGETYPE_JPEG :
#imagejpeg($new_img, $toFile,100); // 存储图像
imagejpeg($new_img,'',100);
break;
case IMAGETYPE_PNG :
#imagepng($new_img,$toFile,100);
imagepng($new_img,'',100);
break;
case IMAGETYPE_GIF :
#imagegif($new_img,$toFile,100);
imagegif($new_img,'',100);
break;
default:
break;
}
}
ImageDestroy($src);
}
$srcFile=$_GET['src'];
$getwidth=$_GET['width'];
$getheight=$_GET['height'];
$toFile="";
 if(function_exists('imagejpeg')) header('Content-Type: image/jpeg');
     elseif(function_exists('imagepng')) header('Content-Type: image/png');
 else header('Content-Type: image/gif');
ImageResize($srcFile,$getwidth,$getheight,$toFile);
?>