Reading Exif data with PHP

Within most photos from digital cameras besides the actual image, there’s a little ”information block” call EXIF data. If you have the correct PHP extension installed on your server – the one called ”exif” – it’s pretty easy to read the EXIF data and display them, as you like.

First, let’s check if the extension is available?

1
2
3
4
5
6
$load_ext = get_loaded_extensions();
if (!in_array(exif, $load_ext)) {
echo "Exif is NOT available";
} else {
echo "Exif extension is available.";
};

If you have the extension available, reading the data is a simple matter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$nikonFile   = './aarhus_demo_photo.jpg';
$exifNikon   = getExifData($nikonFile);
 
$olympusFile = './zanzibar_demo_photo.jpg';
$exifOlympus = getExifData($olympusFile);
 
echo "<h2>Olympus EXIF data</h2>";
echo "<pre>";
print_r($exifOlympus);
echo "</pre>";
 
echo "<h2>Nikon EXIF data</h2>";
echo "<pre>";
print_r($exifNikon);
echo "</pre>";
 
function getExifData($file) {
$exif = exif_read_data($file, 0, true);
return $exif;
}

As you may notice, there’s a photo from an Olympus camera and a Nikon camera as an example. The data available from each photo isn’t quite the same. Most generic fields are available, but some fields are camera specific (or maker specific).

See the example above in action (source). The two images are from Aarhus (Nikon) and Zanzibar (Olympus). You can read more on EXIF in Wikipedia.

  • This was really informative, but doesn’t tell us how we can edit the output in a way that we can manually control. For example if I didn’t want to show all of the EXIF variables, only a select few.

    http://neonglow.net/exif.php

    Any information on how I can re-style that mess would be so appreciated!

  • This was really informative, but doesn’t tell us how we can edit the output in a way that we can manually control. For example if I didn’t want to show all of the EXIF variables, only a select few.

    http://neonglow.net/exif.php

    Any information on how I can re-style that mess would be so appreciated!

  • This was really informative, but doesn't tell us how we can edit the output in a way that we can manually control. For example if I didn't want to show all of the EXIF variables, only a select few.

    http://neonglow.net/exif.php

    Any information on how I can re-style that mess would be so appreciated!

  • Djar

    Perhaps the script below could help, it shows how to extract single items.

    <?php
    $file = "test.jpg"; // Change this to your own image file path

    $exif = exif_read_data($file, 0, true);

    /*
    So far i noticed, there are 7 entities:
    (FILE – COMPUTED – IFD0 – THUMBNAIL – COMMENT – EXIF – WINXP)
    Each entity containing there own attributes
    Below some sample code of how to extract a single entity and attribute.
    */

    print " All Entities and Attributes (avaiable for specified document)”;

    foreach($exif as $key => $val)
    {
    print “”.$key.””;
    foreach($val as $singleAttribute => $val2)
    {
    print ““.$singleAttribute.”: “.$val2.””;
    }
    print “”;
    }

    print ” Read Specified Attributes “;

    // WINXP entity attributes can be edited by clicking on your file:
    // Right Mouse Button > Properties > Summary
    // Below you see how to extract single attributes

    // In the first bracket you put the entity name
    // The second bracket the attribute name
    print “Model: “.$exif[“IFD0”][“Model”].””;
    print “Flash: “.$exif[“EXIF”][“Flash”].””;
    print “Compression: “.$exif[“THUMBNAIL”][“Compression”].””;
    print “Title: “.$exif[“WINXP”][“Title”].””;
    print “Html: “.$exif[“COMPUTED”][“html”].””;
    print “Mime-Type: “.$exif[“FILE”][“MimeType”].””;
    ?>

  • Djar

    Sometime went wrong with my previous comment.

  • Djar

    Perhaps the script below could help, it shows how to extract single items.

    <?php
    $file = “test.jpg”; // Change this to your own image file path

    $exif = exif_read_data($file, 0, true);

    /*
    So far i noticed, there are 7 entities:
    (FILE – COMPUTED – IFD0 – THUMBNAIL – COMMENT – EXIF – WINXP)
    Each entity containing there own attributes
    Below some sample code of how to extract a single entity and attribute.
    */

    print “<h1> All Entities and Attributes (avaiable for specified document)</h1>”;

    foreach($exif as $key => $val)
    {
    print “<h3>”.$key.”</h3>
    “;
    foreach($val as $singleAttribute => $val2)
    {
    print ““.$singleAttribute.”: “.$val2.”
    “;
    }
    print “


    “;
    }

    print “<h1> Read Specified Attributes </h1>”;

    // WINXP entity attributes can be edited by clicking on your file:
    // Right Mouse Button > Properties > Summary
    // Below you see how to extract single attributes

    // In the first bracket you put the entity name
    // The second bracket the attribute name
    print “Model: “.$exif[“IFD0”][“Model”].”
    “;
    print “Flash: “.$exif[“EXIF”][“Flash”].”
    “;
    print “Compression: “.$exif[“THUMBNAIL”][“Compression”].”
    “;
    print “Title: “.$exif[“WINXP”][“Title”].”
    “;
    print “Html: “.$exif[“COMPUTED”][“html”].”
    “;
    print “Mime-Type: “.$exif[“FILE”][“MimeType”].”
    “;
    ?>

  • Djar

    Sometime went wrong with my previous comment.

  • Anonymous

    hi, everything works fine thanks for this…. but some of my photos are giving trouble. I get this error:

    Warning: exif_read_data(indirajoshi_001.jpg) [function.exif-read-data]: Incorrect APP1 Exif Identifier Code in c:documents and settingsUSERmy documentsniceheader.php on line 36

  • swapnet

    hi, everything works fine thanks for this…. but some of my photos are giving trouble. I get this error:

    Warning: exif_read_data(indirajoshi_001.jpg) [function.exif-read-data]: Incorrect APP1 Exif Identifier Code in c:documents and settingsUSERmy documentsniceheader.php on line 36

  • John Chandler

    I guess you just ignore that error by :

    error_reporting(0);print_r(exif_read_data(“/home3/xxxx/IMG_4361.JPG”));error_reporting(E_ALL);

    Am I right?

  • Matt

    Unfortunately, my exif is not available. Do u know how to active the exif extension? 

    thx!