按比例调整图像大小的算法,使其尽可能接近给定尺寸
Algorithm to resize an image proportionally keeping it as close as possible to given dimensions
例子
myFunction(400, 300, 50, 100)
=> 必须 return 宽度和高度才能 按比例 调整我的 400x300(第一个和第二个参数)图像的大小。调整后的图像 必须至少为 50x100(第 3 和第 4 个参数)。 52x.. 或 ..x102 完全没问题,但 "oversize" 必须尽可能小以保持纵横比。
详情
我必须写一个函数(我会用 Javascript/Jquery,但我们不要担心语言:我对逻辑感兴趣)例如:
[new_image_width, new_image_height] function(image_width, image_height, reference_width, reference_height)
这个函数需要:
image_width
: 图片的宽度
image_height
: 图片的高度
reference_width
: 要求的图片最小宽度(见下)
reference_height
: 要求的图片最小高度(见下)
它returns:
new_image_width
:按比例调整的图像宽度(见下文)
new_image_height
: 按比例调整的图像高度(见下文)
该函数必须计算 最接近相应 "reference" 参数的宽度和高度,不低于此值,并保持纵横比。
我的函数不能实际调整图像大小,只能return要调整到的新整数。
注意:我对代码很满意,但对数学只有一年级水平。请手下留情:-(
好的,试试这个:
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];
}
}
如果你不关心舍入误差
让
ratio = min(image_width / reference_width, image_height / reference_height)
和return
image_width / ratio
image_height / ratio
如果您确实关心舍入误差
求 image_width
和 image_height
的最大公约数 GCD
。您可以使用完全相同的宽高比制作的最小图像尺寸为
image_width' = image_width / GCD
image_height' = image_height / GCD
每个具有完全相同宽高比的较大图像都是它们的整数倍。所以,让
ratio_width = ceil(reference_width / image_width')
ratio_height = ceil(reference_heigth / image_heigth')
和
ratio = max(ratio_width, ratio_height)
那么你的结果是
ratio * image_width'
ratio * image_height'
例子
myFunction(400, 300, 50, 100)
=> 必须 return 宽度和高度才能 按比例 调整我的 400x300(第一个和第二个参数)图像的大小。调整后的图像 必须至少为 50x100(第 3 和第 4 个参数)。 52x.. 或 ..x102 完全没问题,但 "oversize" 必须尽可能小以保持纵横比。
详情
我必须写一个函数(我会用 Javascript/Jquery,但我们不要担心语言:我对逻辑感兴趣)例如:
[new_image_width, new_image_height] function(image_width, image_height, reference_width, reference_height)
这个函数需要:
image_width
: 图片的宽度image_height
: 图片的高度reference_width
: 要求的图片最小宽度(见下)reference_height
: 要求的图片最小高度(见下)
它returns:
new_image_width
:按比例调整的图像宽度(见下文)new_image_height
: 按比例调整的图像高度(见下文)
该函数必须计算 最接近相应 "reference" 参数的宽度和高度,不低于此值,并保持纵横比。
我的函数不能实际调整图像大小,只能return要调整到的新整数。
注意:我对代码很满意,但对数学只有一年级水平。请手下留情:-(
好的,试试这个:
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];
}
}
如果你不关心舍入误差
让
ratio = min(image_width / reference_width, image_height / reference_height)
和return
image_width / ratio
image_height / ratio
如果您确实关心舍入误差
求 image_width
和 image_height
的最大公约数 GCD
。您可以使用完全相同的宽高比制作的最小图像尺寸为
image_width' = image_width / GCD
image_height' = image_height / GCD
每个具有完全相同宽高比的较大图像都是它们的整数倍。所以,让
ratio_width = ceil(reference_width / image_width')
ratio_height = ceil(reference_heigth / image_heigth')
和
ratio = max(ratio_width, ratio_height)
那么你的结果是
ratio * image_width'
ratio * image_height'