Tag Archives: exif_read_data

PHP: Checking a Photo’s Image Type before trying to Extract Useable EXIF Data CodeUnit 04 MAY 2011

With EXIF data embedded by almost all digital cameras and mobile phone cameras nowadays, it makes a lot of sense to use this wealth of information in your project when displaying images and their associated information. With PHP’s handy exif_read_data function, grabbing this information becomes a snap – though there is one small caveat: when applied to an image type that doesn’t support EXIF embedding, the function generates a PHP Warning!

Because only really JPG, TIF and RAW files support EXIF, you need to first do an image type check before attempting to extract the embedded EXIF data, which thankfully is pretty simple due to PHP’s exif_imagetype function.

What exif_imagetype does is simply read the first couple of bytes of an image, checks its signature and attempts to report back what it thinks the image is. It supports a number of different image formats, each assigned to a specific IMAGETYPE constant to make it easier to validate image types.

For our purposes, assuming we only allow JPEG photo uploads, our checking code would look like this:

//check that file type is a JPG (IMAGETYPE_JPEG) before exif_read
if (exif_imagetype($filepath) == 2) {
    $exif = exif_read_data($filepath, 0, true);
} else {
    echo 'Photo file type does not support embedded EXIF data';


Related Link: http://www.php.net/manual/en/function.exif-imagetype.php

Display Embedded Photo EXIF data with PHP Programming 16 JAN 2011

Most digital cameras add EXIF (Exchangeable image file format) data to photos captured nowadays, embedding information like camera type, settings, timestamps, etc. into the captured file itself, and in the process creating a host of quite important (to enthusiasts anyway) bits of meta information in the process.

Extracting this information out of photos for display on a website turns out to be particularly easy, thanks to the powerful little exif_read_data function found in PHP.

(Just a note, for the exif family of PHP functions to work, you do need exif enabled in your installation. This can be verified by a quick look through your phpinfo file).

So let’s have an active example then:

if (file_exists($filepath)) {
    echo '