Previous | Up | Next |
PHP Exif Library | PHP Exif Library | Frequently Asked Questions (FAQ) |
The typical use for PEL is to read and write data from JPEG images. Such an image is represented in PEL using an object of the PelJpeg class. With the filename of a JPEG image stored in the variable $filename, then it is a simple matter of creating a PelJpeg object:
If this succeeded without any exceptions being thrown, one can proceed to find the Exif data itself. The Exif data is retrieved using the PelJpeg::getExif() method:
The section stored in $exif is now a PelExif object.
If the JPEG image does not contain Exif information, then
the $exif variable will be
null
.
The Exif data is not stored directly in this object, instead it is stored in a PelTiff object, which can be retrieved using the getTiff() method:
This peculiar step is necessary because what one normally thinks of as Exif data is really just an extension of the TIFF standard. PEL models this by having the PelExif object contain a PelTiff object.
TIFF data is organized as a chain of Image File Directories (IFDs), each represented by a PelIfd object. Each IFD has a number of entries (PelEntry objects) which one can get hold of using the getEntry() method.
The first IFD, number zero, will normally contain the IMAGE_DESCRIPTION tag. The following code will initialize $ifd0 with the first IFD, and $desc with the description:
Now $desc will contain a PelEntryAscii object holding the description. Each entry is represented using an object of a class descendent of PelEntry. There are classes for numbers such as PelEntryShort for small numbers and PelEntryLong for big numbers, and more specialized classes, such as PelEntryVersion for version numbers, PelEntryTime for date and time, and so on.
The value of any entry can be retrieved by calling the getValue() method on the object. Doing this on $desc will return a string, while doing it on a PelEntryShort will normally return an integer (see the documentation for the full story). So to echo the description one simply does:
Writing new values (changing values) to an entry is just as easy as reading values, one just uses the setValue() method on the entry in question.
Continuing on our example from before where $desc contains a PelEntryAscii object with the description for the image, one changes the description with:
The object is now updated and is ready to be turned back into bytes, so that the image can be saved with the new, updated description.
After having changed an image, one would probably want to save it to keep the changes.
PelJpeg objects (and PelTiff objects) can be turned back into bytes with the getBytes method. This will turn the object and all the objects within it into bytes, which can then be saved in a file to produce a JPEG image.
With the image loaded into $jpeg it is a simple matter to write the new JPEG file:
Previous | Up | Next |
PHP Exif Library | PHP Exif Library | Frequently Asked Questions (FAQ) |
Documentation generated on Thu, 05 May 2011 07:18:54 +0200 by phpDocumentor 1.4.3