how to scale a image to a give height and width?

to call the function

$img_prop=try_img_scale(”admin/uploads/1.jpg”,200,200);

function defenition

function try_img_scale($img_path, $width, $height)
{
$img_prop=getimagesize($img_path);
$img_width=$img_prop[0];
$img_height=$img_prop[1];
if($img_width>$width || $img_height>$height)
{
$scale_width=($width/$img_width)*100;
$scale_height=($height/$img_height)*100;
if($scale_width <= $scale_height)
$scale_val=$scale_width;
else
$scale_val=$scale_height;

//$scale_val=$scale_width < $scale_height?$scale_width:$scale_height;
$img_prop[0]=($scale_val/100)*$img_width;
$img_prop[1]=($scale_val/100)*$img_height;
}
return $img_prop;
}

Posted by Suresh B

Leave a Reply

You must be logged in to post a comment.