examinedliving
8/23/2014 - 4:01 AM

Simple Easy Aspect Ratio Script

Simple Easy Aspect Ratio Script

// I got tired of forgetting how to calculate image aspect ratio valuables, and then trying to solve the algebra, instead of actually thinking it through -- 
// so I wrote the worlds simplest function


// h= current image height in pixels
// w= current image width
// nw= the width the image will be
// returns new height that will preserve aspect ratio at new width
// 



function ar(h,w,nw){
  return (h*nw)/w;
  }
  

// can be used to calculate aspect ratio in both directions;
    // example:
    // To find out new height of image to make it fit in available space
    // assuming image heigth= 900 & image width=1600 && new width=1920
    
    // ar(900,1600,1920);
    // returns 1080
    
    // in reverse
       // say you have an image at 1920 that you want to crop down to a 16:9 aspect ratio
       // well then ar(9,16,1920);
       // returns 1080 - hey --- magic!