PHP Exif/IFD0 Functions

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.

$exif = exif_read_data('Picture0218.jpg', 0, true); // capture image meta data
echo "Picture0218.jpg<br />";
echo '<table border="1">';
foreach ( $exif as $key => $section ) {
  foreach($section as $name => $val){
    echo '<tr><td>'.$key.'</td><td>'.$name.'</td><td>'.$val.'</td></tr>';
  };
};
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.

$image = exif_thumbnail('Picture0218.jpg', $width, $height, $type);
 
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';
}

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.
CAPTCHA
1 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.