4 * PEL: PHP Exif Library.
5 * A library with support for reading and
6 * writing all Exif headers in JPEG and TIFF images using PHP.
8 * Copyright (C) 2004, 2005, 2006 Martin Geisler.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program in the file COPYING; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
25 namespace lsolesen\pel;
28 * Classes used to manipulate rational numbers.
30 * @author Martin Geisler <mgeisler@users.sourceforge.net>
31 * @license http://www.gnu.org/licenses/gpl.html GNU General Public
37 * Class for holding signed rational numbers.
39 * This class can hold rational numbers, consisting of a numerator and
40 * denominator both of which are of type unsigned long. Each rational
41 * is represented by an array with just two entries: the numerator and
42 * the denominator, in that order.
44 * The class can hold either just a single rational or an array of
45 * rationals. The class will be used to manipulate any of the Exif
46 * tags which can have format {@link PelFormat::SRATIONAL}.
48 * @author Martin Geisler <mgeisler@users.sourceforge.net>
51 class PelEntrySRational extends PelEntrySLong
55 * Make a new entry that can hold a signed rational.
58 * PelTag the tag which this entry represents. This should
59 * be one of the constants defined in {@link PelTag}, e.g., {@link
60 * PelTag::SHUTTER_SPEED_VALUE}, or any other tag which can have
61 * format {@link PelFormat::SRATIONAL}.
63 * @param array $value...
64 * the rational(s) that this entry will
65 * represent. The arguments passed must obey the same rules as the
66 * argument to {@link setValue}, namely that each argument should be
67 * an array with two entries, both of which must be within range of
68 * a signed long (32 bit), that is between -2147483648 and
69 * 2147483647 (inclusive). If not, then a {@link
70 * PelOverflowException} will be thrown.
72 public function __construct($tag, $value = null)
75 $this->format = PelFormat::SRATIONAL;
77 $this->min = - 2147483648;
78 $this->max = 2147483647;
80 $value = func_get_args();
82 $this->setValueArray($value);
86 * Format a rational number.
88 * The rational will be returned as a string with a slash '/'
89 * between the numerator and denominator. Care is taken to display
90 * '-1/2' instead of the ugly but mathematically equivalent '1/-2'.
93 * array the rational which will be formatted.
98 * @return string the rational formatted as a string suitable for
101 public function formatNumber($number, $brief = false)
103 if ($number[1] < 0) {
104 /* Turn output like 1/-2 into -1/2. */
105 return (- $number[0]) . '/' . (- $number[1]);
107 return $number[0] . '/' . $number[1];
112 * Get the value of an entry as text.
114 * The value will be returned in a format suitable for presentation,
115 * e.g., rationals will be returned as 'x/y', ASCII strings will be
116 * returned as themselves etc.
119 * boolean some values can be returned in a long or more
120 * brief form, and this parameter controls that.
122 * @return string the value as text.
124 public function getText($brief = false)
126 if (isset($this->value[0])) {
127 $v = $this->value[0];
130 switch ($this->tag) {
131 case PelTag::SHUTTER_SPEED_VALUE:
132 // CC (e->components, 1, v);
133 // if (!v_srat.denominator) return (NULL);
134 return Pel::fmt('%.0f/%.0f sec. (APEX: %d)', $v[0], $v[1], pow(sqrt(2), $v[0] / $v[1]));
136 case PelTag::BRIGHTNESS_VALUE:
137 // CC (e->components, 1, v);
139 // TODO: figure out the APEX thing, or remove this so that it is
140 // handled by the default clause at the bottom.
141 return sprintf('%d/%d', $v[0], $v[1]);
142 // FIXME: How do I calculate the APEX value?
144 case PelTag::EXPOSURE_BIAS_VALUE:
145 // CC (e->components, 1, v);
146 // if (!v_srat.denominator) return (NULL);
147 return sprintf('%s%.01f', $v[0] * $v[1] > 0 ? '+' : '', $v[0] / $v[1]);
150 return parent::getText($brief);