图像调整大小算法

发布于 2024-09-11 08:26:36 字数 697 浏览 2 评论 0原文

我想编写一个函数来缩小图像以适应指定的边界。例如,我想调整 2000x2333 图像的大小以适合 1280x800。必须保持纵横比。我想出了以下算法:

NSSize mysize = [self pixelSize]; // just to get the size of the original image
int neww, newh = 0;
float thumbratio = width / height; // width and height are maximum thumbnail's bounds
float imgratio = mysize.width / mysize.height;

if (imgratio > thumbratio)
{
    float scale = mysize.width / width;
    newh = round(mysize.height / scale);
    neww = width;
}
else
{
    float scale = mysize.height / height;
    neww = round(mysize.width / scale);
    newh = height;
}

它似乎有效。嗯……看来。但后来我尝试将 1280x1024 图像调整为 1280x800 范围,结果为 1280x1024(显然不适合 1280x800)。

有人知道这个算法应该如何工作吗?

I want to write a function to downsize an image to fit specified bounds. For example i want to resize a 2000x2333 image to fit into 1280x800. The aspect ratio must be maintained. I've come up with the following algorithm:

NSSize mysize = [self pixelSize]; // just to get the size of the original image
int neww, newh = 0;
float thumbratio = width / height; // width and height are maximum thumbnail's bounds
float imgratio = mysize.width / mysize.height;

if (imgratio > thumbratio)
{
    float scale = mysize.width / width;
    newh = round(mysize.height / scale);
    neww = width;
}
else
{
    float scale = mysize.height / height;
    neww = round(mysize.width / scale);
    newh = height;
}

And it seemed to work. Well ... seemed. But then i tried to resize a 1280x1024 image to a 1280x800 bounds and it gave me a result of 1280x1024 (which obviously doesn't fit in 1280x800).

Does anybody have any ideas how this algorithm should work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫烠⑼条掵仅有一顆心 2024-09-18 08:26:36

我通常这样做的方法是查看原始宽度和新宽度之间的比率以及原始高度和新高度之间的比率。

之后按最大比例缩小图像。例如,如果您想将 800x600 图像调整为 400x400 图像,则宽度比率将为 2,高度比率将为 1.5。将图像缩小 2 的比例即可得到 400x300 的图像。

NSSize mysize = [self pixelSize]; // just to get the size of the original image
int neww, newh = 0;
float rw = mysize.width / width; // width and height are maximum thumbnail's bounds
float rh = mysize.height / height;

if (rw > rh)
{
    newh = round(mysize.height / rw);
    neww = width;
}
else
{
    neww = round(mysize.width / rh);
    newh = height;
}

The way I usually do this is to look at the ratio between the original width and the new width and the ratio between the original height and the new height.

After this shrink the image by the biggest ratio. For example, if you wanted to resize an 800x600 image into a 400x400 image the width ratio would be 2, and the height ratio would be 1.5. Shrinking the image by a ratio of 2 gives a 400x300 image.

NSSize mysize = [self pixelSize]; // just to get the size of the original image
int neww, newh = 0;
float rw = mysize.width / width; // width and height are maximum thumbnail's bounds
float rh = mysize.height / height;

if (rw > rh)
{
    newh = round(mysize.height / rw);
    neww = width;
}
else
{
    neww = round(mysize.width / rh);
    newh = height;
}
帅气称霸 2024-09-18 08:26:36

这是解决该问题的一种方法:

您知道图像的高度或宽度将等于边界框的高度或宽度。

一旦确定哪个尺寸等于边界框的尺寸,就可以使用图像的长宽比来计算另一个尺寸。

double sourceRatio = sourceImage.Width / sourceImage.Height;
double targetRatio = targetRect.Width / targetRect.Height;

Size finalSize;
if (sourceRatio > targetRatio)
{
    finalSize = new Size(targetRect.Width, targetRect.Width / sourceRatio);
}
else
{
    finalSize = new Size(targetRect.Height * sourceRatio, targetRect.Height);
}

Here's a way to approach the problem:

You know that either the image's height or width will be equal to that of the bounding box.

Once you've determined which dimension will equal the bounding box's, you use the image's aspect ratio to calculate the other dimension.

double sourceRatio = sourceImage.Width / sourceImage.Height;
double targetRatio = targetRect.Width / targetRect.Height;

Size finalSize;
if (sourceRatio > targetRatio)
{
    finalSize = new Size(targetRect.Width, targetRect.Width / sourceRatio);
}
else
{
    finalSize = new Size(targetRect.Height * sourceRatio, targetRect.Height);
}
在你怀里撒娇 2024-09-18 08:26:36
    $max_width = MAX_SIZE;
    $max_height = MAX_SIZE;

    if ($width >= $height)                                      // with bigger than height
    { 
        if ($width >= $max_width)
        {
            $new_width = $max_width;                            
            $new_height = round($height*$max_width/$width);     // scale in height
        }
        else 
        {
            $new_width = $width;                                // smaller than max dimentions
            $new_height = $height;                              // maintain dimentions
        }
    }
    else                                                        // height bigger than width
    {
        if ($height >= $max_height)
        {
            $new_width = round($width*$max_height/$height);     // scale in width
            $new_height = $max_height;
        }
        else
        {
            $new_width = $width;                                // smaller than max dimentions
            $new_height = $height;                              // maintain dimentions
        }
    }
    $max_width = MAX_SIZE;
    $max_height = MAX_SIZE;

    if ($width >= $height)                                      // with bigger than height
    { 
        if ($width >= $max_width)
        {
            $new_width = $max_width;                            
            $new_height = round($height*$max_width/$width);     // scale in height
        }
        else 
        {
            $new_width = $width;                                // smaller than max dimentions
            $new_height = $height;                              // maintain dimentions
        }
    }
    else                                                        // height bigger than width
    {
        if ($height >= $max_height)
        {
            $new_width = round($width*$max_height/$height);     // scale in width
            $new_height = $max_height;
        }
        else
        {
            $new_width = $width;                                // smaller than max dimentions
            $new_height = $height;                              // maintain dimentions
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文