• Posted by Peter Haza
  • On April 24, 2008

  • Filed under PHP

  • 3 Comments

Working with image meta data in Exif and IPTC headers from PHP

Most people know that when you take a picture a digital camera it writes the image to a file, but quite a few people do not know that there’s usually a lot more data being written to this image file. The extra data being written is meta data (data about data), and it may contain lots of data about the camera and surroundings when the picture was taken. This data is usually written in Exif headers. Addtionally you can add your own data in IPTC headers. Here’s how to work with the two formats in PHP.

Why two formats?

At first glance, the two formats look very similar in their purpose and capabilities, and they are. The main difference is that Exif is a write-once-read-only-format, IPTC is a read-and-write-format. By this it’s meant that Exif is only supposed to be written once, and that’s when the camera takes the picture. After that you’re only supposed to read the data and never modify it.

IPTC on the other hand is an exchange format that removes the need for sending an additional file with meta data. If you look at the IPTC IIM specification you’ll see that you can embed almost any imaginable meta data. In this post I’ll only focus on chapter 6, section 1.4 of the IPTC headers: “Application record No. 2″, which is where you specify most of the “personal stuff”.

This also somewhat implies that Exif headers contain more technical details, while IPTC headers contain more personal/human details.

Exif

Exif is pretty straight forward to work with in PHP. As of PHP >= 4.2.0 you are presented with exif_read_data(), where you specify the path to the file you want to read the meta data from.

print_r(exif_read_data('05042008042.jpg'));
Array
(
    [FileName] => 05042008042.jpg
    [FileDateTime] => 1207409868
    [FileSize] => 791791
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS
    [COMPUTED] => Array
        (
            [html] => width="2592" height="1944"
            [Height] => 1944
            [Width] => 2592
            [IsColor] => 1
            [ByteOrderMotorola] => 0
            [ApertureFNumber] => f/2.8
            [Thumbnail.FileType] => 2
            [Thumbnail.MimeType] => image/jpeg
        )

    [Make] => Nokia
    [Model] => N95
    [Orientation] => 1
    [XResolution] => 300/1
    [YResolution] => 300/1
    [ResolutionUnit] => 2
    [YCbCrPositioning] => 1
    [Exif_IFD_Pointer] => 144
    [GPS_IFD_Pointer] => 2994
    [THUMBNAIL] => Array
        (
            [Compression] => 6
            [XResolution] => 72/1
            [YResolution] => 72/1
            [ResolutionUnit] => 2
            [JPEGInterchangeFormat] => 3294
            [JPEGInterchangeFormatLength] => 18626
        )

    [ExposureTime] => 29000/1000000
    [FNumber] => 28/10
    [ISOSpeedRatings] => 400
    [ExifVersion] => 0220
    [DateTimeOriginal] => 2008:04:05 17:36:05
    [DateTimeDigitized] => 2008:04:05 17:36:05
    [ComponentsConfiguration] => 
    [ShutterSpeedValue] => 5107/1000
    [ApertureValue] => 297/100
    [LightSource] => 4
    [Flash] => 25
    [FocalLength] => 56/10
    [FlashPixVersion] => 0100
    [ColorSpace] => 1
    [ExifImageWidth] => 2592
    [ExifImageLength] => 1944
    [CustomRendered] => 0
    [ExposureMode] => 0
    [WhiteBalance] => 0
    [DigitalZoomRatio] => 100/100
    [SceneCaptureType] => 0
    [GainControl] => 2
    [GPSVersion] => 
    [GPSLatitudeRef] => N
    [GPSLatitude] => Array
        (
            [0] => 59/1
            [1] => 55/1
            [2] => 2883/100
        )

    [GPSLongitudeRef] => E
    [GPSLongitude] => Array
        (
            [0] => 10/1
            [1] => 29/1
            [2] => 3939/100
        )

    [GPSAltitudeRef] => 
    [GPSAltitude] => 14750/100
    [GPSTimeStamp] => Array
        (
            [0] => 0/1
            [1] => 0/1
            [2] => 0/1
        )
    [GPSDateStamp] => 0000:01:01
)

That’s a lot of data, and unless you’re a photographer probably only understand parts of it. Anyway. This was shot with my Nokia N95 with LocationTagger enabled, so I get the extra GPS data embedded as well. Now, to pick any of this data you just go to the index in your associative array. For instance if I want read the date and time my picture was shot, I get it like this:

$info = exif_read_data('05042008042.jpg');
echo 'Image was taken at: '.$info['DateTimeOriginal'];

Straight forward and easy, time to have a look at IPTC headers on the next page.

Pages: 1 2 3

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

3 comments

  1. Posted by Shay 25th April, 2008 at 05:19 pm |

    another awesome article. Keep them coming, I’m learning lots from your site :)

  2. Posted by Brad 23rd September, 2009 at 10:20 pm |

    I want to make a webpage Gallery of a Directory of JPEGs (on my LINUX/Apache host), showing the above THUMBNAILs… via a web page, much like OSX or WIN shows thumbs of image files in Directories on my local comp.

  3. Posted by Anh Nguyen 19th July, 2010 at 07:05 am |

    Very nice article! By the way, do you know anyway to modify the exif of an image with PHP?

What do you think? Join the discussion...