Source for file PelEntryAscii.php

Documentation is available at PelEntryAscii.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. /* PelEntryAscii.php,v 1.7 2004/06/26 20:39:10 gimpster Exp */
  25.  
  26.  
  27. /**
  28. * Classes used to hold ASCII strings.
  29. *
  30. * The classes defined here are to be used for EXIF entries holding
  31. * ASCII strings, such as {@link PelTag::MAKE}, {@link }
  32. * PelTag::SOFTWARE}, and {@link PelTag::DATE_TIME}. For
  33. * entries holding normal textual ASCII strings the class {@link }
  34. * PelEntryAscii} should be used, but for entries holding
  35. * timestamps the class {@link PelEntryTime} would be more
  36. * convenient instead. Copyright information is handled by the {@link }
  37. * PelEntryCopyright} class.
  38. *
  39. * @author Martin Geisler <gimpster@users.sourceforge.net>
  40. * @version 1.7
  41. * @date 2004/06/26 20:39:10
  42. * @license http://www.gnu.org/licenses/gpl.html GNU General Public
  43. * License (GPL)
  44. * @package PEL
  45. */
  46.  
  47. /**#@+ Required class definitions. */
  48. ('PelEntry.php');
  49. /**#@-*/ * Class for holding a plain ASCII string.
  50. *
  51. * This class can hold a single ASCII string, and it will be used as in
  52. * <code>
  53. * $entry = $ifd->getEntry(PelTag::IMAGE_DESCRIPTION);
  54. * print($entry->getValue());
  55. * $entry->setValue('This is my image. I like it.');
  56. * </code>
  57. *
  58. * @author Martin Geisler <gimpster@users.sourceforge.net>
  59. * @package PEL
  60. */
  61. class PelEntryAscii extends PelEntry {
  62.  
  63. /**
  64. * The string hold by this entry.
  65. *
  66. * This is the string that was given to the {@link __construct}
  67. * constructor} or later to {@link setAscii}, without any final NULL
  68. * character.
  69. *
  70. * @var string
  71. */
  72. private $str;
  73.  
  74.  
  75. /**
  76. * Make a new PelEntry that can hold an ASCII string.
  77. *
  78. * @param int the tag which this entry represents. This should be
  79. * one of the constants defined in {@link PelTag}, e.g., {@link }
  80. * PelTag::IMAGE_DESCRIPTION}, {@link PelTag::MODEL}, or any other
  81. * tag with format {@link PelFormat::ASCII}.
  82. *
  83. * @param string the string that this entry will represent. The
  84. * string must obey the same rules as the string argument to {@link }
  85. * setAscii}, namely that it should be given without any trailing
  86. * NULL character and that it must be plain 7-bit ASCII.
  87. */
  88. function __construct($tag, $str = '') {
  89. $this->tag = $tag;
  90. $this->format = PelFormat::ASCII;
  91. self::setValue($str);
  92. }
  93.  
  94.  
  95. /**
  96. * Give the entry a new ASCII value.
  97. *
  98. * This will overwrite the previous value. The value can be
  99. * retrieved later with the {@link getValue} method.
  100. *
  101. * @param string the new value of the entry. This should be given
  102. * without any trailing NULL character. The string must be plain
  103. * 7-bit ASCII, the string should contain no high bytes.
  104. *
  105. * @todo Implement check for high bytes?
  106. */
  107. function setValue($str) {
  108. $this->components = strlen($str)+1;
  109. $this->str = $str;
  110. $this->bytes = $str . chr(0x00);
  111. }
  112.  
  113.  
  114. /**
  115. * Return the ASCII string of the entry.
  116. *
  117. * @return string the string held, without any final NULL character.
  118. * The string will be the same as the one given to {@link setValue}
  119. * or to the {@link __construct constructor}.
  120. */
  121. function getValue() {
  122. return $this->str;
  123. }
  124.  
  125.  
  126. /**
  127. * Return the ASCII string of the entry.
  128. *
  129. * This methods returns the same as {@link getValue}.
  130. *
  131. * @param boolean not used with ASCII entries.
  132. *
  133. * @return string the string held, without any final NULL character.
  134. * The string will be the same as the one given to {@link setValue}
  135. * or to the {@link __construct constructor}.
  136. */
  137. function getText($brief = false) {
  138. return $this->str;
  139. }
  140.  
  141. }
  142.  
  143.  
  144. /**
  145. * Class for holding a UNIX timestamp.
  146. *
  147. * This class can hold a single UNIX timestamp, and it will be used as
  148. * in this example where the time is advanced by one week:
  149. * <code>
  150. * $entry = $ifd->getEntry(PelTag::DATE_TIME_ORIGINAL);
  151. * $time = $entry->getValue();
  152. * print('The image was taken on the ' . date($time, 'jS'));
  153. * $entry->setValue($time + 7 * 24 * 3600);
  154. * </code>
  155. *
  156. * @author Martin Geisler <gimpster@users.sourceforge.net>
  157. * @package PEL
  158. */
  159. class PelEntryTime extends PelEntryAscii {
  160.  
  161. /**
  162. * The UNIX timestamp held by this entry.
  163. *
  164. * @var int
  165. */
  166. private $timestamp;
  167.  
  168.  
  169. /**
  170. * Make a new entry for holding a timestamp.
  171. *
  172. * @param int the EXIF tag which this entry represents. There are
  173. * only three standard tags which hold timestamp, so this should be
  174. * one of the constants {@link PelTag::DATE_TIME}, {@link }
  175. * PelTag::DATE_TIME_ORIGINAL}, or {@link }
  176. * PelTag::DATE_TIME_DIGITIZED}.
  177. *
  178. * @param int the UNIX timestamp held by this entry.
  179. */
  180. function __construct($tag, $timestamp = false) {
  181. if (!is_int($timestamp))
  182. $timestamp = time();
  183.  
  184. parent::__construct($tag);
  185. $this->setValue($timestamp);
  186. }
  187.  
  188. /**
  189. * Return the UNIX timestamp of the entry.
  190. *
  191. * @return int the timestamp held. This will be a standard UNIX
  192. * timestamp (counting the number of seconds since 00:00:00 January
  193. * 1st, 1970 UTC). This will be the same as the one given to {@link }
  194. * setTime} or to the {@link __construct constructor}.
  195. */
  196. function getValue() {
  197. return $this->timestamp;
  198. }
  199.  
  200.  
  201. /**
  202. * Update the UNIX timestamp held by this entry.
  203. *
  204. * @param int the new timestamp to be held by this entry. This
  205. * should be a standard UNIX timestamp (counting the number of
  206. * seconds since 00:00:00 January 1st, 1970 UTC). The old timestamp
  207. * will be overwritten, retrieve it first with {@link getValue} if
  208. * necessary.
  209. *
  210. * @todo How to deal with timezones? Use the TimeZoneOffset tag
  211. * 0x882A?
  212. */
  213. function setValue($timestamp) {
  214. $this->timestamp = $timestamp;
  215. parent::setValue(gmdate('Y:m:d H:i:s', $timestamp));
  216. }
  217. }
  218.  
  219.  
  220. /**
  221. * Class for holding copyright information.
  222. *
  223. * The EXIF standard specifies a certain format for copyright
  224. * information where the one {@link PelTag::COPYRIGHT copyright}
  225. * tag} holds both the photographer and editor copyrights, separated
  226. * by a NULL character.
  227. *
  228. * This class is used to manipulate that tag so that the format is
  229. * kept to the standard. A common use would be to add a new copyright
  230. * tag to an image, since most cameras don't add this tag themselves.
  231. * This would be done like this:
  232. *
  233. * <code>
  234. * $entry = new PelEntryCopyright('Copyright, Martin Geisler, 2004');
  235. * $ifd0->addEntry($entry);
  236. * </code>
  237. *
  238. * Here we only set the photographer copyright, use the optional
  239. * second argument to specify the editor copyright. If there's only
  240. * an editor copyright, then let the first argument be the empty
  241. * string.
  242. *
  243. * @author Martin Geisler <gimpster@users.sourceforge.net>
  244. * @package PEL
  245. */
  246. class PelEntryCopyright extends PelEntryAscii {
  247.  
  248. /**
  249. * The photographer copyright.
  250. *
  251. * @var string
  252. */
  253. private $photographer;
  254.  
  255. /**
  256. * The editor copyright.
  257. *
  258. * @var string
  259. */
  260. private $editor;
  261.  
  262.  
  263. /**
  264. * Make a new entry for holding copyright information.
  265. *
  266. * @param string the photographer copyright. Use the empty string
  267. * if there's no photographer copyright.
  268. *
  269. * @param string the editor copyright. Use the empty string if
  270. * there's no editor copyright.
  271. */
  272. function __construct($photographer = '', $editor = '') {
  273. parent::__construct(PelTag::COPYRIGHT);
  274. $this->setValue($photographer, $editor);
  275. }
  276.  
  277. /**
  278. * Update the copyright information.
  279. *
  280. * @param string the photographer copyright. Use the empty string
  281. * if there's no photographer copyright.
  282. *
  283. * @param string the editor copyright. Use the empty string if
  284. * there's no editor copyright.
  285. */
  286. function setValue($photographer = '', $editor = '') {
  287. $this->photographer = $photographer;
  288. $this->editor = $editor;
  289.  
  290. if ($photographer == '' && $editor != '')
  291. $photographer = ' ';
  292.  
  293. if ($editor == '')
  294. parent::setValue($photographer);
  295. else
  296. parent::setValue($photographer . chr(0x00) . $editor);
  297. }
  298.  
  299.  
  300. /**
  301. * Retrive the copyright information.
  302. *
  303. * The strings returned will be the same as the one used previously
  304. * with either {@link __construct the constructor} or with {@link }
  305. * setValue}.
  306. *
  307. * @return array an array with two strings, the photographer and
  308. * editor copyrights. The two fields will be returned in that
  309. * order, so that the first array index will be the photographer
  310. * copyright, and the second will be the editor copyright.
  311. */
  312. function getValue() {
  313. return array($this->photographer, $this->editor);
  314. }
  315.  
  316.  
  317. /**
  318. * Return a text string with the copyright information.
  319. *
  320. * The photographer and editor copyright fields will be returned
  321. * with a '-' in between if both copyright fields are present,
  322. * otherwise only one of them will be returned.
  323. *
  324. * @param boolean if false, then the strings '(Photographer)' and
  325. * '(Editor)' will be appended to the photographer and editor
  326. * copyright fields (if present), otherwise the fields will be
  327. * returned as is.
  328. *
  329. * @return string the copyright information in a string.
  330. */
  331. function getText($brief = false) {
  332. if ($brief) {
  333. $p = '';
  334. $e = '';
  335. } else {
  336. $p = ' ' . Pel::tra('(Photographer)');
  337. $e = ' ' . Pel::tra('(Editor)');
  338. }
  339.  
  340. if ($this->photographer != '' && $this->editor != '')
  341. return $this->photographer . $p . ' - ' . $this->editor . $e;
  342. if ($this->photographer != '')
  343. return $this->photographer . $p;
  344.  
  345. if ($this->editor != '')
  346. return $this->editor . $e;
  347.  
  348. return '';
  349. }
  350. }
  351.  
  352. ?>

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