Source for file PelEntry.php

Documentation is available at PelEntry.php

  1. <?php
  2.  
  3. /* PEL: PHP EXIF Library. A library with support for reading and
  4. * writing all EXIF headers in JPEG and TIFF images using PHP.
  5. *
  6. * Copyright (C) 2004 Martin Geisler <gimpster@users.sourceforge.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program in the file COPYING; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  21. * Boston, MA 02111-1307 USA
  22. */
  23.  
  24. /* PelEntry.php,v 1.14 2004/07/21 16:23:12 gimpster Exp */
  25.  
  26.  
  27. /**
  28. * Classes for dealing with EXIF entries.
  29. *
  30. * This file defines two exception classes and the abstract class
  31. * {@link PelEntry} which provides the basic methods that all EXIF
  32. * entries will have. All EXIF entries will be represented by
  33. * descendants of the {@link PelEntry} class.
  34. *
  35. * @author Martin Geisler <gimpster@users.sourceforge.net>
  36. * @version 1.14
  37. * @date 2004/07/21 16:23:12
  38. * @license http://www.gnu.org/licenses/gpl.html GNU General Public
  39. * License (GPL)
  40. * @package PEL
  41. */
  42.  
  43. /**#@+ Required class definitions. */
  44. ('PelDataWindow.php');
  45. require_once('PelException.php');
  46. require_once('PelFormat.php');
  47. require_once('PelTag.php');
  48. require_once('Pel.php');
  49. /**#@-*/ * @author Martin Geisler <gimpster@users.sourceforge.net>
  50. * @package PEL
  51. * @subpackage Exception
  52. */
  53. class PelUnexpectedFormatException extends PelException {
  54.  
  55. /**
  56. * Construct a new exception indicating an invalid format.
  57. *
  58. * @param PelFormat the format found.
  59. *
  60. * @param PelFormat the expected format.
  61. */
  62. function __construct($found, $expected) {
  63. parent::__construct('Unexpected format found: %s. Expected %s instead.',
  64. PelFormat::getName($found),
  65. PelFormat::getName($expected));
  66. }
  67. }
  68.  
  69.  
  70. /**
  71. * @author Martin Geisler <gimpster@users.sourceforge.net>
  72. * @package PEL
  73. * @subpackage Exception
  74. */
  75. class PelWrongComponentCountException extends PelException {
  76.  
  77. /**
  78. * Construct a new exception indicating a wrong number of
  79. * components.
  80. *
  81. * Some tags have strict limits as to the allowed number of
  82. * components, and this exception is thrown if a tag violates such a
  83. * constraint.
  84. *
  85. * @param int the number of components found.
  86. *
  87. * @param int the expected number of components.
  88. */
  89. function __construct($found, $expected) {
  90. parent::__construct('Wrong number of components found: %d. Expected %d.',
  91. $found, $expected);
  92. }
  93. }
  94.  
  95.  
  96. /**
  97. * @author Martin Geisler <gimpster@users.sourceforge.net>
  98. * @package PEL
  99. */
  100. abstract class PelEntry {
  101.  
  102. /**
  103. * The bytes representing this entry.
  104. *
  105. * Subclasses must either override {@link getBytes()} or, if
  106. * possible, maintain this property so that it always contains a
  107. * true representation of the entry.
  108. *
  109. * @var string
  110. */
  111. protected $bytes = '';
  112.  
  113. /**
  114. * The {@link PelTag} of this entry.
  115. *
  116. * @var PelTag
  117. */
  118. protected $tag;
  119.  
  120. /**
  121. * The {@link PelFormat} of this entry.
  122. *
  123. * @var PelFormat
  124. */
  125. protected $format;
  126.  
  127. /**
  128. * The number of components of this entry.
  129. *
  130. * @var int
  131. */
  132. protected $components;
  133.  
  134.  
  135. /**
  136. * Make a new entry from a bunch of bytes.
  137. *
  138. * This method will create the proper subclass of {@link PelEntry}
  139. * corresponding to the {@link PelTag} and {@link PelFormat} given.
  140. *
  141. * @param PelTag the tag of the entry.
  142. *
  143. * @param PelFormat the format of the entry.
  144. *
  145. * @param int the components in the entry.
  146. *
  147. * @param PelDataWindow the data which will be used to construct the
  148. * entry.
  149. *
  150. * @return PelEntry a newly created entry, holding the data given.
  151. */
  152. static function newFromData($tag, $format, $components, $data) {
  153.  
  154. /* First handle tags for which we have a specific PelEntryXXX
  155. * class. */
  156. switch ($tag) {
  157. case PelTag::DATE_TIME:
  158. case PelTag::DATE_TIME_ORIGINAL:
  159. case PelTag::DATE_TIME_DIGITIZED:
  160. if ($format != PelFormat::ASCII)
  161. throw new PelUnexpectedFormatException($format, PelFormat::ASCII);
  162.  
  163. if ($components != 20)
  164. throw new PelWrongComponentsCountException($components, 20);
  165.  
  166. /* Split the string into year, month, date, hour, minute, and
  167. * second components. */
  168. $d = explode('-', strtr($data->getBytes(0, -1), '.: ', '---'));
  169. // TODO: handle timezones.
  170. require_once('PelEntryAscii.php');
  171. return new PelEntryTime($tag, gmmktime($d[3], $d[4], $d[5],
  172. $d[1], $d[2], $d[0]));
  173.  
  174. case PelTag::COPYRIGHT:
  175. if ($format != PelFormat::ASCII)
  176. throw new PelUnexpectedFormatException($format, PelFormat::ASCII);
  177. $v = explode("\0", trim($data->getBytes(), ' '));
  178. require_once('PelEntryAscii.php');
  179. return new PelEntryCopyright($v[0], $v[1]);
  180.  
  181. case PelTag::EXIF_VERSION:
  182. case PelTag::FLASH_PIX_VERSION:
  183. case PelTag::INTEROPERABILITY_VERSION:
  184. if ($format != PelFormat::UNDEFINED)
  185. throw new PelUnexpectedFormatException($format, PelFormat::UNDEFINED);
  186.  
  187. require_once('PelEntryUndefined.php');
  188. return new PelEntryVersion($tag, $data->getBytes() / 100);
  189.  
  190. case PelTag::USER_COMMENT:
  191. if ($format != PelFormat::UNDEFINED)
  192. throw new PelUnexpectedFormatException($format, PelFormat::UNDEFINED);
  193.  
  194. require_once('PelEntryUndefined.php');
  195. if ($data->getSize() < 8) {
  196. return new PelEntryUserComment();
  197. } else {
  198. return new PelEntryUserComment($data->getBytes(8),
  199. rtrim($data->getBytes(0, 8)));
  200. }
  201.  
  202. default:
  203. /* Then handle the formats. */
  204. switch ($format) {
  205. case PelFormat::BYTE:
  206. require_once('PelEntryByte.php');
  207. $v = new PelEntryByte($tag);
  208. for ($i = 0; $i < $components; $i++)
  209. $v->addNumber($data->getByte($i));
  210. return $v;
  211.  
  212. case PelFormat::SBYTE:
  213. require_once('PelEntryByte.php');
  214. $v = new PelEntrySByte($tag);
  215. for ($i = 0; $i < $components; $i++)
  216. $v->addNumber($data->getSByte($i));
  217. return $v;
  218.  
  219. case PelFormat::ASCII:
  220. require_once('PelEntryAscii.php');
  221. return new PelEntryAscii($tag, $data->getBytes(0, -1));
  222.  
  223. case PelFormat::SHORT:
  224. require_once('PelEntryShort.php');
  225. $v = new PelEntryShort($tag);
  226. for ($i = 0; $i < $components; $i++)
  227. $v->addNumber($data->getShort($i*2));
  228. return $v;
  229.  
  230. case PelFormat::SSHORT:
  231. require_once('PelEntryShort.php');
  232. $v = new PelEntrySShort($tag);
  233. for ($i = 0; $i < $components; $i++)
  234. $v->addNumber($data->getSShort($i*2));
  235. return $v;
  236.  
  237. case PelFormat::LONG:
  238. require_once('PelEntryLong.php');
  239. $v = new PelEntryLong($tag);
  240. for ($i = 0; $i < $components; $i++)
  241. $v->addNumber($data->getLong($i*4));
  242. return $v;
  243.  
  244. case PelFormat::SLONG:
  245. require_once('PelEntryLong.php');
  246. $v = new PelEntrySLong($tag);
  247. for ($i = 0; $i < $components; $i++)
  248. $v->addNumber($data->getSLong($i*4));
  249. return $v;
  250.  
  251. case PelFormat::RATIONAL:
  252. require_once('PelEntryRational.php');
  253. $v = new PelEntryRational($tag);
  254. for ($i = 0; $i < $components; $i++)
  255. $v->addNumber($data->getRational($i*8));
  256. return $v;
  257.  
  258. case PelFormat::SRATIONAL:
  259. require_once('PelEntryRational.php');
  260. $v = new PelEntrySRational($tag);
  261. for ($i = 0; $i < $components; $i++)
  262. $v->addNumber($data->getSRational($i*8));
  263. return $v;
  264.  
  265. case PelFormat::UNDEFINED:
  266. require_once('PelEntryUndefined.php');
  267. return new PelEntryUndefined($tag, $data->getBytes());
  268.  
  269. default:
  270. throw new PelException('Unsupported format: %s',
  271. PelFormat::getName($format));
  272. }
  273. }
  274. }
  275.  
  276.  
  277. /**
  278. * Return the tag of this entry.
  279. *
  280. * @return PelTag the tag of this entry.
  281. */
  282. function getTag() {
  283. return $this->tag;
  284. }
  285.  
  286.  
  287. /**
  288. * Return the format of this entry.
  289. *
  290. * @return PelFormat the format of this entry.
  291. */
  292. function getFormat() {
  293. return $this->format;
  294. }
  295.  
  296.  
  297. /**
  298. * Return the number of components of this entry.
  299. *
  300. * @return int the number of components of this entry.
  301. */
  302. function getComponents() {
  303. return $this->components;
  304. }
  305.  
  306.  
  307. /**
  308. * Turn this entry into bytes.
  309. *
  310. * @param PelByteOrder the desired byte order, which must be either
  311. * {@link Convert::LITTLE_ENDIAN} or {@link Convert::BIG_ENDIAN}.
  312. *
  313. * @return string bytes representing this entry.
  314. */
  315. function getBytes($o) {
  316. return $this->bytes;
  317. }
  318.  
  319. /**
  320. * Get the value of an entry as text.
  321. *
  322. * The value will be returned in a format suitable for presentation,
  323. * e.g., rationals will be returned as 'x/y', ASCII strings will be
  324. * returned as themselves etc.
  325. *
  326. * @param boolean some values can be returned in a long or more
  327. * brief form, and this parameter controls that.
  328. *
  329. * @return string the value as text.
  330. */
  331. abstract function getText($brief = false);
  332.  
  333.  
  334. /**
  335. /**
  336. * Turn this entry into a string.
  337. *
  338. * @return string a string representation of this entry. This is
  339. * mostly for debugging.
  340. */
  341. function __toString() {
  342. $str = Pel::fmt(" Tag: 0x%04X (%s)\n",
  343. $this->tag, PelTag::getName($this->tag));
  344. $str .= Pel::fmt(" Format : %d (%s)\n",
  345. $this->format, PelFormat::getName($this->format));
  346. $str .= Pel::fmt(" Components: %d\n", $this->components);
  347. $str .= Pel::fmt(" Value : %s\n", $this->getText());
  348. return $str;
  349. }
  350. }
  351.  
  352. ?>

SourceForge.net Logo Documentation generated on Wed, 21 Jul 2004 19:12:58 +0200 by phpDocumentor 1.3.0RC3