views:

2674

answers:

9

I plan to use it with javascript to crop an image to fit the entire window.

I appreciate all help you can provide.

Edit: I'll be using a 3rd part component that only accepts the aspect ratio in the format like: 4:3, 16:9

A: 
Width / Height

?

Frank Schwieterman
I'll be using a component that only accepts the aspect ratio like: 4:3, 16:9
Nathan
+4  A: 
Aspect Ratio = width / height

if that is what you're after. You can then multiply it by one of the dimensions of the target space to find out the other (that maintains the ratio) e.g.

widthT = heightT * Aspect Ratio
heightT = widthT / Aspect Ratio
Gishu
A: 

I believe that aspect ratio is width divided by height.

 r = w/h
Sean A.O. Harney
+21  A: 

Many of the answers to date give you the ratio, but not a usable aspect ratio, which is generally integer:integer rather than float:1.

What you need to do is find the greatest common divisor (GCD) and divide both values by that. The GCD is the highest number that evenly divides both numbers. So the GCD for 6 and 10 is 2, the GCD for 44 and 99 is 11.

For example, a 1024x768 monitor has a GCD of 256. When you divide both values by that you get 4x3 or 4:3.

A (recursive) GCD algorithm:

function gcd (a,b):
    if b = 0:
        return a
    return gcd (b, a mod b)

In C:

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

int main(void) {
    printf ("gcd(1024,768) = %d\n",gcd(1024,768));
}

And here's some complete HTML/Javascript which shows one way to detect the screen size and calculate the aspect ratio from that. This works in FF3, I'm unsure what support other browsers have for screen.width and screen.height.

<html><body>
    <script type="text/javascript">
        function gcd (a, b) {
            return (b == 0) ? a : gcd (b, a%b);
        }
        var w = screen.width;
        var h = screen.height;
        var r = gcd (w, h);
        document.write ("<pre>");
        document.write ("Dimensions = ", w, " x ", h, "<br>");
        document.write ("Gcd        = ", r, "<br>");
        document.write ("Aspect     = ", w/r, ":", h/r);
        document.write ("</pre>");
    </script>
</body></html>

It outputs (on my weird wide-screen monitor):

Dimensions = 1680 x 1050
Gcd        = 210
Aspect     = 8:5

Others that I tested this on:

Dimensions = 1280 x 1024
Gcd        = 256
Aspect     = 5:4

Dimensions = 1152 x 960
Gcd        = 192
Aspect     = 6:5

Dimensions = 1280 x 960
Gcd        = 320
Aspect     = 4:3

Dimensions = 1920 x 1080
Gcd        = 120
Aspect     = 16:9

I wish I had that last one at home but, no, it's a work machine unfortunately.

What you do if you find out the aspect ratio is not supported by your graphic resize tool is another matter. I suspect the best bet there would be to add letter-boxing lines (like the ones you get at the top and bottom of your old TV when you're watching a wide-screen movie on it). I'd add them at the top/bottom or the sides (whichever one results in the least number of letter-boxing lines) until the image meets the requirements.

One thing you may want to consider is the quality of a picture that's been changed from 16:9 to 5:4 - I still remember the incredibly tall, thin cowboys I used to watch in my youth on television before letter-boxing was introduced. You may be better off having one different image per aspect ratio and just resize the correct one for the actual screen dimensions before sending it down the wire.

paxdiablo
Thanks buddy, I got it =]
Nathan
This was the first answer I thought of giving, but I was worried that it wouldn't return results useful to his 3rd party component if his window is sized to something like 1021x711, for example.
Nosredna
Seems like an overkill. And it doesn't work for cases Nosredna mentioned. I have a solution based on approximation.
Chetan Sastry
@Chetan, yeah I thought the same thing.
Nosredna
My client told me that he needs the aspect ratio of the viewer. Its a service for a print shop. Its for statistics I think
Nathan
I'm always wondering if you can assume the pixels are square. But maybe that's an obsolete concern.
Nosredna
A: 

I think this does what you are asking for:

webdeveloper.com - decimal to fraction

Width/height gets you a decimal, converted to a fraction with ":" in place of '/' gives you a "ratio".

Tobias Cohen
+1  A: 

This algorithm in Python gets you part of the way there.


Tell me what happens if the windows is a funny size.

Maybe what you should have is a list of all acceptable ratios (to the 3rd party component). Then, find the closest match to your window and return that ratio from the list.

Nosredna
+4  A: 

I guess you want to decide which of 4:3 and 16:9 is the best fit.

function getAspectRatio(width, height) {
    var ratio = width / height;
    return ( Math.abs( ratio - 4 / 3 ) < Math.abs( ratio - 16 / 9 ) ) ? '4:3' : '16:9';
}
Chetan Sastry
While your solution is fine for 4x3 and 16x9, this doesn't seem like it would support all possible aspect ratios (though maybe that's not important for the OP). The ratio for most wide-screen monitors, for example, is 16x10 (1920x1200, 1600x1000)?
Falaina
We really don't have enough information to answer the question well. :-)
Nosredna
+1  A: 

As an alternative solution to the GCD searching, I suggest you to check against a set of standard values. You can find a list on Wikipedia.

mouviciel
+1  A: 

Im assuming your talking about video here, in which case you may also need to worry about pixel aspect ratio of the source video. For example.

PAL DV comes in a resolution of 720x576. Which would look like its 4:3. Now depending on the Pixel aspect ratio (PAR) the screen ratio can be either 4:3 or 16:9.

For more info have a look here http://en.wikipedia.org/wiki/Pixel_aspect_ratio

You can get Square pixel Aspect Ratio, and a lot of web video is that, but you may want to watch out of the other cases.

Hope this helps

Mark

Mark Underwood