From: Brion Vibber Date: Tue, 29 Nov 2005 07:47:26 +0000 (+0000) Subject: * Reduce fractions in display of exif exposure time X-Git-Tag: 1.6.0~1098 X-Git-Url: https://git.cyclocoop.org/%7B%7B%20url_for%28?a=commitdiff_plain;h=7039d9bdd7127fca42efe54d4324489fab8b5dd7;p=lhc%2Fweb%2Fwiklou.git * Reduce fractions in display of exif exposure time --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 220afa5454..1e6349b8ed 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -261,6 +261,7 @@ fully support the editing toolbar, but was found to be too confusing. * (bug 4093) '&bot=1' in Special:Contributions now propagate to other links * Fix display of old recentchanges records for page moves * (bug 2111) Collapsable exif metadata table, clean up display +* Reduce fractions in display of exif exposure time === Caveats === 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; + } }