X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FExif.php;h=9c3965a58e7e5b91403c74de589cbc429d80808f;hb=71e75ac5ead653914ec3ae100e26d186375a306e;hp=8e71986952d2d39e89bb62b0e1f00335c4e79262;hpb=baed3966b0a16103d655c9605c795fbbda8833ad;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Exif.php b/includes/Exif.php index 8e71986952..9c3965a58e 100644 --- a/includes/Exif.php +++ b/includes/Exif.php @@ -998,7 +998,7 @@ class FormatExif { case 'ExposureTime': // Show the pretty fraction as well as decimal version $tags[$tag] = wfMsg( 'exif-exposuretime-format', - $val, $this->formatNum( $val ) ); + $this->formatFraction( $val ), $this->formatNum( $val ) ); break; case 'FNumber': @@ -1053,4 +1053,52 @@ class FormatExif { else return $num; } + + /** + * Format a rational number, reducing fractions + * + * @access private + * + * @param mixed $num The value to format + * @return mixed A floating point number or whatever we were fed + */ + function formatFraction( $num ) { + if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) ) { + $numerator = intval( $m[1] ); + $denominator = intval( $m[2] ); + $gcd = $this->gcd( $numerator, $denominator ); + if( $gcd != 0 ) { + // 0 shouldn't happen! ;) + return $numerator / $gcd . '/' . $denominator / $gcd; + } + } + return $this->formatNum( $num ); + } + + /** + * Calculate the greatest common divisor of two integers. + * + * @param int $a + * @param int $b + * @return int + * @access private + */ + function gcd( $a, $b ) { + /* + // http://en.wikipedia.org/wiki/Euclidean_algorithm + // Recursive form would be: + if( $b == 0 ) + return $a; + else + return gcd( $b, $a % $b ); + */ + while( $b != 0 ) { + $remainder = $a % $b; + + // tail recursion... + $a = $b; + $b = $remainder; + } + return $a; + } }