开发者

Determine image aspect ratio is appropriate

Aloha,

how do I determine/calculate whether an image aspect ratio is appropriate (proportion) in Javascript programatically, based 开发者_JAVA百科on these information?

For Example: Below is ok:

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

This is not ok:

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


If that 'Ratio' value is the maximum allowable, then:

if (Ratio < (Width / Height)) {
    ... bad ratio ...
}


If you want the aspect ratio to be within 20% of a square then do this:

maxOff=0.2; //percent margin of aspect ratio acceptance... (20%)
if ((width/height)>=(1-maxOff)&&(width/height)<=(1+maxOff)) {
//image is ok
}


Why not just divide Height with Width, and then pass the result to an if statement that accepts numbers bigger than X and smaller than Y? If the result of the divide calculation doesn't fit, the proportions are off.


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;
}

then just set the size to nw,nh

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜