c7deb2a43a4bc68c9f5ae86bd03591938f27d136
[yaffs-website] / vendor / lsolesen / pel / src / PelExif.php
1 <?php
2
3 /**
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.
7  *
8  * Copyright (C) 2004, 2005 Martin Geisler.
9  *
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.
14  *
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.
19  *
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
24  */
25 namespace lsolesen\pel;
26
27 /**
28  * Classes for dealing with Exif data.
29  *
30  * @author Martin Geisler <mgeisler@users.sourceforge.net>
31  * @license http://www.gnu.org/licenses/gpl.html GNU General Public
32  *          License (GPL)
33  * @package PEL
34  */
35
36 /**
37  * Class representing Exif data.
38  *
39  * Exif data resides as {@link PelJpegContent data} and consists of a
40  * header followed by a number of {@link PelJpegIfd IFDs}.
41  *
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
46  * format.
47  *
48  * @author Martin Geisler <mgeisler@users.sourceforge.net>
49  * @package PEL
50  */
51 class PelExif extends PelJpegContent
52 {
53
54     /**
55      * Exif header.
56      *
57      * The Exif data must start with these six bytes to be considered
58      * valid.
59      */
60     const EXIF_HEADER = "Exif\0\0";
61
62     /**
63      * The PelTiff object contained within.
64      *
65      * @var PelTiff
66      */
67     private $tiff = null;
68
69     /**
70      * Construct a new Exif object.
71      *
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}.
76      */
77     public function __construct()
78     {
79         // nothing to be done
80     }
81
82     /**
83      * Load and parse Exif data.
84      *
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.
88      *
89      * @param PelDataWindow $d
90      */
91     public function load(PelDataWindow $d)
92     {
93         Pel::debug('Parsing %d bytes of Exif data...', $d->getSize());
94
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());
98         }
99         /* Verify the Exif header */
100         if ($d->strcmp(0, self::EXIF_HEADER)) {
101             $d->setWindowStart(strlen(self::EXIF_HEADER));
102         } else {
103             throw new PelInvalidDataException('Exif header not found.');
104         }
105
106         /* The rest of the data is TIFF data. */
107         $this->tiff = new PelTiff();
108         $this->tiff->load($d);
109     }
110
111     /**
112      * Change the TIFF information.
113      *
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
116      * another.
117      *
118      * @param PelTiff $tiff
119      *            the new TIFF object.
120      */
121     public function setTiff(PelTiff $tiff)
122     {
123         $this->tiff = $tiff;
124     }
125
126     /**
127      * Get the underlying TIFF object.
128      *
129      * The actual Exif data is stored in a {@link PelTiff} object, and
130      * this method provides access to it.
131      *
132      * @return PelTiff the TIFF object with the Exif data.
133      */
134     public function getTiff()
135     {
136         return $this->tiff;
137     }
138
139     /**
140      * Produce bytes for the Exif data.
141      *
142      * @return string bytes representing this object.
143      */
144     public function getBytes()
145     {
146         return self::EXIF_HEADER . $this->tiff->getBytes();
147     }
148
149     /**
150      * Return a string representation of this object.
151      *
152      * @return string a string describing this object. This is mostly
153      *         useful for debugging.
154      */
155     public function __toString()
156     {
157         return Pel::tra("Dumping Exif data...\n") . $this->tiff->__toString();
158     }
159 }