Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / lsolesen / pel / src / Pel.php
1 <?php
2
3 /**
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.
7  *
8  * Copyright (C) 2004, 2005, 2006, 2007 Martin Geisler.
9  *
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.
14  *
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.
19  *
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
24  */
25 namespace lsolesen\pel;
26
27 /**
28  * Class with miscellaneous static methods.
29  *
30  * This class will contain various methods that govern the overall
31  * behavior of PEL.
32  *
33  * Debugging output from PEL can be turned on and off by assigning
34  * true or false to {@link Pel::$debug}.
35  *
36  * @author Martin Geisler <mgeisler@users.sourceforge.net>
37  * @package PEL
38  */
39 class Pel
40 {
41
42     /**
43      * Flag that controls if dgettext can be used.
44      * Is set to true or fals at the first access
45      *
46      * @var boolean|NULL
47      */
48     private static $hasdgetext = null;
49
50     /**
51      * Flag for controlling debug information.
52      *
53      * The methods producing debug information ({@link debug()} and
54      * {@link warning()}) will only output something if this variable is
55      * set to true.
56      *
57      * @var boolean
58      */
59     private static $debug = false;
60
61     /**
62      * Flag for strictness of parsing.
63      *
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}.
68      *
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
71      * possible.
72      *
73      * @var boolean
74      */
75     private static $strict = false;
76
77     /**
78      * Stored exceptions.
79      *
80      * When {@link Pel::$strict} is set to false exceptions will be
81      * accumulated here instead of being thrown.
82      */
83     private static $exceptions = array();
84
85     /**
86      * Quality setting for encoding JPEG images.
87      *
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.
91      *
92      * The default is 75 for average quality images, but you can change
93      * this to an integer between 0 and 100.
94      *
95      * @var int
96      */
97     private static $quality = 75;
98
99     /**
100      * Set the JPEG encoding quality.
101      *
102      * @param int $quality
103      *            an integer between 0 and 100 with 75 being
104      *            average quality and 95 very good quality.
105      */
106     public static function setJPEGQuality($quality)
107     {
108         self::$quality = $quality;
109     }
110
111     /**
112      * Get current setting for JPEG encoding quality.
113      *
114      * @return int the quality.
115      */
116     public static function getJPEGQuality()
117     {
118         return self::$quality;
119     }
120
121     /**
122      * Return list of stored exceptions.
123      *
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.
127      *
128      * Code for using this could look like this:
129      *
130      * <code>
131      * Pel::setStrictParsing(true);
132      * Pel::clearExceptions();
133      *
134      * $jpeg = new PelJpeg($file);
135      *
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()));
143      * }
144      * }
145      * </code>
146      *
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.
150      *
151      * @return array the exceptions.
152      */
153     public static function getExceptions()
154     {
155         return self::$exceptions;
156     }
157
158     /**
159      * Clear list of stored exceptions.
160      *
161      * Use this function before a call to some method if you intend to
162      * check for exceptions afterwards.
163      */
164     public static function clearExceptions()
165     {
166         self::$exceptions = array();
167     }
168
169     /**
170      * Conditionally throw an exception.
171      *
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
176      * Pel::warning}).
177      *
178      * @param PelException $e
179      *            the exceptions.
180      */
181     public static function maybeThrow(PelException $e)
182     {
183         if (self::$strict) {
184             throw $e;
185         } else {
186             self::$exceptions[] = $e;
187             self::warning('%s (%s:%s)', $e->getMessage(), basename($e->getFile()), $e->getLine());
188         }
189     }
190
191     /**
192      * Enable/disable strict parsing.
193      *
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
198      * getExceptions()}.
199      *
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
202      * possible.
203      *
204      * @param boolean $flag
205      *            use true to enable strict parsing, false to
206      *            diable.
207      */
208     public static function setStrictParsing($flag)
209     {
210         self::$strict = $flag;
211     }
212
213     /**
214      * Get current setting for strict parsing.
215      *
216      * @return boolean true if strict parsing is in effect, false
217      *         otherwise.
218      */
219     public static function getStrictParsing()
220     {
221         return self::$strict;
222     }
223
224     /**
225      * Enable/disable debugging output.
226      *
227      * @param boolean $flag
228      *            use true to enable debug output, false to
229      *            diable.
230      */
231     public static function setDebug($flag)
232     {
233         self::$debug = $flag;
234     }
235
236     /**
237      * Get current setting for debug output.
238      *
239      * @return boolean true if debug is enabled, false otherwise.
240      */
241     public static function getDebug()
242     {
243         return self::$debug;
244     }
245
246     /**
247      * Conditionally output debug information.
248      *
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.
252      *
253      * @param string $format
254      *            the format string.
255      *
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
259      *            sprintf().
260      */
261     public static function debug($format)
262     {
263         if (self::$debug) {
264             $args = func_get_args();
265             $str = array_shift($args);
266             vprintf($str . "\n", $args);
267         }
268     }
269
270     /**
271      * Conditionally output a warning.
272      *
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.
277      *
278      * @param string $format
279      *            the format string.
280      *
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
284      *            sprintf().
285      */
286     public static function warning($format)
287     {
288         if (self::$debug) {
289             $args = func_get_args();
290             $str = array_shift($args);
291             vprintf('Warning: ' . $str . "\n", $args);
292         }
293     }
294
295     /**
296      * Translate a string.
297      *
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.
302      *
303      * @param string $str
304      *            the string that should be translated.
305      *
306      * @return string the translated string, or the original string if
307      *         no translation could be found.
308      */
309     public static function tra($str)
310     {
311         return self::dgettextWrapper('pel', $str);
312     }
313
314     /**
315      * Translate and format a string.
316      *
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
322      * faster.
323      *
324      * @param string $format
325      *            the format string. This will be translated
326      *            before being used as a format string.
327      *
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
331      *            sprintf().
332      *
333      * @return string the translated string, or the original string if
334      *         no translation could be found.
335      */
336     public static function fmt($format)
337     {
338         $args = func_get_args();
339         $str = array_shift($args);
340         return vsprintf(self::dgettextWrapper('pel', $str), $args);
341     }
342
343     /**
344      * Warapper for dgettext.
345      * The untranslated stub will be return in the case that dgettext is not available.
346      *
347      * @param string $domain
348      * @param string $str
349      * @return string
350      */
351     private static function dgettextWrapper($domain, $str)
352     {
353         if (self::$hasdgetext === null) {
354             self::$hasdgetext = function_exists('dgettext');
355             if (self::$hasdgetext === true) {
356                 bindtextdomain('pel', __DIR__ . '/locale');
357             }
358         }
359         if (self::$hasdgetext) {
360             return dgettext($domain, $str);
361         } else {
362             return $str;
363         }
364     }
365 }