source: extensions/AMetaData/JpegMetaData/Readers/GenericReader.class.php @ 4698

Last change on this file since 4698 was 4698, checked in by grum, 14 years ago

[Plugin:AMetaData] Finished to comment the JpegMetaData classes and rename some methods

  • Property svn:executable set to *
File size: 6.4 KB
Line 
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
73    protected $data = null;
74
75    protected $tagDef = null; // $tagDef must be a KnownTags (or derived) object
76
77    /**
78     * the constructor needs data to parse ; given data must be a String
79     *
80     * @param String $data : data to parse
81     */
82    function __construct($data)
83    {
84      $this->initializeTagDef();
85      $this->data=new Data($data);
86    }
87
88    function __destruct()
89    {
90      unset($this->tagDef);
91      unset($this->data);
92      unset($this->entries);
93    }
94
95    public function toString()
96    {
97      $returned="NbEntries: ".sprintf("%02d", $this->nbEntries);
98      return($returned);
99    }
100
101    /**
102     * returns the number of tags for the structure
103     *
104     * @return Integer
105     */
106    public function getNbTags()
107    {
108      return($this->nbEntries);
109    }
110
111    /**
112     * returns a Tag object from a sequential index
113     * return null if index is out of range
114     *
115     * @return Tag
116     */
117    public function getTag($index)
118    {
119      if($index>=0 and $index<=count($this->entries))
120        return($this->entries[$index]);
121      else
122        return(null);
123    }
124
125    /**
126     * returns an array of Tag objects
127     *
128     * @return Tag[]
129     */
130    public function getTags()
131    {
132      return($this->entries);
133    }
134
135    /**
136     * returns the index of the given Tag, searched with the tag Id
137     * returns -1 if there is no tag with this tagId
138     *
139     * @param String or Integer $tagId : the tag id of the searched tag
140     * @return Integer
141     */
142    public function getTagIndexById($tagId)
143    {
144      for($i=0;$i<count($this->entries);$i++)
145      {
146        if($this->entries[$i] instanceof IfdEntryReader)
147        {
148          if($this->entries[$i]->getTagId()==$tagId)
149          {
150            return($i);
151          }
152        }
153        elseif($this->entries[$i] instanceof Tag)
154        {
155          if($this->entries[$i]->getId()==$tagId)
156          {
157            return($i);
158          }
159        }
160      }
161      return(-1);
162    }
163
164    /**
165     * returns a Tag object, tag is searched with the tag Id
166     * returns null if there is no tag with this tagId
167     *
168     * @param String or Integer $tagId : the tag id of the searched tag
169     * @return Integer
170     */
171    public function getTagById($tagId)
172    {
173      $i=$this->getTagIndexById($tagId);
174      if($i>=0)
175      {
176        return($this->entries[$i]);
177      }
178      return(null);
179    }
180
181    /**
182     * returns the first Tag object found, tag is searched with the tag name
183     * returns null if there is no tag with this tagId
184     *
185     * @param String $name : the name of the searched tag
186     * @return Integer
187     */
188    public function getTagByName($name)
189    {
190      for($i=0;$i<count($this->entries);$i++)
191      {
192        if($this->entries[$i] instanceof IfdEntryReader)
193        {
194          if($this->entries[$i]->getTag()->getName()==$name)
195          {
196            return($this->entries[$i]->getTag());
197          }
198        }
199        elseif($this->entries[$i] instanceof Tag)
200        {
201          if($this->entries[$i]->getName()==$name)
202          {
203            return($this->entries[$i]);
204          }
205        }
206      }
207      return(null);
208    }
209
210    /**
211     * must be overrided by derived class
212     * this function is called in the constructor to initialiaze the tags
213     * definitions (tagDef is instanciation of a KnownTag derived class)
214     */
215    protected function initializeTagDef()
216    {
217    }
218
219
220    /**
221     * skip header, if any
222     * (used by maker sub IFD classes)
223     *
224     * @param ULong $headerSize (optional) : size of header to skip
225     */
226    protected function skipHeader($headerSize=0)
227    {
228      $this->data->seek($headerSize);
229    }
230
231
232  }
233
234
235
236
237?>
Note: See TracBrowser for help on using the repository browser.