To calculate the aspect ratio of an image, you divide the height by the width. The formula is Height * Width / Height. We use the below code to calculate aspect ratio in our tool. It’s basically saying when we ask for the GCD of (x,y), return us the ratio(width,height). In this instance, x = width and y = height of our image or device.
{
function ratio(x,y) {c=gcd(x,y); return ""+(x/c)+":"+(y/c)};
return ratio(width,height);
}