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

时间:2011-07-07 06:00:50

标签: php image image-processing resize

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

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

2。图像高度超出最大高度限制 同样,将高度调整到最大高度并相应地计算宽度。

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

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

PS。昨天我问过类似的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);
        }
    }
} 
?>

6 个答案:

答案 0 :(得分: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; 
}

答案 1 :(得分:2)

我不懂PHP,但我会这样做(在C ++伪代码中):

float ratio = 1.0;
float ratio_w = max_width/static_cast<float>(original_width);
float ratio_h = max_height/static_cast<float>(original_height);
float ratio_max = std::max(ratio_w, ratio_h);

if (ratio_max < 1.0)
{  
   // width & height are larger than allowed
   // scale to the larger scaling factor
   ratio = ratio_max;
}
else
{
   // pick a scaling factor <= 1.0
   ratio = std::min(ratio, ratio_w);
   ratio = std::min(ratio, ratio_h);
}

if (ratio < 1.0) // this if-clause is not necessary
{
   new_width = original_width * ratio;
   new_height = original_height * ratio;   
}

我编辑了我的代码以包含我之前忽略的第三种情况。我通常会尝试避免嵌套的if语句,因为可读性(个人风格)和部分性能原因(主要是迷信;-))。

我不知道我的版本是否比原始版本更快或更易读,但至少$new_width$new_height作业的冗余已经消失。

答案 2 :(得分:2)

使用相同的变量名和阻止升级的if语句:

<?php
$max_width = 1024;
$max_height = 768;

$img = "t2.jpg";


list($original_width, $original_height) = getimagesize($img);

$ratio = ($max_width / $original_width < $max_height / $original_height
  ? $max_width / $original_width
  : $max_height / $original_width
);
if ($original_width > $max_width || $original_height > $max_height) {
  $new_width = $original_width * $ratio;
  $new_height = $original_height * ratio;
} 
?>

答案 3 :(得分:2)

我发现这是一个更加优雅的扩展或缩小解决方案,如果你只想缩小,只需添加if($ scale_percent&lt; 1){}

<?php
$width  = $max_width = 400;
$height = $max_height = 400;
$size = getimagesize($img);
if(count($size)) {
    $width = $size[0];
    $height = $size[1];
    $width_percent = $max_width/$width;
    $height_percent = $max_height/$height;
    $scale = ($width_percent>$height_percent)?'height':'width';
    $scale_percent = $scale.'_percent';
    $scale_percent = $$scale_percent;   
    $width*=$scale_percent;
    $height*=$scale_percent;        
}
?>

答案 4 :(得分:1)

我经常使用一个非常方便的类来完成这类工作,在保持纵横比的同时调整图像大小。 它允许您将图像调整到给定的高度或宽度......

Image resizing class

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

答案 5 :(得分:0)

获得宽度和宽度图像高度试试这个

$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];