PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/slir/pel-0.9.2/src/PelEntry.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 382 lines | 78 code | 47 blank | 257 comment | 3 complexity | 526a7bd8f98c0c87f0acbfa4d229fb08 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /* PEL: PHP Exif Library. A library with support for reading and
  3. * writing all Exif headers in JPEG and TIFF images using PHP.
  4. *
  5. * Copyright (C) 2004, 2005, 2006 Martin Geisler.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program in the file COPYING; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301 USA
  21. */
  22. /* $Id$ */
  23. /**
  24. * Classes for dealing with Exif entries.
  25. *
  26. * This file defines two exception classes and the abstract class
  27. * {@link PelEntry} which provides the basic methods that all Exif
  28. * entries will have. All Exif entries will be represented by
  29. * descendants of the {@link PelEntry} class --- the class itself is
  30. * abstract and so it cannot be instantiated.
  31. *
  32. * @author Martin Geisler <mgeisler@users.sourceforge.net>
  33. * @version $Revision$
  34. * @date $Date$
  35. * @license http://www.gnu.org/licenses/gpl.html GNU General Public
  36. * License (GPL)
  37. * @package PEL
  38. */
  39. /**#@+ Required class definitions. */
  40. require_once('PelException.php');
  41. require_once('PelFormat.php');
  42. require_once('PelTag.php');
  43. require_once('Pel.php');
  44. /**#@-*/
  45. /**
  46. * Exception indicating a problem with an entry.
  47. *
  48. * @author Martin Geisler <mgeisler@users.sourceforge.net>
  49. * @package PEL
  50. * @subpackage Exception
  51. */
  52. class PelEntryException extends PelException {
  53. /**
  54. * The IFD type (if known).
  55. *
  56. * @var int
  57. */
  58. protected $type;
  59. /**
  60. * The tag of the entry (if known).
  61. *
  62. * @var PelTag
  63. */
  64. protected $tag;
  65. /**
  66. * Get the IFD type associated with the exception.
  67. *
  68. * @return int one of {@link PelIfd::IFD0}, {@link PelIfd::IFD1},
  69. * {@link PelIfd::EXIF}, {@link PelIfd::GPS}, or {@link
  70. * PelIfd::INTEROPERABILITY}. If no type is set, null is returned.
  71. */
  72. function getIfdType() {
  73. return $this->type;
  74. }
  75. /**
  76. * Get the tag associated with the exception.
  77. *
  78. * @return PelTag the tag. If no tag is set, null is returned.
  79. */
  80. function getTag() {
  81. return $this->tag;
  82. }
  83. }
  84. /**
  85. * Exception indicating that an unexpected format was found.
  86. *
  87. * The documentation for each tag in {@link PelTag} will detail any
  88. * constrains.
  89. *
  90. * @author Martin Geisler <mgeisler@users.sourceforge.net>
  91. * @package PEL
  92. * @subpackage Exception
  93. */
  94. class PelUnexpectedFormatException extends PelEntryException {
  95. /**
  96. * Construct a new exception indicating an invalid format.
  97. *
  98. * @param int the type of IFD.
  99. *
  100. * @param PelTag the tag for which the violation was found.
  101. *
  102. * @param PelFormat the format found.
  103. *
  104. * @param PelFormat the expected format.
  105. */
  106. function __construct($type, $tag, $found, $expected) {
  107. parent::__construct('Unexpected format found for %s tag: PelFormat::%s. ' .
  108. 'Expected PelFormat::%s instead.',
  109. PelTag::getName($type, $tag),
  110. strtoupper(PelFormat::getName($found)),
  111. strtoupper(PelFormat::getName($expected)));
  112. $this->tag = $tag;
  113. $this->type = $type;
  114. }
  115. }
  116. /**
  117. * Exception indicating that an unexpected number of components was
  118. * found.
  119. *
  120. * Some tags have strict limits as to the allowed number of
  121. * components, and this exception is thrown if the data violates such
  122. * a constraint. The documentation for each tag in {@link PelTag}
  123. * explains the expected number of components.
  124. *
  125. * @author Martin Geisler <mgeisler@users.sourceforge.net>
  126. * @package PEL
  127. * @subpackage Exception
  128. */
  129. class PelWrongComponentCountException extends PelEntryException {
  130. /**
  131. * Construct a new exception indicating a wrong number of
  132. * components.
  133. *
  134. * @param int the type of IFD.
  135. *
  136. * @param PelTag the tag for which the violation was found.
  137. *
  138. * @param int the number of components found.
  139. *
  140. * @param int the expected number of components.
  141. */
  142. function __construct($type, $tag, $found, $expected) {
  143. parent::__construct('Wrong number of components found for %s tag: %d. ' .
  144. 'Expected %d.',
  145. PelTag::getName($type, $tag), $found, $expected);
  146. $this->tag = $tag;
  147. $this->type = $type;
  148. }
  149. }
  150. /**
  151. * Common ancestor class of all {@link PelIfd} entries.
  152. *
  153. * As this class is abstract you cannot instantiate objects from it.
  154. * It only serves as a common ancestor to define the methods common to
  155. * all entries. The most important methods are {@link getValue()} and
  156. * {@link setValue()}, both of which is abstract in this class. The
  157. * descendants will give concrete implementations for them.
  158. *
  159. * If you have some data coming from an image (some raw bytes), then
  160. * the static method {@link newFromData()} is helpful --- it will look
  161. * at the data and give you a proper decendent of {@link PelEntry}
  162. * back.
  163. *
  164. * If you instead want to have an entry for some data which take the
  165. * form of an integer, a string, a byte, or some other PHP type, then
  166. * don't use this class. You should instead create an object of the
  167. * right subclass ({@link PelEntryShort} for short integers, {@link
  168. * PelEntryAscii} for strings, and so on) directly.
  169. *
  170. * @author Martin Geisler <mgeisler@users.sourceforge.net>
  171. * @package PEL
  172. */
  173. abstract class PelEntry {
  174. /**
  175. * Type of IFD containing this tag.
  176. *
  177. * This must be one of the constants defined in {@link PelIfd}:
  178. * {@link PelIfd::IFD0} for the main image IFD, {@link PelIfd::IFD1}
  179. * for the thumbnail image IFD, {@link PelIfd::EXIF} for the Exif
  180. * sub-IFD, {@link PelIfd::GPS} for the GPS sub-IFD, or {@link
  181. * PelIfd::INTEROPERABILITY} for the interoperability sub-IFD.
  182. *
  183. * @var int
  184. */
  185. protected $ifd_type;
  186. /**
  187. * The bytes representing this entry.
  188. *
  189. * Subclasses must either override {@link getBytes()} or, if
  190. * possible, maintain this property so that it always contains a
  191. * true representation of the entry.
  192. *
  193. * @var string
  194. */
  195. protected $bytes = '';
  196. /**
  197. * The {@link PelTag} of this entry.
  198. *
  199. * @var PelTag
  200. */
  201. protected $tag;
  202. /**
  203. * The {@link PelFormat} of this entry.
  204. *
  205. * @var PelFormat
  206. */
  207. protected $format;
  208. /**
  209. * The number of components of this entry.
  210. *
  211. * @var int
  212. */
  213. protected $components;
  214. /**
  215. * Return the tag of this entry.
  216. *
  217. * @return PelTag the tag of this entry.
  218. */
  219. function getTag() {
  220. return $this->tag;
  221. }
  222. /**
  223. * Return the type of IFD which holds this entry.
  224. *
  225. * @return int one of the constants defined in {@link PelIfd}:
  226. * {@link PelIfd::IFD0} for the main image IFD, {@link PelIfd::IFD1}
  227. * for the thumbnail image IFD, {@link PelIfd::EXIF} for the Exif
  228. * sub-IFD, {@link PelIfd::GPS} for the GPS sub-IFD, or {@link
  229. * PelIfd::INTEROPERABILITY} for the interoperability sub-IFD.
  230. */
  231. function getIfdType() {
  232. return $this->ifd_type;
  233. }
  234. /**
  235. * Update the IFD type.
  236. *
  237. * @param int must be one of the constants defined in {@link
  238. * PelIfd}: {@link PelIfd::IFD0} for the main image IFD, {@link
  239. * PelIfd::IFD1} for the thumbnail image IFD, {@link PelIfd::EXIF}
  240. * for the Exif sub-IFD, {@link PelIfd::GPS} for the GPS sub-IFD, or
  241. * {@link PelIfd::INTEROPERABILITY} for the interoperability
  242. * sub-IFD.
  243. */
  244. function setIfdType($type) {
  245. $this->ifd_type = $type;
  246. }
  247. /**
  248. * Return the format of this entry.
  249. *
  250. * @return PelFormat the format of this entry.
  251. */
  252. function getFormat() {
  253. return $this->format;
  254. }
  255. /**
  256. * Return the number of components of this entry.
  257. *
  258. * @return int the number of components of this entry.
  259. */
  260. function getComponents() {
  261. return $this->components;
  262. }
  263. /**
  264. * Turn this entry into bytes.
  265. *
  266. * @param PelByteOrder the desired byte order, which must be either
  267. * {@link Convert::LITTLE_ENDIAN} or {@link Convert::BIG_ENDIAN}.
  268. *
  269. * @return string bytes representing this entry.
  270. */
  271. function getBytes($o) {
  272. return $this->bytes;
  273. }
  274. /**
  275. * Get the value of this entry as text.
  276. *
  277. * The value will be returned in a format suitable for presentation,
  278. * e.g., rationals will be returned as 'x/y', ASCII strings will be
  279. * returned as themselves etc.
  280. *
  281. * @param boolean some values can be returned in a long or more
  282. * brief form, and this parameter controls that.
  283. *
  284. * @return string the value as text.
  285. */
  286. abstract function getText($brief = false);
  287. /**
  288. * Get the value of this entry.
  289. *
  290. * The value returned will generally be the same as the one supplied
  291. * to the constructor or with {@link setValue()}. For a formatted
  292. * version of the value, one should use {@link getText()} instead.
  293. *
  294. * @return mixed the unformatted value.
  295. */
  296. abstract function getValue();
  297. /**
  298. * Set the value of this entry.
  299. *
  300. * The value should be in the same format as for the constructor.
  301. *
  302. * @param mixed the new value.
  303. *
  304. * @abstract
  305. */
  306. function setValue($value) {
  307. /* This (fake) abstract method is here to make it possible for the
  308. * documentation to refer to PelEntry::setValue().
  309. *
  310. * It cannot declared abstract in the proper PHP way, for then PHP
  311. * wont allow subclasses to define it with two arguments (which is
  312. * what PelEntryCopyright does).
  313. */
  314. throw new PelException('setValue() is abstract.');
  315. }
  316. /**
  317. * Turn this entry into a string.
  318. *
  319. * @return string a string representation of this entry. This is
  320. * mostly for debugging.
  321. */
  322. function __toString() {
  323. $str = Pel::fmt(" Tag: 0x%04X (%s)\n",
  324. $this->tag, PelTag::getName($this->ifd_type, $this->tag));
  325. $str .= Pel::fmt(" Format : %d (%s)\n",
  326. $this->format, PelFormat::getName($this->format));
  327. $str .= Pel::fmt(" Components: %d\n", $this->components);
  328. if ($this->getTag() != PelTag::MAKER_NOTE &&
  329. $this->getTag() != PelTag::PRINT_IM)
  330. $str .= Pel::fmt(" Value : %s\n", print_r($this->getValue(), true));
  331. $str .= Pel::fmt(" Text : %s\n", $this->getText());
  332. return $str;
  333. }
  334. }
  335. ?>