4 * PEL: PHP Exif Library.
5 * A library with support for reading and
6 * writing all Exif headers in JPEG and TIFF images using PHP.
8 * Copyright (C) 2004, 2005 Martin Geisler.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program in the file COPYING; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
25 namespace lsolesen\pel;
28 * Classes for dealing with Exif data.
30 * @author Martin Geisler <mgeisler@users.sourceforge.net>
31 * @license http://www.gnu.org/licenses/gpl.html GNU General Public
37 * Class representing Exif data.
39 * Exif data resides as {@link PelJpegContent data} and consists of a
40 * header followed by a number of {@link PelJpegIfd IFDs}.
42 * The interesting method in this class is {@link getTiff()} which
43 * will return the {@link PelTiff} object which really holds the data
44 * which one normally think of when talking about Exif data. This is
45 * because Exif data is stored as an extension of the TIFF file
48 * @author Martin Geisler <mgeisler@users.sourceforge.net>
51 class PelExif extends PelJpegContent
57 * The Exif data must start with these six bytes to be considered
60 const EXIF_HEADER = "Exif\0\0";
63 * The PelTiff object contained within.
70 * Construct a new Exif object.
72 * The new object will be empty --- use the {@link load()} method to
73 * load Exif data from a {@link PelDataWindow} object, or use the
74 * {@link setTiff()} to change the {@link PelTiff} object, which is
75 * the true holder of the Exif {@link PelEntry entries}.
77 public function __construct()
83 * Load and parse Exif data.
85 * This will populate the object with Exif data, contained as a
86 * {@link PelTiff} object. This TIFF object can be accessed with
87 * the {@link getTiff()} method.
89 * @param PelDataWindow $d
91 public function load(PelDataWindow $d)
93 Pel::debug('Parsing %d bytes of Exif data...', $d->getSize());
95 /* There must be at least 6 bytes for the Exif header. */
96 if ($d->getSize() < 6) {
97 throw new PelInvalidDataException('Expected at least 6 bytes of Exif ' . 'data, found just %d bytes.', $d->getSize());
99 /* Verify the Exif header */
100 if ($d->strcmp(0, self::EXIF_HEADER)) {
101 $d->setWindowStart(strlen(self::EXIF_HEADER));
103 throw new PelInvalidDataException('Exif header not found.');
106 /* The rest of the data is TIFF data. */
107 $this->tiff = new PelTiff();
108 $this->tiff->load($d);
112 * Change the TIFF information.
114 * Exif data is really stored as TIFF data, and this method can be
115 * used to change this data from one {@link PelTiff} object to
118 * @param PelTiff $tiff
119 * the new TIFF object.
121 public function setTiff(PelTiff $tiff)
127 * Get the underlying TIFF object.
129 * The actual Exif data is stored in a {@link PelTiff} object, and
130 * this method provides access to it.
132 * @return PelTiff the TIFF object with the Exif data.
134 public function getTiff()
140 * Produce bytes for the Exif data.
142 * @return string bytes representing this object.
144 public function getBytes()
146 return self::EXIF_HEADER . $this->tiff->getBytes();
150 * Return a string representation of this object.
152 * @return string a string describing this object. This is mostly
153 * useful for debugging.
155 public function __toString()
157 return Pel::tra("Dumping Exif data...\n") . $this->tiff->__toString();