1

阿罗哈,

如何根据这些信息以编程方式在 Javascript 中确定/计算图像纵横比是否合适(比例)?

例如:下面是好的:

Width = 570px
Height = 520px
Ratio = 10
Aspect = 57:52

这不行:

Width = 815px
Height = 85px
Ratio = 5
Aspect = 163:17
4

4 回答 4

2

如果该“比率”值是允许的最大值,则:

if (Ratio < (Width / Height)) {
    ... bad ratio ...
}
于 2011-06-28T18:21:12.217 回答
1

如果您希望纵横比在正方形的 20% 以内,请执行以下操作:

maxOff=0.2; //percent margin of aspect ratio acceptance... (20%)
if ((width/height)>=(1-maxOff)&&(width/height)<=(1+maxOff)) {
//image is ok
}
于 2011-06-28T18:54:36.303 回答
0

为什么不直接将高度除以宽度,然后将结果传递给接受大于 X 且小于 Y 的数字的 if 语句?如果除法计算的结果不合适,则比例关闭。

于 2011-06-28T18:21:52.030 回答
0
var nh=0;
var nw=0;
if (Width>Height) 
{
    nw=100;//or whatever you want the new thing to be.
    nh=(100*Height)/Width;
}
else
{
    nh=100;//same as before just switched
    nw=(100*Height)/Width;
}

然后只需将大小设置为 nw,nh

于 2011-06-28T18:25:27.943 回答