PHP proportional image resizing math 7/18/13
Recently I needed to enlarge and shrink images proportionally
null
, if not they return an array with new proportional dimensions.
<?php
function image_resize_up($orig_w, $orig_h, $MIN_W, $MIN_H){
$ratio = $orig_w * 1.0 / $orig_h;
$w_undersized = ($orig_w < $MIN_W);
$h_undersized = ($orig_h < $MIN_H);
if ($w_undersized OR $h_undersized)
{
$new_w = round( max($MIN_W, $ratio * $MIN_H) );
$new_h = round( max($MIN_H, $MIN_W / $ratio) );
return array('width' => $new_w, 'height' => $new_h);
}
return null;
}
function image_resize_down($orig_w, $orig_h, $MAX_W, $MAX_H){
$ratio = $orig_w * 1.0 / $orig_h;
$w_undersized = ($orig_w > $MAX_W);
$h_undersized = ($orig_h > $MAX_H);
if ($w_undersized OR $h_undersized)
{
$new_w = round( min($MAX_W, $ratio * $MAX_H) );
$new_h = round( min($MAX_H, $MAX_W / $ratio) );
return array('width' => $new_w, 'height' => $new_h);
}
return null;
}
?>
-
Resizing the images proportionally means that they will maintain their original [aspect ratio](https://en.wikipedia.org/wiki/Aspect_ratio_(image%29) and not look distorted. ↩
-
In Wordpress you can use registered image sizes to shrink images proportionally, but there is nothing for enlarging them. ↩
-
Extended and modified from this answer by jon_darkstar on Stack Overflow. ↩