计算图像大小比例以调整大小 [英] Calculating image size ratio for resizing

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

问题描述

我有一个定义的固定宽度和高度来调整图像大小.但是,我对此有问题,因为图像可以具有任何大小比例(可以是垂直水平).在这种情况下,固定宽度和高度会导致问题.我想以更智能的方式计算宽度和高度.

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.

例如,假设我已经定义了宽度 1024 像素和高度 768 像素.我想调整垂直(高度 1100 像素和宽度 200 像素) 的图像的大小.所以在我的例子中它会调整为固定的大小(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?

同样,如果图像具有高度 200 像素,宽度 1100 像素.所以宽度应该减少到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?

第三个问题是,假设高度和宽度都大于最大高度和最大宽度,比如说width: 1100px and height: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天全站免登陆