JPEG EXIF Data

From DreamHost
Jump to: navigation, search

Display all EXIF data for a particular jpg file

<?php
echo "test1.jpg:<br />\n";
$exif = exif_read_data('tests/test1.jpg', 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";

$exif = exif_read_data('tests/test2.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
echo "$key.$name: $val<br />\n";
}
}
?> 

Display "built-in" thumbnail from jpg file

Save as showthumb.php to use with following "gallery" page. Also consider adding error checking from listing farther below.

<?php
$imgdat = exif_thumbnail($_REQUEST['image'],$width, $height, $type);
header('Content-type: ' . image_type_to_mime_type($type));
echo($imgdat);
?> 

Display "gallery" of thumbnails for all jpg files in a directory with selected EXIF data

<html>
<head></head>
<body>
<table>
<?php
// define directory path
$dir = ".";

// iterate through files
// look for JPEGs
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($image = readdir($dh)) !== false) {
            if (preg_match("/.jpg/", $image)) {
                // read EXIF headers
                $exif = exif_read_data($image, 0, true);
                echo "<tr>";
                // get thumbnail
                // link to full image
                              
                echo("<td valign=top><A href=" . $image ."> <IMG border=0 src=showthumb.php?image=" . $image ."></A><td>");
                
        echo "<td valign=top><font size=-1>";
                // get file name
                echo "File: <b>" . $exif['FILE']['FileName'] . "</b><br/>";
                // get timestamp
                echo "Timestamp: "  . $exif['IFD0']['DateTime'] . "<br/>";
                // get image dimensions
                echo "Dimensions: " . $exif['COMPUTED']['Width'] . " x " . $exif['COMPUTED']['Height'] . " <br/>";
                // get camera make and model
                echo "Camera: " . $exif['IFD0']['Model'] . "<br/>";
                echo "Exposure Time: " . $exif['EXIF']['ExposureTime'] . "<br/>";
                echo "Aperture: " . $exif['COMPUTED']['ApertureFNumber'] . "<br/>";
                echo "F Number: " . $exif['EXIF']['FNumber'] . "<br/>";
                echo "ISO: " . $exif['EXIF']['ISOSpeedRatings'] . "    ";
                echo "Flash: " . $exif['EXIF']['Flash'] . "<br/>";
                echo "</font></td>";
                echo "</tr>";
           }
        }
       closedir($dh);
   }
}

?>
</table>
</body>
</html> 

Show "built-in" thumbnail, with error check for not available

<?php
if (array_key_exists('file', $_REQUEST)) {
   $image = exif_thumbnail($_REQUEST['file'], $width, $height, $type);
} else {
   $image = false;
}
if ($image!==false) {
   header('Content-type: ' .image_type_to_mime_type($type));
   echo $image;
   exit;
} else {
   // no thumbnail available, handle the error here
   echo 'No thumbnail available';
}
?>

See Also