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

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