Source for file PelEntryRational.php

Documentation is available at PelEntryRational.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 used to manipulate rational numbers.
  29.  *
  30.  * @author Martin Geisler <mgeisler@users.sourceforge.net>
  31.  * @version $Revision$
  32.  * @date $Date$
  33.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public
  34.  *  License (GPL)
  35.  * @package PEL
  36.  */
  37.  
  38. /**#@+ Required class definitions. */
  39. require_once('PelEntryLong.php');
  40. /**#@-*/
  41.  
  42.  
  43. /**
  44.  * Class for holding unsigned rational numbers.
  45.  *
  46.  * This class can hold rational numbers, consisting of a numerator and
  47.  * denominator both of which are of type unsigned long.  Each rational
  48.  * is represented by an array with just two entries: the numerator and
  49.  * the denominator, in that order.
  50.  *
  51.  * The class can hold either just a single rational or an array of
  52.  * rationals.  The class will be used to manipulate any of the Exif
  53.  * tags which can have format {@link PelFormat::RATIONAL} like in this
  54.  * example:
  55.  *
  56.  * <code>
  57.  * $resolution = $ifd->getEntry(PelTag::X_RESOLUTION);
  58.  * $resolution->setValue(array(1, 300));
  59.  * </code>
  60.  *
  61.  * Here the x-resolution is adjusted to 1/300, which will be 300 DPI,
  62.  * unless the {@link PelTag::RESOLUTION_UNIT resolution unit} is set
  63.  * to something different than 2 which means inches.
  64.  *
  65.  * @author Martin Geisler <mgeisler@users.sourceforge.net>
  66.  * @package PEL
  67.  */
  68. class PelEntryRational extends PelEntryLong {
  69.  
  70.   /**
  71.    * Make a new entry that can hold an unsigned rational.
  72.    *
  73.    * @param PelTag the tag which this entry represents.  This should
  74.    *  be one of the constants defined in {@link PelTag}, e.g., {@link }
  75.    *  PelTag::X_RESOLUTION}, or any other tag which can have format
  76.    *  {@link PelFormat::RATIONAL}.
  77.    *
  78.    * @param array $value... the rational(s) that this entry will
  79.    *  represent.  The arguments passed must obey the same rules as the
  80.    *  argument to {@link setValue}, namely that each argument should be
  81.    *  an array with two entries, both of which must be within range of
  82.    *  an unsigned long (32 bit), that is between 0 and 4294967295
  83.    *  (inclusive).  If not, then a {@link PelOverflowException} will be
  84.    *  thrown.
  85.    */
  86.   function __construct($tag /* $value... */{
  87.     $this->tag       = $tag;
  88.     $this->format    = PelFormat::RATIONAL;
  89.     $this->dimension = 2;
  90.     $this->min       = 0;
  91.     $this->max       = 4294967295;
  92.  
  93.     $value func_get_args();
  94.     array_shift($value);
  95.     $this->setValueArray($value);
  96.   }
  97.  
  98.  
  99.   /**
  100.    * Format a rational number.
  101.    *
  102.    * The rational will be returned as a string with a slash '/'
  103.    * between the numerator and denominator.
  104.    *
  105.    * @param array the rational which will be formatted.
  106.    *
  107.    * @param boolean not used.
  108.    *
  109.    * @return string the rational formatted as a string suitable for
  110.    *  display.
  111.    */
  112.   function formatNumber($number$brief false{
  113.     return $number[0'/' $number[1];
  114.   }
  115.  
  116.  
  117.   /**
  118.    * Get the value of an entry as text.
  119.    *
  120.    * The value will be returned in a format suitable for presentation,
  121.    * e.g., rationals will be returned as 'x/y', ASCII strings will be
  122.    * returned as themselves etc.
  123.    *
  124.    * @param boolean some values can be returned in a long or more
  125.    *  brief form, and this parameter controls that.
  126.    *
  127.    * @return string the value as text.
  128.    */
  129.   function getText($brief false{
  130.     if (isset($this->value[0]))
  131.       $v $this->value[0];
  132.  
  133.     switch ($this->tag{
  134.     case PelTag::FNUMBER:
  135.       //CC (e->components, 1, v);
  136.       return Pel::fmt('f/%.01f'$v[0]/$v[1]);
  137.  
  138.     case PelTag::APERTURE_VALUE:
  139.       //CC (e->components, 1, v);
  140.       //if (!v_rat.denominator) return (NULL);
  141.       return Pel::fmt('f/%.01f'pow(2$v[0]/$v[1]/2));
  142.  
  143.     case PelTag::FOCAL_LENGTH:
  144.       //CC (e->components, 1, v);
  145.       //if (!v_rat.denominator) return (NULL);
  146.       return Pel::fmt('%.1f mm'$v[0]/$v[1]);
  147.  
  148.     case PelTag::SUBJECT_DISTANCE:
  149.       //CC (e->components, 1, v);
  150.       //if (!v_rat.denominator) return (NULL);
  151.       return Pel::fmt('%.1f m'$v[0]/$v[1]);
  152.  
  153.     case PelTag::EXPOSURE_TIME:
  154.       //CC (e->components, 1, v);
  155.       //if (!v_rat.denominator) return (NULL);
  156.       if ($v[0]/$v[11)
  157.         return Pel::fmt('1/%d sec.'$v[1]/$v[0]);
  158.       else
  159.         return Pel::fmt('%d sec.'$v[0]/$v[1]);
  160.  
  161.     case PelTag::GPS_LATITUDE:
  162.     case PelTag::GPS_LONGITUDE:
  163.       $degrees $this->value[0][0]/$this->value[0][1];
  164.       $minutes $this->value[1][0]/$this->value[1][1];
  165.       $seconds $this->value[2][0]/$this->value[2][1];
  166.  
  167.       return sprintf('%s° %s\' %s" (%.2f°)',
  168.                      $degrees$minutes$seconds,
  169.                      $degrees $minutes/60 $seconds/3600);
  170.  
  171.     default:
  172.       return parent::getText($brief);
  173.     }
  174.   }
  175. }
  176.  
  177.  
  178. /**
  179.  * Class for holding signed rational numbers.
  180.  *
  181.  * This class can hold rational numbers, consisting of a numerator and
  182.  * denominator both of which are of type unsigned long.  Each rational
  183.  * is represented by an array with just two entries: the numerator and
  184.  * the denominator, in that order.
  185.  *
  186.  * The class can hold either just a single rational or an array of
  187.  * rationals.  The class will be used to manipulate any of the Exif
  188.  * tags which can have format {@link PelFormat::SRATIONAL}.
  189.  *
  190.  * @author Martin Geisler <mgeisler@users.sourceforge.net>
  191.  * @package PEL
  192.  */
  193. class PelEntrySRational extends PelEntrySLong {
  194.  
  195.   /**
  196.    * Make a new entry that can hold a signed rational.
  197.    *
  198.    * @param PelTag the tag which this entry represents.  This should
  199.    *  be one of the constants defined in {@link PelTag}, e.g., {@link }
  200.    *  PelTag::SHUTTER_SPEED_VALUE}, or any other tag which can have
  201.    *  format {@link PelFormat::SRATIONAL}.
  202.    *
  203.    * @param array $value... the rational(s) that this entry will
  204.    *  represent.  The arguments passed must obey the same rules as the
  205.    *  argument to {@link setValue}, namely that each argument should be
  206.    *  an array with two entries, both of which must be within range of
  207.    *  a signed long (32 bit), that is between -2147483648 and
  208.    *  2147483647 (inclusive).  If not, then a {@link }
  209.    *  PelOverflowException} will be thrown.
  210.    */
  211.   function __construct($tag /* $value... */{
  212.     $this->tag       = $tag;
  213.     $this->format    = PelFormat::SRATIONAL;
  214.     $this->dimension = 2;
  215.     $this->min       = -2147483648;
  216.     $this->max       = 2147483647;
  217.  
  218.     $value func_get_args();
  219.     array_shift($value);
  220.     $this->setValueArray($value);
  221.   }
  222.  
  223.  
  224.   /**
  225.    * Format a rational number.
  226.    *
  227.    * The rational will be returned as a string with a slash '/'
  228.    * between the numerator and denominator.  Care is taken to display
  229.    * '-1/2' instead of the ugly but mathematically equivalent '1/-2'.
  230.    *
  231.    * @param array the rational which will be formatted.
  232.    *
  233.    * @param boolean not used.
  234.    *
  235.    * @return string the rational formatted as a string suitable for
  236.    *  display.
  237.    */
  238.   function formatNumber($number$brief false{
  239.     if ($number[10)
  240.       /* Turn output like 1/-2 into -1/2. */
  241.       return (-$number[0]'/' (-$number[1]);
  242.     else
  243.       return $number[0'/' $number[1];
  244.   }
  245.  
  246.  
  247.   /**
  248.    * Get the value of an entry as text.
  249.    *
  250.    * The value will be returned in a format suitable for presentation,
  251.    * e.g., rationals will be returned as 'x/y', ASCII strings will be
  252.    * returned as themselves etc.
  253.    *
  254.    * @param boolean some values can be returned in a long or more
  255.    *  brief form, and this parameter controls that.
  256.    *
  257.    * @return string the value as text.
  258.    */
  259.   function getText($brief false{
  260.     if (isset($this->value[0]))
  261.       $v $this->value[0];
  262.  
  263.     switch ($this->tag{
  264.     case PelTag::SHUTTER_SPEED_VALUE:
  265.       //CC (e->components, 1, v);
  266.       //if (!v_srat.denominator) return (NULL);
  267.       return Pel::fmt('%.0f/%.0f sec. (APEX: %d)',
  268.                       $v[0]$v[1]pow(sqrt(2)$v[0]/$v[1]));
  269.  
  270.     case PelTag::BRIGHTNESS_VALUE:
  271.       //CC (e->components, 1, v);
  272.       //
  273.       // TODO: figure out the APEX thing, or remove this so that it is
  274.       // handled by the default clause at the bottom.
  275.       return sprintf('%d/%d'$v[0]$v[1]);
  276.       //FIXME: How do I calculate the APEX value?
  277.  
  278.     case PelTag::EXPOSURE_BIAS_VALUE:
  279.       //CC (e->components, 1, v);
  280.       //if (!v_srat.denominator) return (NULL);
  281.       return sprintf('%s%.01f'$v[0]*$v[1'+' ''$v[0]/$v[1]);
  282.  
  283.     default:
  284.       return parent::getText($brief);
  285.     }
  286.   }
  287.  
  288. }
  289.  
  290. ?>

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