Using PHP to Extract EXIF Information from JEPGs
Like most of us digital natives, I have a collected a lot of digital photographs. Most of these are in a JPEG format. A number of years ago, I played around with some PHP scripts to extract EXIF information from a collection of photographs. Of course you need to first compile PHP with exif support. Here is a simple starter script to get you going:
<code>
<html>
<head></head>
<body>
<?php
// initialize recursive directory iterator
$dir = new RecursiveDirectoryIterator("/storm/pictures/South America 2008/");
foreach(new RecursiveIteratorIterator($dir) as $file) {
// find photo files
// extract EXIF information
if (preg_match('/(.jpg|.jpeg|.tif|.tiff)$/i', $file->getFilename())) {
$exif = exif_read_data($file->getPathname());
if (preg_match('/(EXIF|IFD0)/', $exif['SectionsFound'])) {
?>
<div style="padding: 5px; border-bottom: 2px solid silver">
<table>
<tr>
<td><img src="thumb.php?path=<?php echo urlencode(realpath($file->getPathname())); ?>" /></td>
<td style="font: 12px Helvetica,sans-serif; text-align: top">
File path: <?php echo realpath($file->getPathname()); ?> <br/>
Camera: <?php echo $exif['Model']; ?><br/>
Date and time: <?php echo date('d-M-Y h:i', $exif['FileDateTime']); ?><br/>
Flash: <?php echo $exif['Flash']; ?> |
Orientation: <?php echo $exif['Orientation']; ?><br/>
Exif version: <?php echo $exif['ExifVersion']; ?> |
Image Number: <?php echo $exif['ImageNumber']; ?><br/>
White Balance: <?php echo $exif['WhiteBalance']; ?> |
File Name: <?php echo $exif['FileName']; ?><br/>
Exposure: <?php echo $exif['ExposureBiasValue']; ?> |
Image Width: <?php echo $exif['RelatedImageWidth']; ?> |
Image Heightif['RelatedImageHeight']; ?><br/>
F-number: <?php echo $exif['FNumber']; ?> |
Aperture: <?php echo $exif['ApertureValue']; ?><br/>
Shutter speed: <?php echo $exif['ShutterSpeedValue']; ?> |
Focal length: <?php echo $exif['FocalLength']; ?><br/>
</td>
</tr>
</table>
</div>
<?php
}
}
}
?>
</code>
And this is the resultant image of the resultant webpage that is produced:
Now I can easily write a script that will parse a bunch of sub directories and report back all photographs that where taken with an iPhone, and the Date, GPS and compass readings.
Note: This technique can be used to extract MP3 metadata information from your MP3 collection. I will hunt up that sample script one day.