Get photo EXIF data with PHP

The EXIF data of a photo contains a lot of information, time taken, device taken with, geolocation, even a thumbnail for your use. Best part of all is that it requires no work on you or your users part to compile the data, just read it and fill the database as needed. Today I’m going to talk about how you can grab this information via PHP.

The Function

PHP has a function built in to handle this operation, read_exif_data.

To get the most bang for your buck your going to want to use it like this:

The first variable is the location of the image, pretty straight forward. The second variable is a list EXIF data sections, comma separated, that must be present for the function to return an array, leaving it as NULL will make the function give you an  array as long as there is any EXIF data. The third variable determines if the function should keep processing after the first level, pulling nested arrays for the entire data set. And finally the last variable tells the function to pull the thumbnail in the EXIF data (if present). After that the script will return an array containing all the information possible, which were placing in the $exif variable.

The Output

Now that we have the data we can use it, the easiest way to see what fields we have to play with is to print_r() the array. I did it to a random image I had lying around and came up with this.

Note: I tried to find an image with geodata but I don’t appear to have any.

As you can see theres a lot of information available. Some of the more interesting points are [COMPUTED] whcih contains the image size and type, including th size in a html friendly format, and [IFD0] which contains the Camera Make and Model, the Artist (not in this particular photo), Software used for any post-processing, and the time the image was taken. Another useful section is the [THUMBNAIL]. I commented it out of the above because it would just make things a mess, I’ll explain below.

The Thumbnail

The reason I removed the thumbnail above is simple, it’s in raw code format, meaning that it’s a mess to look at. This doesn’t mean it’s useless however, we just need to do a little formatting first. The simplest way to turn that thumbnail into a viewable image would be the following:

What your doing there is converting the raw data into a data URI, displaying the image without the need of creating a file server side (it also prevents additional HTTP requests, which is very useful when trying to speed up a site, but for an image that that sites it’s also somewhat impractical). Not every image has a thumbnail, in fact most don’t seem to (at least in my experience) but for the ones that do it’s a quick way to grab it without having to generate one yourself.

The Conclusion

Now that you’ve got all the EXIF data you can do what you want with it, some of it would do well to be stored in a database for quick, efficient searching, other parts are generally pretty useless (unless your doing something specific). One thing to keep in mind, you can only pull EXIF data from TIFF and JPEG images, it’s not included in images in other formats (well, it is but it’s not compatible with the PHP function used above). If you have any questions let me know here or at the appropriate Google + post.

6 Replies to “Get photo EXIF data with PHP”

  1. hello,

    i’m using exif_read_data() in this tutorial and its work,

    but how can I distinguish an image/picture that was editor or not,

     

    thanks

    1. You can’t, not with this. The best you can do is determine if it was last saved by Photoshop, but that does not tell you if it has been manipulated in any meaningful way.

  2. Hi,

    Thank you for your article

    I have a gallerie of pictures and I would like to display under each picture a simple link (a href) to check localization on google maps.

    For that, I would like that a code check inside the photo the exif data to extract the GPS information and then create a link for that.  It could be for me a simple shortcode to put in my template under each picture objetc…

    Similar thing that I need is here :
    https://wordpress.org/plugins/geotagphoto/

    Do you think you could help me to do that please ?

    Best regards,

    Olive

    1. I don’t have anything pre-made for this but it should be easy. Keep in mind that a lot of image services will strip the metadata out, including the GPS information.

  3. Hey Phillip,

     

    Could I use this to catalogue the photos I add to my blog?

    For example, I choose three photos I want to share. Could this script check the exif data, and add a title to the blog post with that data? Kind of like how Apple Photos catalogues photos based on location?

    Thanks, Paul

    1. You could use that to get the geolocation if the photos provide it, but you’d have to take the coordinates and run them through google location services API (or similar) to get something more title-friendly. I don’t know of any current plugin that does this.

Leave a Reply

Your email address will not be published. Required fields are marked *