PHP Exif/IFD0 Functions

5th February 2008 - 3 minutes read time

The Exif/IFD0 functions in PHP work with images to pull out meta data associated with them. Most image applications and digital cameras will produce an image with a certain amount of meta data present. This is obvious stuff like file size and creation time stamps, but you can also get stuff like copy right notices, camera name, date picture taken and even things like location if the camera was linked to a GPS system. This meta data can be used to sort or categorise images.

Installation

On Linux systems you must configure exif support with the command --enable-exif when calling the configure script.

On Windows all you have to do is uncomment the lines in your php.ini file for the DLL's php_exif.dll and php_mbstring.dll. However, you must ensure that the php_mbstring.dll DLL is loaded before the php_exif.dll DLL. So you will need to edit your php.ini file so that php_mbstring.dll is located above php_exif.dll.

Use

To read the meta data from an image use the function exif_read_data(). This has various inputs but to print off a list of the different meta data available use the following code.

  1. $exif = exif_read_data('Picture0218.jpg', 0, true); // capture image meta data
  2. echo "Picture0218.jpg<br />";
  3. echo '<table border="1">';
  4. foreach ( $exif as $key => $section ) {
  5. foreach($section as $name => $val){
  6. echo '<tr><td>'.$key.'</td><td>'.$name.'</td><td>'.$val.'</td></tr>';
  7. };
  8. };
  9. echo '</table>';

This will print off a table containing all of the meta data available to PHP. To access an individual component you can access it through an associative array. The following code will print off the date and time that a picture was taken.

echo $exif['IFD0']['DateTime'];

It is also possible to retrieve thumbnail information from the image (if the image contains it) by using the exif_read_data() function with the following parameters.

$exif = exif_read_data('Picture0218.jpg', 0, true, true);

You can pick out the thumbnail information using this:

$image = $exif['THUMBNAIL']['THUMBNAIL'];

Alternatively, you could use the exif_thumbnail() function which is a handy way of getting the thumbnail of any image. It takes the image name as the first parameter and three optional parameters which are set by the function.

  1. $image = exif_thumbnail('Picture0218.jpg', $width, $height, $type);
  2.  
  3. if($image!==false){
  4. header('Content-type:' .image_type_to_mime_type($type));
  5. echo $image;
  6. exit;
  7. }else{
  8. // no thumbnail available, handle the error here
  9. echo 'No thumbnail available';
  10. }

You can then use the $width, $height and $type variables later on in your script.

Add new comment

The content of this field is kept private and will not be shown publicly.