mathematical error or??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
da_buddha
Forum Newbie
Posts: 3
Joined: Sun Feb 12, 2006 3:45 am

mathematical error or??

Post by da_buddha »

hey guys... new here, looking for a bit of PHP help...

I'm putting together and working on tweaking some PHP code to have EXIF info from photos (look it up, from digi cams) be outputed via some PHP work. I already used the base code PHP EXIF Reader (found here) that works great. Only problem is, it displays some funky decimal number as the "shutter speed" instead of a nice "1/30 seconds" number. I don't exactly know what the calculation is to change it, but that isn't necessarily the concern.

My good friend wrote a small short PHP function to calculate the actual number. Anyway...

So, for reference, this photo, it outputs a number via the EXIF reading as 0.050761421319797. I used an application (non-PHP) to determine that it is actually 1/13 sec. So it needs to go from that number to 1/13.

It gets called from the original output decimal number via:

Code: Select all

$exif_data["ExposureTime"] = dTOf(round($exif_data["ExposureTime"],7));
And the function is:

Code: Select all

function dTOf( $dec ) {
	$numer=(int)$dec;
	$denom=1;

    // Special cases that this seems function seems to die on....
    if($dec == .0333333)
        return "1/30";
    if($dec == .0166667)
        return "1/60";

	//while ($numer != $dec){	//for some reason this failed when 1/60
	while($dec - $numer > .0000000001){
		$dec*=10;
		$denom*=10;
		$numer=(int)$dec;
	}
	
	if ($denom==1)
		return $numer;

	$lo = $numer;
    $hi = $denom;
    // Euclidean Algorithm (get GCD). concepts of math proves useful :)
	while($lo != 0){
		$t=$hi;
		$hi = $lo;
		$lo = $t - floor($t / $lo) * $lo;
    }
	//	$hi is the gcd

	return ($numer / $hi) ."/". ($denom / $hi);
}
And the odd thing is the page errors out with this error:

Code: Select all

Fatal error: Maximum execution time of 30 seconds exceeded in /home/cmuogs/public_html/photostack/includes/exif.php on line 419
Line 419 that it references is:

Code: Select all

$dec*=10;
Now what's odd is that it sometimes errors for other measurements, which is the "special cases" comment. It OFTEN works fine, so I know the calculation is correct, every so often it just errors like this. It depends on the camera and I guess however the camera calculates that number (the long decimal one).

Anyone provide some help as to why it completely errors out at that *10 part?? Thanks...
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Might want to use this instead...

Decimal to Fraction PHP code snippet
da_buddha
Forum Newbie
Posts: 3
Joined: Sun Feb 12, 2006 3:45 am

Post by da_buddha »

Damn... you're right, works fine now... I just get some crazy whack fraction instead of the 1/13... Gives me: 393701/5000000 (hah)
da_buddha
Forum Newbie
Posts: 3
Joined: Sun Feb 12, 2006 3:45 am

Post by da_buddha »

Aight, you're right, works fine. It is just some screwy number. I was referencing some other photos from a Nikon camera that have some crap number for their shutter speed, while everything from my own Canon camera comes out great. Thanks for the help! :)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Sometimes the Zend site has some nifty code snippets that will do obscure things you need. It's easier to check there at times. :)
Post Reply