Home > Article > Backend Development > Change image size according to proportion_PHP tutorial
/**
Change the image size according to the proportion (not generating thumbnails)
@param string $img image path
@param int $max_w Maximum zoom width
@param int $max_h Maximum zoom height
*/
function chImageSize ($img,$max_w,$max_h)
{
$size = @getimagesize($img);
$w = $size[0];
$h
//Calculate scaling ratio
@$w_ratio = $max_w / $w;
@$h_ratio = $max_h / $h;
//Determine the width and height of the processed image
If( ($w <= $max_w) && ($h <= $max_h) )
{
$tn['w'] = $w;
$tn['h'] = $h;
}
else if(($w_ratio * $h) < $max_h)
{
$tn['h'] = ceil($w_ratio * $h);
$tn['w'] = $max_w;
}
else
{
$tn['w'] = ceil($h_ratio * $w);
$tn['h'] = $max_h;
}
$tn['rc_w'] = $w;
$tn['rc_h'] = $h;
Return $tn ;
}
?>
Function description and examples