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

sun*_*jie 5 php resize image image-processing

我想计算调整大小的图像宽度和大小.我认为可能有三种情况:

1.图像宽度超出最大宽度限制: 然后将图像宽度调整为最大宽度,并根据最大宽度限制计算高度.例如,图像宽度为2248,高度为532.宽度超过但高度较小.因此,将宽度减小到最大值1024并计算高度,大约为242.

2.图像高度超过最大高度限制: 同样,将高度调整为最大高度并相应地计算宽度.

3.高度和宽度均超过最大高度和最大宽度: 进一步处理,确定图像是垂直还是水平.如果图像是水平的,请将宽度调整为最大宽度,并根据该值计算高度.否则,如果图像是垂直或正方形,则将高度调整为最大值并根据该值计算宽度.

对于这些情况,你能看看我的代码,给你的专家意见,如果有什么好处?还是可以改进?怎么样?

PS.我昨天问了类似的问题.在我之前的问题中,我不确定逻辑应该是什么,现在我知道它应该是什么(如上所述),并且想知道它是否有任何好处.谢谢你的帮助.

<?
$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);
        }
    }
} 
?>
Run Code Online (Sandbox Code Playgroud)

小智 10

// 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; 
}
Run Code Online (Sandbox Code Playgroud)


Yon*_*Ran 1

有一个非常方便的类,我经常将其用于此类工作,在保留纵横比的同时调整图像大小。它可以让您将图像大小调整为给定的高度或宽度......

图像大小调整类

注意:您必须在该网站上注册才能获取文件