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

时间:2022-11-19 12:57:05

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.

1。图像宽度超过最大宽度限制:然后将图像宽度调整为最大宽度,根据最大宽度限制计算高度。例如,图像宽度为2248,高度为532。宽度超过了,但高度更小。所以把宽度减小到最大1024,然后计算高度,大概是242。

2. Image height exceeds the maximum height limit: Similarly, resize the height to maximum height and calculate the width accordingly.

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

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.

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

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

6 个解决方案

#1


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

#2


2  

I don't know PHP, but I would do it this way (in C++ pseudo code):

我不知道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;   
}

I edited my code to include the third case, which I ignored before. I generally try to avoid nested if-statements, because of readability (personal style) and partially performance reasons ( mostly superstition ;-) ).

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

I don't know if my version is faster or more readable than the original version, but at least the redundancy of the $new_width and $new_height assignments is gone.

我不知道我的版本是否比原始版本更快或可读性更好,但至少$new_width和$new_height分配的冗余已经消失。

#3


2  

Using the same variable names and the if statement preventing upscaling:

使用相同的变量名和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;
} 
?>

#4


2  

I find this to be a more elegant solution to scale up or down, if you only want to scale down just add if($scale_percent<1) {}

我发现这是一个更优雅的向上或向下扩展的解决方案,如果您只想要向下扩展,只需添加if($scale_percent<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;        
}
?>

#5


1  

There is a very handy class that I often use for just this type of work, resizing images while retaining aspect ratio. It lets you resize the image to a given height or width...

有一个非常方便的类,我经常用于这种类型的工作,在保留纵横比的同时调整图像的大小。它允许您调整图像到给定的高度或宽度……

Image resizing class

调整图像的类

Note: you have to register on that site to get the file

注意:您必须在该站点注册才能获得文件

#6


0  

TO get width & height of Image Try this

要获得图像的宽度和高度,请尝试这个

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

#1


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

#2


2  

I don't know PHP, but I would do it this way (in C++ pseudo code):

我不知道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;   
}

I edited my code to include the third case, which I ignored before. I generally try to avoid nested if-statements, because of readability (personal style) and partially performance reasons ( mostly superstition ;-) ).

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

I don't know if my version is faster or more readable than the original version, but at least the redundancy of the $new_width and $new_height assignments is gone.

我不知道我的版本是否比原始版本更快或可读性更好,但至少$new_width和$new_height分配的冗余已经消失。

#3


2  

Using the same variable names and the if statement preventing upscaling:

使用相同的变量名和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;
} 
?>

#4


2  

I find this to be a more elegant solution to scale up or down, if you only want to scale down just add if($scale_percent<1) {}

我发现这是一个更优雅的向上或向下扩展的解决方案,如果您只想要向下扩展,只需添加if($scale_percent<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;        
}
?>

#5


1  

There is a very handy class that I often use for just this type of work, resizing images while retaining aspect ratio. It lets you resize the image to a given height or width...

有一个非常方便的类,我经常用于这种类型的工作,在保留纵横比的同时调整图像的大小。它允许您调整图像到给定的高度或宽度……

Image resizing class

调整图像的类

Note: you have to register on that site to get the file

注意:您必须在该站点注册才能获得文件

#6


0  

TO get width & height of Image Try this

要获得图像的宽度和高度,请尝试这个

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