Notes

User Contributed Notes 17 notes

The following code embeds both IPTC APP segment 13 and EXIF APP segment 1 data from a source file and embeds it into a destination file. This overcomes the limitation where the iptcembed statement does not seem to embed EXIF data, only IPTC data.

Windows makes a distinction between 'text' and 'binary' files. So if you run the above code on a windows platform, it will produce a garbled image. To overcome this problem put the file mode in fopen() as 'wb' instead of 'w'.

I've spent a whole day debugging my code (that was based on the example below, posted by knut) until I found out, that iptcembed() only works if the image specified in jpeg_file_name already has IPTC fields included.

That means that you cannot write IPTC fields in a jpeg file without preexisting IPTC information in it.

Updating of IPTC fields also worked only with a few files, I don't really know what it depends on whether it works or not. (Well, it depends on the IPTC header, that's for sure :-)

I'm using PHP 4.2.1, maybe this is fixed in more recent versions, but I don't believe...

Nevertheless, here's some piece of code I tried:

I replaced the line

<?$iptc_old = iptcparse ($info["APP13"]);?>

from knut's example below with

<?$iptc_old["2#000"][0] = chr(0) . chr(2);?>

This creates just an empty "Header" according to the IPTC spec. This one also is included when getting IPTC info using iptcparse(). So my intention was to create a fully new header, but when using iptcembed() afterwards, the file size of the new file was a little smaller than the original but without any IPTC info stored in it.

You might have noticed that several metadata fields in Photoshop are not available via IPTC. Also, Photoshop now uses XMP for it's primary metadata, meaning IPTC is only read by Photoshop if XMP is not present.

I have written a library "PHP JPEG Metadata Toolkit" which bypasses this problem as it allows reading, writing and interpreting of virtually any type of metadata, including XMP, IPTC and EXIF.

Reading and changing IPTC make no problem with the PEAR function, but recent Adobe software add XMP datas on JPEG files and read them instead of IPTC datas. If you need to change IPTC on a JPEG file and want Adobe PS7 read them, you have two solutions:- writing XMP and IPTC datas- writing IPTC datas and removing XMP datas

Because I've not enough time to work on XMP datas, I've choosen the second solution. Here is the result of this work:

Example to read IPTC text from an image, changing the text an write to a new file using the functions iptcparse and iptcembed.

Also a list of the most common IPTC fields.

<?// original file name$image_name_old = "test.jpg";

// New file name$image_name_new = "test2.jpg";

// Reads the IPTC text in to the array '$iptc'// The number after the '#' is the IPTC field// Ex: $iptc["2#120"][0] is Caption// $iptc["2#055"][0]; is Creation date$size = GetImageSize ("$image_name_old",&$info);$iptc_old = iptcparse ($info["APP13"]);

// Adding or replacing IPTC text // This ex. replace the original category or create it if it dos not exist $iptc_old["2#015"][0] = "Sport"; // .. and adding more text to the original caption $iptc_old["2#120"][0] .= " More caption text";

To remove all EXIF, XMP etc. tags from a jpeg file you need no resampling (which, by the way, may give you memory problems). It's just enough to recreate the image, presumably with 100% quality in order not to loose anything. The code is as simple as that: