tags:

views:

34

answers:

3

Re,

One photo with exposure being 1/640 has the EXIF field of "ExposureTime" eq. "15625/10000000". I am not sure why some photos display this value in a readable format (e.g., "1/100"), but I need to convert this "15625" back to "1/640". How? :)

Thanks.

+2  A: 

It's simple mathematics: simply divide the top and bottom of the fraction by the top value.

  15625 / 10000000
= (15625/15625) / (10000000/15625)
= 1 / 640

In PHP, you can do it like this:

$exposure = "15625/10000000";
$parts = explode("/", $exposure);
$exposure = implode("/", array(1, $parts[1]/$parts[0]));

echo $exposure;
Dean Harding
So long as the top is never 0, nor an imperfect fraction nor a fraction that has a non-1 numerator when reduced. Other wise agree
ircmaxell
@ircmaxell: Right, it's also assuming that it's numerical and all that, but I assume you'd have error checking in place. The problem is, you can't always trust that whatever wrote the files is putting the correct thing in there :)
Dean Harding
Thanks, exactly what I was looking for.
MarkL
A: 

This is the code I use to normalize the exposure,

                    if (($bottom % $top) == 0) {
                            $data = '1/'.round($bottom/$top, 0).' sec';
                    }       else {
                            if ($bottom == 1) {
                                    $data = $top.' sec';
                            } else {
                                    $data = $top.'/'.$bottom.' sec';
                            }
                    }

It handles most exposures correctly but I see some weird ones once a while.

ZZ Coder
Thanks, I'll use it.
MarkL
A: 

You can use Euclid's algorithm to find the greatest common divisor, which will help you reduce the fraction.

AbeVoelker