Using PEL in applications
Learn how to load, edit, and save images
Martin
Geisler
{@link mailto:mgeisler@users.sourceforge.net
mgeisler@users.sourceforge.net}
{@toc}
Loading a JPEG image
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 {@link 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 {@link PelJpeg::getExif()} method:
getExif();
]]>
The section stored in $exif is now a
{@link PelExif} object.
If the JPEG image does not contain Exif information, then
the $exif variable will be
null.
Obtaining the TIFF data
The Exif data is not stored directly in this object, instead
it is stored in a {@link PelTiff} object, which can be retrieved
using the {@link PelExif::getTiff() getTiff()} method:
getTiff();
]]>
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 {@link PelExif} object
contain a {@link PelTiff} object.
TIFF data is organized as a chain of Image File Directories
(IFDs), each represented by a {@link PelIfd} object. Each IFD has
a number of entries ({@link PelEntry} objects) which one can get
hold of using the {@link PelIfd::getEntry() getEntry()}
method.
The first IFD, number zero, will normally contain the
{@link PelTag::IMAGE_DESCRIPTION IMAGE_DESCRIPTION} tag. The
following code will initialize $ifd0 with the
first IFD, and $desc with the
description:
getIfd();
$desc = $ifd0->getEntry(PelTag::IMAGE_DESCRIPTION);
]]>
Now $desc will contain a
{@link PelEntryAscii} object holding the description. Each entry
is represented using an object of a class descendent of
{@link PelEntry}. There are classes for numbers such as
{@link PelEntryShort} for small numbers and {@link PelEntryLong}
for big numbers, and more specialized classes, such as
{@link PelEntryVersion} for version numbers, {@link PelEntryTime}
for date and time, and so on.
Reading Values
The value of any entry can be retrieved by calling the
{@link PelEntry::getValue() getValue()} method on the object.
Doing this on $desc will return a string, while
doing it on a {@link PelEntryShort} will normally return an
integer (see {@link PelEntryNumber::getValue() the documentation}
for the full story). So to echo the description one simply
does:
getValue();
]]>
Writing Values
Writing new values (changing values) to an entry is just as
easy as reading values, one just uses the
{@link PelEntry::setValue() setValue()} method on the entry in
question.
Continuing on our example from before where
$desc contains a {@link PelEntryAscii} object
with the description for the image, one changes the description
with:
setValue('The new description.');
]]>
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.
Saving an Image
After having changed an image, one would probably want to
save it to keep the changes.
{@link PelJpeg} objects (and {@link PelTiff} objects) can be
turned back into bytes with the {@link PelJpeg::getBytes()
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:
saveFile('new-' . $filename);
]]>