Search code examples
javascriptphpaspect-ratio

Determine image aspect ratio is appropriate


Aloha,

how do I determine/calculate whether an image aspect ratio is appropriate (proportion) in Javascript programatically, based 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

Solution

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