PageRenderTime 87ms CodeModel.GetById 37ms RepoModel.GetById 5ms app.codeStats 0ms

/craft/app/vendor/lsolesen/pel/src/Pel.php

https://gitlab.com/madebycloud/derekman
PHP | 372 lines | 69 code | 42 blank | 261 comment | 6 complexity | a3f28000556bc7ac4af5bff1313a341b MD5 | raw file
  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, 2007 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. /* $Id$ */
  24. /**
  25. * Miscellaneous stuff for the overall behavior of PEL.
  26. *
  27. * @author Martin Geisler <mgeisler@users.sourceforge.net>
  28. * @version $Revision$
  29. * @date $Date$
  30. * @license http://www.gnu.org/licenses/gpl.html GNU General Public
  31. * License (GPL)
  32. * @package PEL
  33. */
  34. /* Initialize Gettext, if available. This must be done before any
  35. * part of PEL calls Pel::tra() or Pel::fmt() --- this is ensured if
  36. * every piece of code using those two functions require() this file.
  37. *
  38. * If Gettext is not available, wrapper functions will be created,
  39. * allowing PEL to function, but without any translations.
  40. *
  41. * The PEL translations are stored in './locale'. It is important to
  42. * use an absolute path here because the lookups will be relative to
  43. * the current directory. */
  44. if (function_exists('dgettext')) {
  45. bindtextdomain('pel', dirname(__FILE__) . '/locale');
  46. } else {
  47. /**
  48. * Pretend to lookup a message in a specific domain.
  49. *
  50. * This is just a stub which will return the original message
  51. * untranslated. The function will only be defined if the Gettext
  52. * extension has not already defined it.
  53. *
  54. * @param string $domain the domain.
  55. *
  56. * @param string $str the message to be translated.
  57. *
  58. * @return string the original, untranslated message.
  59. */
  60. function dgettext($domain, $str) {
  61. return $str;
  62. }
  63. }
  64. /**
  65. * Class with miscellaneous static methods.
  66. *
  67. * This class will contain various methods that govern the overall
  68. * behavior of PEL.
  69. *
  70. * Debugging output from PEL can be turned on and off by assigning
  71. * true or false to {@link Pel::$debug}.
  72. *
  73. * @author Martin Geisler <mgeisler@users.sourceforge.net>
  74. * @package PEL
  75. */
  76. class Pel {
  77. /**
  78. * Flag for controlling debug information.
  79. *
  80. * The methods producing debug information ({@link debug()} and
  81. * {@link warning()}) will only output something if this variable is
  82. * set to true.
  83. *
  84. * @var boolean
  85. */
  86. private static $debug = false;
  87. /**
  88. * Flag for strictness of parsing.
  89. *
  90. * If this variable is set to true, then most errors while loading
  91. * images will result in exceptions being thrown. Otherwise a
  92. * warning will be emitted (using {@link Pel::warning}) and the
  93. * exceptions will be appended to {@link Pel::$exceptions}.
  94. *
  95. * Some errors will still be fatal and result in thrown exceptions,
  96. * but an effort will be made to skip over as much garbage as
  97. * possible.
  98. *
  99. * @var boolean
  100. */
  101. private static $strict = false;
  102. /**
  103. * Stored exceptions.
  104. *
  105. * When {@link Pel::$strict} is set to false exceptions will be
  106. * accumulated here instead of being thrown.
  107. */
  108. private static $exceptions = array();
  109. /**
  110. * Quality setting for encoding JPEG images.
  111. *
  112. * This controls the quality used then PHP image resources are
  113. * encoded into JPEG images. This happens when you create a
  114. * {@link PelJpeg} object based on an image resource.
  115. *
  116. * The default is 75 for average quality images, but you can change
  117. * this to an integer between 0 and 100.
  118. *
  119. * @var int
  120. */
  121. private static $quality = 75;
  122. /**
  123. * Set the JPEG encoding quality.
  124. *
  125. * @param int $quality an integer between 0 and 100 with 75 being
  126. * average quality and 95 very good quality.
  127. */
  128. function setJPEGQuality($quality) {
  129. self::$quality = $quality;
  130. }
  131. /**
  132. * Get current setting for JPEG encoding quality.
  133. *
  134. * @return int the quality.
  135. */
  136. function getJPEGQuality() {
  137. return self::$quality;
  138. }
  139. /**
  140. * Return list of stored exceptions.
  141. *
  142. * When PEL is parsing in non-strict mode, it will store most
  143. * exceptions instead of throwing them. Use this method to get hold
  144. * of them when a call returns.
  145. *
  146. * Code for using this could look like this:
  147. *
  148. * <code>
  149. * Pel::setStrictParsing(true);
  150. * Pel::clearExceptions();
  151. *
  152. * $jpeg = new PelJpeg($file);
  153. *
  154. * // Check for exceptions.
  155. * foreach (Pel::getExceptions() as $e) {
  156. * printf("Exception: %s\n", $e->getMessage());
  157. * if ($e instanceof PelEntryException) {
  158. * // Warn about entries that couldn't be loaded.
  159. * printf("Warning: Problem with %s.\n",
  160. * PelTag::getName($e->getType(), $e->getTag()));
  161. * }
  162. * }
  163. * </code>
  164. *
  165. * This gives applications total control over the amount of error
  166. * messages shown and (hopefully) provides the necessary information
  167. * for proper error recovery.
  168. *
  169. * @return array the exceptions.
  170. */
  171. static function getExceptions() {
  172. return self::$exceptions;
  173. }
  174. /**
  175. * Clear list of stored exceptions.
  176. *
  177. * Use this function before a call to some method if you intend to
  178. * check for exceptions afterwards.
  179. */
  180. static function clearExceptions() {
  181. self::$exceptions = array();
  182. }
  183. /**
  184. * Conditionally throw an exception.
  185. *
  186. * This method will throw the passed exception when strict parsing
  187. * in effect (see {@link setStrictParsing()}). Otherwise the
  188. * exception is stored (it can be accessed with {@link
  189. * getExceptions()}) and a warning is issued (with {@link
  190. * Pel::warning}).
  191. *
  192. * @param PelException $e the exceptions.
  193. */
  194. static function maybeThrow(PelException $e) {
  195. if (self::$strict) {
  196. throw $e;
  197. } else {
  198. self::$exceptions[] = $e;
  199. self::warning('%s (%s:%s)', $e->getMessage(),
  200. basename($e->getFile()), $e->getLine());
  201. }
  202. }
  203. /**
  204. * Enable/disable strict parsing.
  205. *
  206. * If strict parsing is enabled, then most errors while loading
  207. * images will result in exceptions being thrown. Otherwise a
  208. * warning will be emitted (using {@link Pel::warning}) and the
  209. * exceptions will be stored for later use via {@link
  210. * getExceptions()}.
  211. *
  212. * Some errors will still be fatal and result in thrown exceptions,
  213. * but an effort will be made to skip over as much garbage as
  214. * possible.
  215. *
  216. * @param boolean $flag use true to enable strict parsing, false to
  217. * diable.
  218. */
  219. function setStrictParsing($flag) {
  220. self::$strict = $flag;
  221. }
  222. /**
  223. * Get current setting for strict parsing.
  224. *
  225. * @return boolean true if strict parsing is in effect, false
  226. * otherwise.
  227. */
  228. function getStrictParsing() {
  229. return self::$strict;
  230. }
  231. /**
  232. * Enable/disable debugging output.
  233. *
  234. * @param boolean $flag use true to enable debug output, false to
  235. * diable.
  236. */
  237. function setDebug($flag) {
  238. self::$debug = $flag;
  239. }
  240. /**
  241. * Get current setting for debug output.
  242. *
  243. * @return boolean true if debug is enabled, false otherwise.
  244. */
  245. function getDebug() {
  246. return self::$debug;
  247. }
  248. /**
  249. * Conditionally output debug information.
  250. *
  251. * This method works just like printf() except that it always
  252. * terminates the output with a newline, and that it only outputs
  253. * something if the {@link Pel::$debug} is true.
  254. *
  255. * @param string $format the format string.
  256. *
  257. * @param mixed $args,... any number of arguments can be given. The
  258. * arguments will be available for the format string as usual with
  259. * sprintf().
  260. */
  261. static function debug() {
  262. if (self::$debug) {
  263. $args = func_get_args();
  264. $str = array_shift($args);
  265. vprintf($str . "\n", $args);
  266. }
  267. }
  268. /**
  269. * Conditionally output a warning.
  270. *
  271. * This method works just like printf() except that it prepends the
  272. * output with the string 'Warning: ', terminates the output with a
  273. * newline, and that it only outputs something if the PEL_DEBUG
  274. * defined to some true value.
  275. *
  276. * @param string $format the format string.
  277. *
  278. * @param mixed $args,... any number of arguments can be given. The
  279. * arguments will be available for the format string as usual with
  280. * sprintf().
  281. */
  282. static function warning() {
  283. if (self::$debug) {
  284. $args = func_get_args();
  285. $str = array_shift($args);
  286. vprintf('Warning: ' . $str . "\n", $args);
  287. }
  288. }
  289. /**
  290. * Translate a string.
  291. *
  292. * This static function will use Gettext to translate a string. By
  293. * always using this function for static string one is assured that
  294. * the translation will be taken from the correct text domain.
  295. * Dynamic strings should be passed to {@link fmt} instead.
  296. *
  297. * @param string the string that should be translated.
  298. *
  299. * @return string the translated string, or the original string if
  300. * no translation could be found.
  301. */
  302. static function tra($str) {
  303. return dgettext('pel', $str);
  304. }
  305. /**
  306. * Translate and format a string.
  307. *
  308. * This static function will first use Gettext to translate a format
  309. * string, which will then have access to any extra arguments. By
  310. * always using this function for dynamic string one is assured that
  311. * the translation will be taken from the correct text domain. If
  312. * the string is static, use {@link tra} instead as it will be
  313. * faster.
  314. *
  315. * @param string $format the format string. This will be translated
  316. * before being used as a format string.
  317. *
  318. * @param mixed $args,... any number of arguments can be given. The
  319. * arguments will be available for the format string as usual with
  320. * sprintf().
  321. *
  322. * @return string the translated string, or the original string if
  323. * no translation could be found.
  324. */
  325. static function fmt() {
  326. $args = func_get_args();
  327. $str = array_shift($args);
  328. return vsprintf(dgettext('pel', $str), $args);
  329. }
  330. }