Forum Moderators: coopster

Message Too Old, No Replies

Simplifying fraction

         

tebrino

12:00 am on Jul 12, 2009 (gmt 0)

10+ Year Member



Anyone has any idea how to it? I'm trying to do this in order to calculate aspect ratio of images. For example, how to detect that image of 2048x1152 resolution has 16:9 aspect ratio? I'm sure is something simple, but I'm completely lost

NomikOS

2:42 am on Jul 12, 2009 (gmt 0)

10+ Year Member



seems like "Rule of Three"

look at:

[en.wikipedia.org...]

NomikOS

2:47 am on Jul 12, 2009 (gmt 0)

10+ Year Member



2048 -> 16
1152 -> x

x = 2048/(16*1152)

?

rainborick

2:57 pm on Jul 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you need to look for scripts than can calculate 'least common multiple' and adapt them to your needs.

tebrino

3:15 pm on Jul 12, 2009 (gmt 0)

10+ Year Member



Thanks for your replies. What I needed to do is find greatest common divisor:

<?php

function GCD($a, $b) {

while ($b != 0) {

$remainder = $a % $b;
$a = $b;
$b = $remainder;

}
return abs ($a);
}

?>

And then simply divide both sides with it:

<?php

$a = 2048;
$b = 1152;
$gcd = GCD($a, $b);
$a = $a/$gcd;
$b = $b/$gcd;
$ratio = $a . ":" . $b;

?>

Not very elegant at this moment, but it works :-)