1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| 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(); } $im = ImageCreateFromGIF($srcFile); break; case 2: if(!function_exists("imagecreatefromjpeg")){ echo "你的GD庫不支持jpeg格式的圖片,請使用其他格式的圖片!<a href='javascript:go(-1);'>返回</a>"; exit(); } $im = ImageCreateFromJpeg($srcFile); break; case 3: $im = ImageCreateFromPNG($srcFile); break; } $srcW=ImageSX($im); $srcH=ImageSY($im); $toWH=$toW/$toH; $srcWH=$srcW/$srcH; if($toWH<=$srcWH){ $ftoW=$toW; $ftoH=$ftoW*($srcH/$srcW); } else{ $ftoH=$toH; $ftoW=$ftoH*($srcW/$srcH); } if($srcW>$toW||$srcH>$toH) { if(function_exists("imagecreatetruecolor")){ @$ni = ImageCreateTrueColor($ftoW,$ftoH); if($ni) ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); else{ $ni=ImageCreate($ftoW,$ftoH); ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); } }else{ $ni=ImageCreate($ftoW,$ftoH); ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); } if(function_exists('imagejpeg')) ImageJpeg($ni,$toFile); else ImagePNG($ni,$toFile); ImageDestroy($ni); } ImageDestroy($im); }
|