function save_scaled($img_name,$dst_image,$ext,$max_width,$max_height)
{
$size=@getimagesize($img_name);
$width=$size[0];
$height=$size[1];
if($max_width==0)
{
$max_width=$width;
}
if($max_height==0)
{
$max_height=$height;
}
$src=image_create_from($img_name,$ext);
if($width<=$max_width)
{
if($height<=$max_height)
{
$tn_width=$width;
$tn_height=$height;
}
else
{
$th_height=$max_height;
$tn_width=round($max_height * $width/$height);
}
}
else if($height<=$max_height)
{
$tn_width=$max_width;
$tn_height=round($max_width * $height/$width);
}
else
{
$x_ratio=$max_width/$width;
$y_ratio=$max_height/$height;
}
if($x_ratio <=$y_ratio)
{
$tn_width=$max_width;
$tn_height=round($x_ratio*$height);
}
else
{
$tn_height=$max_height;
$tn_width=round($y_ratio*$width);
}
$dst=@imagecreatetruecolor($tn_width,$th_height);
@imagecopyrcsampled($dst,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);
image_save($dst,$dst_image_name,$ext);
@imagedestroy($src);
@imagedestroy($dst);
}