0

Say I'm targeting all images on a page and want to make sure that they all pass a test of being > 1:5...

This will give me the aspect ratio (from: here):

function gcd(a, b) {
    return (b == 0) ? a : gcd(b, a % b);
}

var image = document.getElementById('image');
var w = image.width;
var h = image.height;
var r = gcd(w, h);

Demo: http://jsfiddle.net/f8LwL/

But how can I make sure it's > 1:5 always?

Community
  • 1
  • 1
bob_cobb
  • 2,229
  • 11
  • 49
  • 109
  • 3
    Looks like you want to simplify the ratio. But you can just check `w/h > 1/5` as you can compare floating point numbers easily. – pimvdb Oct 25 '12 at 21:21
  • I guess I'm not sure what you're asking. You have a method to compute image dimension greatest common denominator, but ratio is x:y, and how do you propose to force image ratios on a site that's not yours? – im so confused Oct 25 '12 at 21:21
  • How about dividing height by width and looking at the result. It should be 5 with very little or no remainder. – Terry Oct 25 '12 at 21:22
  • @pimvdb yeah that's going to work. Thanks. – bob_cobb Oct 25 '12 at 21:25
  • @AK4749 I'm not forcing image ratios on a site that isn't mine. Not sure what you are talking about. – bob_cobb Oct 25 '12 at 21:26
  • @bob_cobb bad interpretation of what you said, as you were! – im so confused Oct 25 '12 at 21:26

2 Answers2

4

why are you attempting to find gcd's? Your aspect ratio is your width to height ratio.. ie w/h .. so to ensure that your aspect ratio is > 1:5 (ie > .2), just check (w/h) > (1/5)

hexist
  • 5,151
  • 26
  • 33
1

How about

if(w/h > 1/5 ) {
    alert("Aspect ratio is fine");
} else {
    alert("Aspect ratio is too low");
}
Dancrumb
  • 26,597
  • 10
  • 74
  • 130