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:

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.