以下是PHP源代码(ResizeImage.php)。
<BR><?php <BR>$FILENAME="image.thumb"; <BR>// 生成图片的宽度 <BR>$RESIZEWIDTH=400; <BR>// 生成图片的高度 <BR>$RESIZEHEIGHT=400; <br><br>function ResizeImage($im,$maxwidth,$maxheight,$name){ <BR>$width = imagesx($im); <BR>$height = imagesy($im); <BR>if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){ <BR>if($maxwidth && $width > $maxwidth){ <BR>$widthratio = $maxwidth/$width; <BR>$RESIZEWIDTH=true; <BR>} <BR>if($maxheight && $height > $maxheight){ <BR>$heightratio = $maxheight/$height; <BR>$RESIZEHEIGHT=true; <BR>} <BR>if($RESIZEWIDTH && $RESIZEHEIGHT){ <BR>if($widthratio < $heightratio){ <BR>$ratio = $widthratio; <BR>}else{ <BR>$ratio = $heightratio; <BR>} <BR>}elseif($RESIZEWIDTH){ <BR>$ratio = $widthratio; <BR>}elseif($RESIZEHEIGHT){ <BR>$ratio = $heightratio; <BR>} <BR>$newwidth = $width * $ratio; <BR>$newheight = $height * $ratio; <BR>if(function_exists("imagecopyresampled")){ <BR>$newim = imagecreatetruecolor($newwidth, $newheight); <BR>imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); <BR>}else{ <BR>$newim = imagecreate($newwidt<a>本2文来*源gao($daima.com搞@代@#码(网</a><strong>搞gaodaima代码</strong>h, $newheight); <BR>imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); <BR>} <BR>ImageJpeg ($newim,$name . ".jpg"); <BR>ImageDestroy ($newim); <BR>}else{ <BR>ImageJpeg ($im,$name . ".jpg"); <BR>} <BR>} <br><br>if($_FILES['image']['size']){ <BR>if($_FILES['image']['type'] == "image/pjpeg"){ <BR>$im = imagecreatefromjpeg($_FILES['image']['tmp_name']); <BR>}elseif($_FILES['image']['type'] == "image/x-png"){ <BR>$im = imagecreatefrompng($_FILES['image']['tmp_name']); <BR>}elseif($_FILES['image']['type'] == "image/gif"){ <BR>$im = imagecreatefromgif($_FILES['image']['tmp_name']); <BR>} <BR>if($im){ <BR>if(file_exists("$FILENAME.jpg")){ <BR>unlink("$FILENAME.jpg"); <BR>} <BR>ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME); <BR>ImageDestroy ($im); <BR>} <BR>} <BR>?> <BR>
以下是测试代码(demo.php)
<BR><?php <BR>include('ResizeImage.php'); <BR>if(!empty($_POST)){ <BR>echo($FILENAME.".jpg?cache=".rand(0,999999)); <BR>} <BR>?> <BR> <BR><p> <BR> <BR> <BR>