function ResizeImage($oldpic,$newpic,$maxwidth=800,$maxheight=600){
if( eregi('.jpg',$oldpic) ){
$im = imagecreatefromjpeg($oldpic);
}elseif( eregi('.png',$oldpic) ){
$im = imagecreatefrompng($oldpic);
}elseif( eregi('.gif',$oldpic) ){
$im = imagecreatefromgif($oldpic);
}else{
//die("图片格式不对$oldpic");
}
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
//imagepstext($newim,'tryr','10','12','000000','000000',0,0,5,5,1,1);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$newpic);
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$newpic);
}
ImageDestroy ($im);
}
if( eregi('.jpg',$oldpic) ){
$im = imagecreatefromjpeg($oldpic);
}elseif( eregi('.png',$oldpic) ){
$im = imagecreatefrompng($oldpic);
}elseif( eregi('.gif',$oldpic) ){
$im = imagecreatefromgif($oldpic);
}else{
//die("图片格式不对$oldpic");
}
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
//imagepstext($newim,'tryr','10','12','000000','000000',0,0,5,5,1,1);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$newpic);
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$newpic);
}
ImageDestroy ($im);
}