[英]Algorithm to resize an image proportionally keeping it as close as possible to given dimensions
myFunction(400, 300, 50, 100)
=> Must return the width and heigth to resize my 400x300 (1st and 2nd params) image proportionally . myFunction(400, 300, 50, 100)
=> 必须返回宽度和高度以按比例调整我的 400x300(第一个和第二个参数)图像的大小。 The resized image must be at minimum 50x100 (3rd and 4th params).调整大小的图像必须至少为 50x100 (第 3 和第 4 参数)。 It's totally ok to go 52x.. or ..x102 but the "oversize" must be as small as matematically possible to keep the aspect ratio.使用 52x .. 或 ..x102 完全可以,但“超大尺寸”必须尽可能小以保持纵横比。
I must write a function (I will use Javascript/Jquery, but let's not worry about the language: I'm interested in the logic) such as this:我必须编写一个函数(我将使用 Javascript/Jquery,但让我们不要担心语言:我对逻辑感兴趣),例如:
[new_image_width, new_image_height] function(image_width, image_height, reference_width, reference_height)
This function takes:这个函数需要:
image_width
: the width of an image image_width
: 图像的宽度image_height
: the height of an image image_height
: 图像的高度reference_width
: the desidered minimum width of the image (see below) reference_width
:所需的图像最小宽度(见下文)reference_height
: the desidered minimum height of the image (see below) reference_height
:所需的图像最小高度(见下文)It returns:它返回:
new_image_width
: the proportionally resized width of the image (see below) new_image_width
:按比例调整大小的图像宽度(见下文)new_image_height
: the proportionally resized height of the image (see below) new_image_height
:按比例调整大小的图像高度(见下文)The function must calculate the closest width and height to respective "reference" parameter, without going below, and preserving the aspect ratio .该函数必须计算与相应“参考”参数最接近的宽度和高度,不要低于,并保留纵横比。
My function must not actually resize the image, only return the new integer to resize to.我的函数不得实际调整图像大小,只返回要调整大小的新整数。
Note: I'm confortable with code but 1st-grade-level with math.注意:我对代码很满意,但数学是一年级的。 Please show some mercy :-(请怜悯一些:-(
If you don't care about rounding errors如果你不关心舍入误差
Let让
ratio = min(image_width / reference_width, image_height / reference_height)
and return并返回
image_width / ratio
image_height / ratio
If you do care about rounding errors如果您确实关心舍入误差
Find the greatest common divisor GCD
of image_width
and image_height
.找到image_width
和image_height
最大公约数GCD
。 The smallest image you can make with the exact same aspect ratio has dimensions您可以使用完全相同的纵横比制作的最小图像具有尺寸
image_width' = image_width / GCD
image_height' = image_height / GCD
Every larger image with the exact same aspect ratio is an integer multiple of those.每个具有完全相同纵横比的较大图像都是这些图像的整数倍。 So, let所以让
ratio_width = ceil(reference_width / image_width')
ratio_height = ceil(reference_heigth / image_heigth')
and和
ratio = max(ratio_width, ratio_height)
then your result is那么你的结果是
ratio * image_width'
ratio * image_height'
Alright, so try this:好的,那么试试这个:
function myFunction(image_width, image_height, reference_width, reference_height) {
var proportion = image_width/image_height;
if(reference_height*proportion < reference_width){
return [reference_width, reference_width/proportion];
} else {
return [reference_height*proportion,reference_height];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.