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

Documentation generated on Thu, 05 May 2011 07:19:00 +0200 by phpDocumentor 1.4.3