1 | <?php |
---|
2 | /* |
---|
3 | * --:: JPEG MetaDatas ::------------------------------------------------------- |
---|
4 | * |
---|
5 | * Author : Grum |
---|
6 | * email : grum at piwigo.org |
---|
7 | * website : http://photos.grum.fr |
---|
8 | * |
---|
9 | * << May the Little SpaceFrog be with you ! >> |
---|
10 | * |
---|
11 | * |
---|
12 | * +-----------------------------------------------------------------------+ |
---|
13 | * | JpegMetaData - a PHP based Jpeg Metadata manager | |
---|
14 | * +-----------------------------------------------------------------------+ |
---|
15 | * | Copyright(C) 2010 Grum - http://www.grum.fr | |
---|
16 | * +-----------------------------------------------------------------------+ |
---|
17 | * | This program is free software; you can redistribute it and/or modify | |
---|
18 | * | it under the terms of the GNU General Public License as published by | |
---|
19 | * | the Free Software Foundation | |
---|
20 | * | | |
---|
21 | * | This program is distributed in the hope that it will be useful, but | |
---|
22 | * | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
23 | * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
24 | * | General Public License for more details. | |
---|
25 | * | | |
---|
26 | * | You should have received a copy of the GNU General Public License | |
---|
27 | * | along with this program; if not, write to the Free Software | |
---|
28 | * | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
29 | * | USA. | |
---|
30 | * +-----------------------------------------------------------------------+ |
---|
31 | * |
---|
32 | * |
---|
33 | * ----------------------------------------------------------------------------- |
---|
34 | * |
---|
35 | * The GenericReader is a generic class providing methods for tags management |
---|
36 | * |
---|
37 | * ----------------------------------------------------------------------------- |
---|
38 | * |
---|
39 | * .. Notes .. |
---|
40 | * |
---|
41 | * This class is not declared as an abstract class, but it's not recommanded to |
---|
42 | * create object with it. |
---|
43 | * |
---|
44 | * |
---|
45 | * Derived classes : |
---|
46 | * - IfdReader (for exif tags) |
---|
47 | * - GpsReader (for exif GPS tags, in fact derived from IfdReader) |
---|
48 | * - MakerNotesReader (for maker notes tags, in fact derived from IfdReader) |
---|
49 | * - PentaxReader (for Pentax exif tags, in fact derived from MakerNotesReader) |
---|
50 | * - IptcReader (for iptc tags) |
---|
51 | * - XmpReader (for xmp tags) |
---|
52 | * |
---|
53 | * |
---|
54 | * This class provides theses public functions : |
---|
55 | * - getNbTags |
---|
56 | * - getTag |
---|
57 | * - getTags |
---|
58 | * - getTagIndexById |
---|
59 | * - getTagById |
---|
60 | * - getTagByName |
---|
61 | * |
---|
62 | * ----------------------------------------------------------------------------- |
---|
63 | */ |
---|
64 | |
---|
65 | require_once(JPEG_METADATA_DIR."Common/Data.class.php"); |
---|
66 | |
---|
67 | |
---|
68 | class GenericReader |
---|
69 | { |
---|
70 | protected $nbEntries = 0; |
---|
71 | protected $entries = Array(); |
---|
72 | protected $headerSize = 0; |
---|
73 | |
---|
74 | protected $data = null; |
---|
75 | |
---|
76 | protected $tagDef = null; // $tagDef must be a KnownTags (or derived) object |
---|
77 | |
---|
78 | /** |
---|
79 | * the constructor needs data to parse ; given data must be a String |
---|
80 | * |
---|
81 | * @param String $data : data to parse |
---|
82 | */ |
---|
83 | function __construct($data) |
---|
84 | { |
---|
85 | $this->initializeTagDef(); |
---|
86 | $this->data=new Data($data); |
---|
87 | } |
---|
88 | |
---|
89 | function __destruct() |
---|
90 | { |
---|
91 | unset($this->tagDef); |
---|
92 | unset($this->data); |
---|
93 | unset($this->entries); |
---|
94 | } |
---|
95 | |
---|
96 | public function toString() |
---|
97 | { |
---|
98 | $returned="NbEntries: ".sprintf("%02d", $this->nbEntries); |
---|
99 | return($returned); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * returns the number of tags for the structure |
---|
104 | * |
---|
105 | * @return Integer |
---|
106 | */ |
---|
107 | public function getNbTags() |
---|
108 | { |
---|
109 | return($this->nbEntries); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * returns a Tag object from a sequential index |
---|
114 | * return null if index is out of range |
---|
115 | * |
---|
116 | * @return Tag |
---|
117 | */ |
---|
118 | public function getTag($index) |
---|
119 | { |
---|
120 | if($index>=0 and $index<=count($this->entries)) |
---|
121 | return($this->entries[$index]); |
---|
122 | else |
---|
123 | return(null); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * returns an array of Tag objects |
---|
128 | * |
---|
129 | * @return Tag[] |
---|
130 | */ |
---|
131 | public function getTags() |
---|
132 | { |
---|
133 | return($this->entries); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * returns the index of the given Tag, searched with the tag Id |
---|
138 | * returns -1 if there is no tag with this tagId |
---|
139 | * |
---|
140 | * @param String or Integer $tagId : the tag id of the searched tag |
---|
141 | * @return Integer |
---|
142 | */ |
---|
143 | public function getTagIndexById($tagId) |
---|
144 | { |
---|
145 | for($i=0;$i<count($this->entries);$i++) |
---|
146 | { |
---|
147 | if($this->entries[$i] instanceof IfdEntryReader) |
---|
148 | { |
---|
149 | if($this->entries[$i]->getTagId()==$tagId) |
---|
150 | { |
---|
151 | return($i); |
---|
152 | } |
---|
153 | } |
---|
154 | elseif($this->entries[$i] instanceof Tag) |
---|
155 | { |
---|
156 | if($this->entries[$i]->getId()==$tagId) |
---|
157 | { |
---|
158 | return($i); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | return(-1); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * returns a Tag object, tag is searched with the tag Id |
---|
167 | * returns null if there is no tag with this tagId |
---|
168 | * |
---|
169 | * @param String or Integer $tagId : the tag id of the searched tag |
---|
170 | * @return Integer |
---|
171 | */ |
---|
172 | public function getTagById($tagId) |
---|
173 | { |
---|
174 | $i=$this->getTagIndexById($tagId); |
---|
175 | if($i>=0) |
---|
176 | { |
---|
177 | return($this->entries[$i]); |
---|
178 | } |
---|
179 | return(null); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * returns the first Tag object found, tag is searched with the tag name |
---|
184 | * returns null if there is no tag with this tagId |
---|
185 | * |
---|
186 | * @param String $name : the name of the searched tag |
---|
187 | * @return Integer |
---|
188 | */ |
---|
189 | public function getTagByName($name) |
---|
190 | { |
---|
191 | for($i=0;$i<count($this->entries);$i++) |
---|
192 | { |
---|
193 | if($this->entries[$i] instanceof IfdEntryReader) |
---|
194 | { |
---|
195 | if($this->entries[$i]->getTag()->getName()==$name) |
---|
196 | { |
---|
197 | return($this->entries[$i]->getTag()); |
---|
198 | } |
---|
199 | } |
---|
200 | elseif($this->entries[$i] instanceof Tag) |
---|
201 | { |
---|
202 | if($this->entries[$i]->getName()==$name) |
---|
203 | { |
---|
204 | return($this->entries[$i]); |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | return(null); |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * must be overrided by derived class |
---|
213 | * this function is called in the constructor to initialiaze the tags |
---|
214 | * definitions (tagDef is instanciation of a KnownTag derived class) |
---|
215 | */ |
---|
216 | protected function initializeTagDef() |
---|
217 | { |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | /** |
---|
222 | * skip header, if any |
---|
223 | * (used by maker sub IFD classes) |
---|
224 | * |
---|
225 | * @param ULong $headerSize (optional) : size of header to skip |
---|
226 | */ |
---|
227 | protected function skipHeader($headerSize=0) |
---|
228 | { |
---|
229 | $this->data->seek($headerSize); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | ?> |
---|