gpt4 book ai didi

php - 计算宽度和高度以调整图像大小

转载 作者:可可西里 更新时间:2023-11-01 13:36:38 26 4
gpt4 key购买 nike

我想计算调整大小的图像宽度和大小。我觉得可以分为三种情况:

<强>1。图片宽度超过最大宽度限制:然后将图片宽度调整到最大宽度,并根据最大宽度限制计算高度。例如图片宽度为2248,高度为532,宽度超出高度小于。因此,将宽度减少到最大值 1024,并计算高度,大约为 242。

<强>2。图片高度超过最大高度限制:同样,将高度调整为最大高度并相应地计算宽度。

<强>3。高度和宽度均超过最大高度和最大宽度:进一步处理,找出图像是垂直的还是水平的。如果图像是水平的,将宽度调整为最大宽度并据此计算高度。否则,如果图像是垂直的或正方形的,将高度调整为最大并据此计算宽度。

对于这些情况,你能看看我下面的代码,给出你的专家意见,如果它有什么好处?或者可以改进吗?怎么办?

附言。我问了类似question昨天。在我之前的问题中,我不确定逻辑应该是什么,现在我知道它应该是什么(如上所述),并且想知道它是否有用。感谢您的帮助。

<?
$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;
}

关于php - 计算宽度和高度以调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6606445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com