计算用于调整大小的图像大小比率 [英] Calculating image size ratio for resizing

查看:145
本文介绍了计算用于调整大小的图像大小比率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义的固定宽度和高度来调整图像大小。但是,我有问题,因为图像可以有任何类型的大小比例(可以是垂直水平)。在这种情况下,固定的宽度和高度会引起问题。我想以更聪明的方式计算宽度和高度

I have a defined fixed width and height to resize an image. However, I have problem a with this, because the image can have any kind of size ratio (it can be vertical or the horizontal). In this case, fixed width and height cause a problem. I want to calculate width and height in a smarter way.

例如,假设我已经定义了宽度1024px和高度768px 即可。我想调整垂直(高度1100px和宽度200px)的图像。因此,在我的情况下,它会调整为固定的大小(1024x768),因此宽度将从 100px增加到768px ,并且会非常难看。同样,如果图像的高度小于 768px ,则会强制将高度增加到 768px

For example lets say I have defined width 1024px and height 768px. And I want to resize an image which is vertical (height 1100px and width 200px). So in my case it will resize to fixed size (1024x768), so the width will be increased from 100px to 768px, and it will be very ugly. Similarly if the image has height less than 768px, it will increase the height by force to 768px.

因此我想根据原始图像大小比率计算新的图像大小。让我们说如果上面的示例图像应该调整为 768px 的最大高度,但那么宽度呢?它已经小于我的最大宽度,即 200px ,那么宽度应保持不变吗?还是应该进一步减少?

Therefore I would like to calculate the new image size based on the original image size ratio. Lets say if the above example image should be resized to maximum height of 768px, but then what about the width? it's already less than my "maximum width", which is 200px, so should the width remain unchanged? or should it be further decreased?

同样,如果图像的高度为200px,宽度为1100px 。所以宽度应该降低到1024px ,但高度怎么样?

Similarly, if the image has the height 200px, and the width 1100px. So the width should be decreased to 1024px, but what about the height?

第三个问题是,让我们假设高度和宽度都是如此超过最大高度和最大宽度,假设宽度:1100px和高度:4000px 。现在,由于宽度和高度都超过最大宽度和最大高度,但图像是垂直的,它将使其成为水平。那么如何在这种情况下检查是否应根据最大高度或根据最大宽度来调整图像?

The third problem is that, let's suppose if both height and width are more than the maximum height and maximum width, let's say width: 1100px and height:4000px. Now since width and height both are more than the maximum width and maximum height but the image is vertical, it will make it horizontal. So how can I check if in this case if I should resize the image according to maximum height, or according to maximum width?

我感谢您对此提供任何帮助。

I appreciate any help with this.

推荐答案

这是我个人抓取图像大小调整代码的代码。首先,您需要的数据:

Here's code from my personal grab bag of image resizing code. First, data you need:

list($originalWidth, $originalHeight) = getimagesize($imageFile);
$ratio = $originalWidth / $originalHeight;

然后,此算法尽可能地将图像拟合到目标尺寸,保持原始宽高比,不要将图像拉伸大于原始图像:

Then, this algorithm fits the image into the target size as best it can, keeping the original aspect ratio, not stretching the image larger than the original:

$targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));

if ($ratio < 1) {
    $targetWidth = $targetHeight * $ratio;
} else {
    $targetHeight = $targetWidth / $ratio;
}

$srcWidth = $originalWidth;
$srcHeight = $originalHeight;
$srcX = $srcY = 0;

这会使图像完全填满目标尺寸,而不是拉伸它:

This crops the image to fill the target size completely, not stretching it:

$targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);

if ($ratio < 1) {
    $srcX = 0;
    $srcY = ($originalHeight / 2) - ($originalWidth / 2);
    $srcWidth = $srcHeight = $originalWidth;
} else {
    $srcY = 0;
    $srcX = ($originalWidth / 2) - ($originalHeight / 2);
    $srcWidth = $srcHeight = $originalHeight;
}

这实际调整大小:

$targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetImage, $originalImage, 0, 0, $srcX, $srcY, $targetWidth, $targetHeight, $srcWidth, $srcHeight);

在这种情况下, $ size 只是宽度和高度均为一个数字(方形目标大小)。我相信你可以修改它以使用非方形目标。它还应该为您提供其他可以使用的其他调整大小算法的灵感。

In this case the $size is just one number for both width and height (square target size). I'm sure you can modify it to use non-square targets. It should also give you an inspiration on what other resizing algorithms you can use.

这篇关于计算用于调整大小的图像大小比率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆