I want to calculate the image width and size for resize. I think there can be three situations:
1. Image width exceeds the maximum width limit: Then resize the image width to the maximum width, and calculate the height according to the maximum width limit. For example, image width is 2248, and height is 532. The width exceeds but height is less. So reduce the width to maximum 1024 and calculate height, which will be around 242.
2. Image height exceeds the maximum height limit: Similarly, resize the height to maximum height and calculate the width accordingly.
3. Height and width both exceeds the maximum height and maximum width: Process further, find out if the image is vertical or horizontal. If the image is horizontal, resize width to maximum width and calculate height according to that. Else, if the image is vertical or square, resize height to maximum and calculate width according to that.
For these situations, Can you have a look my code below,give your expert opinion about it, if it is any good? Or can it be improved? How?
PS. I asked similar question yesterday. In my previous question I was not sure what the logic should be, and now I know what it should be (mentioned above), and would like to know if its any good. Thanks for any help.
<?
$max_width = 1024;
$max_height = 768;
$img = "t2.jpg";
list($original_width, $original_height) = getimagesize($img);
if (($original_width > $max_width) OR ($original_height > $max_height))
{
//original width exceeds, so reduce the original width to maximum limit.
//calculate the height according to the maximum width.
if(($original_width > $max_width) AND ($original_height <= $max_height))
{
$percent = $max_width/$original_width;
$new_width = $max_width;
$new_height = round ($original_height * $percent);
}
//image height exceeds, recudece the height to maxmimum limit.
//calculate the width according to the maximum height limit.
if(($original_width <= $max_width) AND ($original_height > $max_height))
{
$percent = $max_height/$original_height;
$new_height = $max_height;
$new_width = round ($original_width * $percent);
}
//both height and width exceeds.
//but image can be vertical or horizontal.
if(($original_width > $max_width) AND ($original_height > $max_height))
{
//if image has more width than height
//resize width to maximum width.
if ($original_width > $original_height)
{
$percent = $max_width/$original_width;
$new_width = $max_width;
$new_height = round ($original_height * $percent );
}
//image is vertical or square. More height than width.
//resize height to maximum height.
else
{
$new_height = $max_height;
$percent = $max_height/$original_height;
$new_height = $max_height;
$new_width = round ($original_width * $percent);
}
}
}
?>
// Usage example to find the proper dimensions to resize an image down to 300x400 pixels maximum:
list($width, $height) = getimagesize($image);
$new_dimensions = resize_dimensions(100,100,$width,$height);
// Calculates restricted dimensions with a maximum of $goal_width by $goal_height
function resize_dimensions($goal_width,$goal_height,$width,$height) {
$return = array('width' => $width, 'height' => $height);
// If the ratio > goal ratio and the width > goal width resize down to goal width
if ($width/$height > $goal_width/$goal_height && $width > $goal_width) {
$return['width'] = $goal_width;
$return['height'] = $goal_width/$width * $height;
}
// Otherwise, if the height > goal, resize down to goal height
else if ($height > $goal_height) {
$return['width'] = $goal_height/$height * $width;
$return['height'] = $goal_height;
}
return $return;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With