Bout time

0

Here it is, the new blog design! (Yes the one that has been sitting here for about 2 years…) I decided that it was good enough to be put to some use. It probably is still buggy so please let me know if something looks bad.  A couple of features aren’t quite finished -hopefully I will get to them soon. Let me know what you think!

Oh, and here’s a picture of a Gray bat (endangered species) that I captured a couple of weeks ago for my real job. Enjoy!

Quick shutter-speed fix for WordPress EXIF

5

For a long time (well ok, maybe since WordPress 2.5 came out) I have wanted to use image EXIF data on my blog.  Finally Sarah posted the code needed to get the extracted data displayed.  Much tanks to Sarah for her post, but as one quickly finds out, the shutter speed is stored (and displayed) as a decimal not the nice fraction that we’re used to seeing on our camera.

I thought there must be a fairly easy way to display the shutter speed as a fraction so I set out to see if I could find a solution.  I quickly found out that while php does a fairly good job at math, very few people must need to convert decimals to fractions.  A google search shows a few attempts with varied success but nothing that would work conveniently for the job at hand.  I dropped some of the shutter speeds into Excel to see if I could figure a convenient function to do the conversion and as it turns out, it didn’t take me all that long to figure it out… Unfortunately for me, I’m a biologist -not a php coding expert so please forgive any glaring errors and I won’t tell you how may times I had to hit the reload button on my browser trying to figure out the correct php syntax… (I’ve got to buy a book on php… any suggestions?).  Here is what I figured that seems to do the trick for me.

Starting with the code from Sarah.

<?php echo "<li>Shutter Speed: " . number_format($imgmeta['image_meta']['shutter_speed'],2) . " seconds</li>\n"; ?>

It occurred to me that the decimal could be converted to the denominator like so.

<?php echo (1 / $imgmeta['image_meta']['shutter_speed']) ?>

That’s nice, but because shutter speeds can be several seconds long I had to combine it with an “if” statement to separate the ones that needed to be displayed as a fraction and the others that needed to be left in the decimal format.

<?php if ((1 / $imgmeta['image_meta']['shutter_speed']) > 1)
{
echo "1/";
echo (1 / $imgmeta['image_meta']['shutter_speed']) . " second";
}
else{
echo $imgmeta['image_meta']['shutter_speed'] . " seconds";
}
?>

As it turns out, a few of the shutter speeds (from my camera at least) don’t turn out to be perfect whole numbers (e.g. 1/45 = 1/45.000045 etc.). The decimals can be easily trimmed off with the number_format function but then what about those shutter speeds of 1/1.5 or 1/2.5 that the camera likes to dish out on occasion?  Ever since I started working with if statements in Excel I’ve always used the stone soup principle -and it always serves me well (you do remember stone soup… don’t you)(“it’s good… but it would be better with some carrots…”).

Long story short; here’s the code that worked for me (and my camera).

<?php if (!empty($imgmeta['image_meta']['shutter_speed']))
{
if ((1 / $imgmeta['image_meta']['shutter_speed']) > 1)
{
echo "1/";
if ((number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1)) == 1.3
or number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1) == 1.5
or number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1) == 1.6
or number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1) == 2.5)
{
echo number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1, '.', '') . " second";
}
else{
echo number_format((1 / $imgmeta['image_meta']['shutter_speed']), 0, '.', '') . " second";
}
}
else{
echo $imgmeta['image_meta']['shutter_speed'] . " seconds";
}
}
?>

[Update: NOTE: the if (!empty($imgmeta['image_meta']['shutter_speed'])) function is required to prevent a PHP "dividing by 0" warning message if the shutter speed field is empty. ]

Finally, combining it together with the if statements that check to see whether a field is empty or not and all the rest that creates the table currently displaying the EXIF data on my site I get something like:

<?php if (is_attachment()) : $imgmeta = wp_get_attachment_metadata( $id );
	echo "<table class=\"exif\">\n";
	if (!empty($imgmeta['image_meta']['camera'])) echo "<tr><th>Camera:</th><td>" . $imgmeta['image_meta']['camera']."</td></tr>\n";
	if (!empty($imgmeta['image_meta']['focal_length']))echo "<tr><th>Focal length:</th><td>" . $imgmeta['image_meta']['focal_length']."&nbsp;mm</td></tr>\n";
	if (!empty($imgmeta['image_meta']['aperture']))echo "<tr><th>Aperture:</th><td>" . $imgmeta['image_meta']['aperture']."</td></tr>\n";
	if (!empty($imgmeta['image_meta']['shutter_speed']))
	{
	echo "<tr><th>Shutter Speed:&nbsp;</th><td>";
		if ((1 / $imgmeta['image_meta']['shutter_speed']) > 1)
		{
		echo "1/";
			if ((number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1)) == 1.3
			or number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1) == 1.5
			or number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1) == 1.6
			or number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1) == 2.5)
			{
			echo number_format((1 / $imgmeta['image_meta']['shutter_speed']), 1, '.', '') . " second</td></tr>\n";
			}
			else{
			echo number_format((1 / $imgmeta['image_meta']['shutter_speed']), 0, '.', '') . " second</td></tr>\n";
			}
		}
		else{
		echo $imgmeta['image_meta']['shutter_speed']." seconds</td></tr>\n";
		}
	}
	if (!empty($imgmeta['image_meta']['iso']))echo "<tr><th>ISO:</th><td>" . $imgmeta['image_meta']['iso']."</td></tr>\n";
	echo "</table>\n";
   endif;
?>

I hope this is helpful.  So far it seems to be correctly displaying all the shutter speeds that I've tried -though I haven't tried every last one.  You can see the code in action by clicking the picture below. Let me know what you think -Enjoy!