• 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

Reading IPTC IIM/headers

IPTC is a trickier beast from a PHP perspective. We’ve been given two functions to work with, iptcparse() and iptcembed().

iptcparse() is almost as easy as exif_read_data(), but unfortunately it requires more pre-work than exif_read_data(). JPEG saves application data in markers named APPn where n is a number. IPTC is most often (almost always) saved in APP13. To get this IPTC binary blob, we can use the slightly awkwardly named function getimagesize()‘s second(!) parameter.

$info = array();                      
getimagesize('05042008042.jpg', $info);
print_r(array_keys($info));
Array
(
    [0] => APP1
)

Two things to note:

  1. I only print the array keys, because APPn markers holds binary data which makes little sense to print out.
  2. My image only contains APP1 (which by coincidence is where Exif is usually stored), but no APP13. This is actually expected because IPTC is “human” (as mentioned near the start of the article) and thus not present without someone adding it.

Now if our image had IPTC data, we could do something like this.

$info = array();                      
getimagesize('05042008042.jpg', $info);
if(isset($info['APP13']))
{
    $iptc = iptcparse($info['APP13']);
    print_r($iptc);
}
Array
(
    [2#080] => Array
        (
            [0] => ccc
        )

    [2#110] => Array
        (
            [0] => ccc
        )

    [2#115] => Array
        (
            [0] => ccc
        )

    [2#118] => Array
        (
            [0] => ddd
        )

    [2#120] => Array
        (
            [0] => aaa
        )
)

This is from a test image I used at work, so the contents looks pretty cryptic or nonsense (aaa, bbb, ccc), but these fields are author, author, author, contact and caption in clear text, I just didn’t add any sensible data. The array indices may also seem a bit cryptic, but the number before the hash refers to the application record no. 2 (mentioned on previous page), and the number behind the hash refers to a predefined index which you find in the IPTC IIM specification.

Turn the page for how to write IPTC headers

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...