Thursday, October 21, 2010

Showing image EXIF data with PHP

Today I had an issue that I didn't know how to solve, until I searched the web. I wanted to pull the EXIF data that images contain and display it on the page. Mainly I wanted to show any copyright data.

I found this blog post on v-nessa.net which helped me a lot. Looking at the post, I came up with the below to show the copyright of an image:

$image = "/path/to/image.jpg";
$exif = exif_read_data($image, 0, true);
foreach ($exif as $key => $section) {
    foreach ($section as $name => $val) {
        if ($name == "Copyright") {
            echo "$val";
        }
    }
}

For the exif_read_data function to work, if you're using Windows, you need to go into your php.ini file, make sure both php_mbstring.dll and php_exif.dll are not commented out, and make sure that php_mbstring.dll is listed above php_exif.dll so it's loaded first.

If you wanted to show all of the image's EXIF data, in the blog post linked to above they show you how to do that.

No comments: