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

Documentation generated on Tue, 19 Dec 2006 01:08:17 +0100 by phpDocumentor 1.3.0 SourceForge.net Logo