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, 2007 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 * Class with miscellaneous static methods.
30 * This class will contain various methods that govern the overall
33 * Debugging output from PEL can be turned on and off by assigning
34 * true or false to {@link Pel::$debug}.
36 * @author Martin Geisler <mgeisler@users.sourceforge.net>
43 * Flag that controls if dgettext can be used.
44 * Is set to true or fals at the first access
48 private static $hasdgetext = null;
51 * Flag for controlling debug information.
53 * The methods producing debug information ({@link debug()} and
54 * {@link warning()}) will only output something if this variable is
59 private static $debug = false;
62 * Flag for strictness of parsing.
64 * If this variable is set to true, then most errors while loading
65 * images will result in exceptions being thrown. Otherwise a
66 * warning will be emitted (using {@link Pel::warning}) and the
67 * exceptions will be appended to {@link Pel::$exceptions}.
69 * Some errors will still be fatal and result in thrown exceptions,
70 * but an effort will be made to skip over as much garbage as
75 private static $strict = false;
80 * When {@link Pel::$strict} is set to false exceptions will be
81 * accumulated here instead of being thrown.
83 private static $exceptions = array();
86 * Quality setting for encoding JPEG images.
88 * This controls the quality used then PHP image resources are
89 * encoded into JPEG images. This happens when you create a
90 * {@link PelJpeg} object based on an image resource.
92 * The default is 75 for average quality images, but you can change
93 * this to an integer between 0 and 100.
97 private static $quality = 75;
100 * Set the JPEG encoding quality.
102 * @param int $quality
103 * an integer between 0 and 100 with 75 being
104 * average quality and 95 very good quality.
106 public static function setJPEGQuality($quality)
108 self::$quality = $quality;
112 * Get current setting for JPEG encoding quality.
114 * @return int the quality.
116 public static function getJPEGQuality()
118 return self::$quality;
122 * Return list of stored exceptions.
124 * When PEL is parsing in non-strict mode, it will store most
125 * exceptions instead of throwing them. Use this method to get hold
126 * of them when a call returns.
128 * Code for using this could look like this:
131 * Pel::setStrictParsing(true);
132 * Pel::clearExceptions();
134 * $jpeg = new PelJpeg($file);
136 * // Check for exceptions.
137 * foreach (Pel::getExceptions() as $e) {
138 * printf("Exception: %s\n", $e->getMessage());
139 * if ($e instanceof PelEntryException) {
140 * // Warn about entries that couldn't be loaded.
141 * printf("Warning: Problem with %s.\n",
142 * PelTag::getName($e->getType(), $e->getTag()));
147 * This gives applications total control over the amount of error
148 * messages shown and (hopefully) provides the necessary information
149 * for proper error recovery.
151 * @return array the exceptions.
153 public static function getExceptions()
155 return self::$exceptions;
159 * Clear list of stored exceptions.
161 * Use this function before a call to some method if you intend to
162 * check for exceptions afterwards.
164 public static function clearExceptions()
166 self::$exceptions = array();
170 * Conditionally throw an exception.
172 * This method will throw the passed exception when strict parsing
173 * in effect (see {@link setStrictParsing()}). Otherwise the
174 * exception is stored (it can be accessed with {@link
175 * getExceptions()}) and a warning is issued (with {@link
178 * @param PelException $e
181 public static function maybeThrow(PelException $e)
186 self::$exceptions[] = $e;
187 self::warning('%s (%s:%s)', $e->getMessage(), basename($e->getFile()), $e->getLine());
192 * Enable/disable strict parsing.
194 * If strict parsing is enabled, then most errors while loading
195 * images will result in exceptions being thrown. Otherwise a
196 * warning will be emitted (using {@link Pel::warning}) and the
197 * exceptions will be stored for later use via {@link
200 * Some errors will still be fatal and result in thrown exceptions,
201 * but an effort will be made to skip over as much garbage as
204 * @param boolean $flag
205 * use true to enable strict parsing, false to
208 public static function setStrictParsing($flag)
210 self::$strict = $flag;
214 * Get current setting for strict parsing.
216 * @return boolean true if strict parsing is in effect, false
219 public static function getStrictParsing()
221 return self::$strict;
225 * Enable/disable debugging output.
227 * @param boolean $flag
228 * use true to enable debug output, false to
231 public static function setDebug($flag)
233 self::$debug = $flag;
237 * Get current setting for debug output.
239 * @return boolean true if debug is enabled, false otherwise.
241 public static function getDebug()
247 * Conditionally output debug information.
249 * This method works just like printf() except that it always
250 * terminates the output with a newline, and that it only outputs
251 * something if the {@link Pel::$debug} is true.
253 * @param string $format
256 * @param mixed ...$args
257 * any number of arguments can be given. The
258 * arguments will be available for the format string as usual with
261 public static function debug($format)
264 $args = func_get_args();
265 $str = array_shift($args);
266 vprintf($str . "\n", $args);
271 * Conditionally output a warning.
273 * This method works just like printf() except that it prepends the
274 * output with the string 'Warning: ', terminates the output with a
275 * newline, and that it only outputs something if the PEL_DEBUG
276 * defined to some true value.
278 * @param string $format
281 * @param mixed ...$args
282 * any number of arguments can be given. The
283 * arguments will be available for the format string as usual with
286 public static function warning($format)
289 $args = func_get_args();
290 $str = array_shift($args);
291 vprintf('Warning: ' . $str . "\n", $args);
296 * Translate a string.
298 * This static function will use Gettext to translate a string. By
299 * always using this function for static string one is assured that
300 * the translation will be taken from the correct text domain.
301 * Dynamic strings should be passed to {@link fmt} instead.
304 * the string that should be translated.
306 * @return string the translated string, or the original string if
307 * no translation could be found.
309 public static function tra($str)
311 return self::dgettextWrapper('pel', $str);
315 * Translate and format a string.
317 * This static function will first use Gettext to translate a format
318 * string, which will then have access to any extra arguments. By
319 * always using this function for dynamic string one is assured that
320 * the translation will be taken from the correct text domain. If
321 * the string is static, use {@link tra} instead as it will be
324 * @param string $format
325 * the format string. This will be translated
326 * before being used as a format string.
328 * @param mixed ...$args
329 * any number of arguments can be given. The
330 * arguments will be available for the format string as usual with
333 * @return string the translated string, or the original string if
334 * no translation could be found.
336 public static function fmt($format)
338 $args = func_get_args();
339 $str = array_shift($args);
340 return vsprintf(self::dgettextWrapper('pel', $str), $args);
344 * Warapper for dgettext.
345 * The untranslated stub will be return in the case that dgettext is not available.
347 * @param string $domain
351 private static function dgettextWrapper($domain, $str)
353 if (self::$hasdgetext === null) {
354 self::$hasdgetext = function_exists('dgettext');
355 if (self::$hasdgetext === true) {
356 bindtextdomain('pel', __DIR__ . '/locale');
359 if (self::$hasdgetext) {
360 return dgettext($domain, $str);