PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/com_joomleague/components/com_joomleague/assets/classes/PHPThumb/PhpThumb.inc.php

https://gitlab.com/julienv/joomleague
PHP | 247 lines | 97 code | 22 blank | 128 comment | 26 complexity | 4290c9f318393fe5efce5853a2bccfeb MD5 | raw file
  1. <?php
  2. /**
  3. * PhpThumb Library Definition File
  4. *
  5. * This file contains the definitions for the PhpThumb class.
  6. *
  7. * PHP Version 5 with GD 2.0+
  8. * PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
  9. * Copyright (c) 2009, Ian Selby/Gen X Design
  10. *
  11. * Author(s): Ian Selby <ian@gen-x-design.com>
  12. *
  13. * Licensed under the MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @author Ian Selby <ian@gen-x-design.com>
  17. * @copyright Copyright (c) 2009 Gen X Design
  18. * @link http://phpthumb.gxdlabs.com
  19. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  20. * @version 3.0
  21. * @package PhpThumb
  22. * @filesource
  23. */
  24. /**
  25. * PhpThumb Object
  26. *
  27. * This singleton object is essentially a function library that helps with core validation
  28. * and loading of the core classes and plugins. There isn't really any need to access it directly,
  29. * unless you're developing a plugin and need to take advantage of any of the functionality contained
  30. * within.
  31. *
  32. * If you're not familiar with singleton patterns, here's how you get an instance of this class (since you
  33. * can't create one via the new keyword):
  34. * <code>$pt = PhpThumb::getInstance();</code>
  35. *
  36. * It's that simple! Outside of that, there's no need to modify anything within this class, unless you're doing
  37. * some crazy customization... then knock yourself out! :)
  38. *
  39. * @package PhpThumb
  40. * @subpackage Core
  41. */
  42. class PhpThumb
  43. {
  44. /**
  45. * Instance of self
  46. *
  47. * @var object PhpThumb
  48. */
  49. protected static $_instance;
  50. /**
  51. * The plugin registry
  52. *
  53. * This is where all plugins to be loaded are stored. Data about the plugin is
  54. * provided, and currently consists of:
  55. * - loaded: true/false
  56. * - implementation: gd/imagick/both
  57. *
  58. * @var array
  59. */
  60. protected $_registry;
  61. /**
  62. * What implementations are available
  63. *
  64. * This stores what implementations are available based on the loaded
  65. * extensions in PHP, NOT whether or not the class files are present.
  66. *
  67. * @var array
  68. */
  69. protected $_implementations;
  70. /**
  71. * Returns an instance of self
  72. *
  73. * This is the usual singleton function that returns / instantiates the object
  74. *
  75. * @return PhpThumb
  76. */
  77. public static function getInstance ()
  78. {
  79. if(!(self::$_instance instanceof self))
  80. {
  81. self::$_instance = new self();
  82. }
  83. return self::$_instance;
  84. }
  85. /**
  86. * Class constructor
  87. *
  88. * Initializes all the variables, and does some preliminary validation / checking of stuff
  89. *
  90. */
  91. private function __construct ()
  92. {
  93. $this->_registry = array();
  94. $this->_implementations = array('gd' => false, 'imagick' => false);
  95. $this->getImplementations();
  96. }
  97. /**
  98. * Finds out what implementations are available
  99. *
  100. * This function loops over $this->_implementations and validates that the required extensions are loaded.
  101. *
  102. * I had planned on attempting to load them dynamically via dl(), but that would provide more overhead than I
  103. * was comfortable with (and would probably fail 99% of the time anyway)
  104. *
  105. */
  106. private function getImplementations ()
  107. {
  108. foreach($this->_implementations as $extension => $loaded)
  109. {
  110. if($loaded)
  111. {
  112. continue;
  113. }
  114. if(extension_loaded($extension))
  115. {
  116. $this->_implementations[$extension] = true;
  117. }
  118. }
  119. }
  120. /**
  121. * Returns whether or not $implementation is valid (available)
  122. *
  123. * If 'all' is passed, true is only returned if ALL implementations are available.
  124. *
  125. * You can also pass 'n/a', which always returns true
  126. *
  127. * @return bool
  128. * @param string $implementation
  129. */
  130. public function isValidImplementation ($implementation)
  131. {
  132. if ($implementation == 'n/a')
  133. {
  134. return true;
  135. }
  136. if ($implementation == 'all')
  137. {
  138. foreach ($this->_implementations as $imp => $value)
  139. {
  140. if ($value == false)
  141. {
  142. return false;
  143. }
  144. }
  145. return true;
  146. }
  147. if (array_key_exists($implementation, $this->_implementations))
  148. {
  149. return $this->_implementations[$implementation];
  150. }
  151. return false;
  152. }
  153. /**
  154. * Registers a plugin in the registry
  155. *
  156. * Adds a plugin to the registry if it isn't already loaded, and if the provided
  157. * implementation is valid. Note that you can pass the following special keywords
  158. * for implementation:
  159. * - all - Requires that all implementations be available
  160. * - n/a - Doesn't require any implementation
  161. *
  162. * When a plugin is added to the registry, it's added as a key on $this->_registry with the value
  163. * being an array containing the following keys:
  164. * - loaded - whether or not the plugin has been "loaded" into the core class
  165. * - implementation - what implementation this plugin is valid for
  166. *
  167. * @return bool
  168. * @param string $pluginName
  169. * @param string $implementation
  170. */
  171. public function registerPlugin ($pluginName, $implementation)
  172. {
  173. if (!array_key_exists($pluginName, $this->_registry) && $this->isValidImplementation($implementation))
  174. {
  175. $this->_registry[$pluginName] = array('loaded' => false, 'implementation' => $implementation);
  176. return true;
  177. }
  178. return false;
  179. }
  180. /**
  181. * Loads all the plugins in $pluginPath
  182. *
  183. * All this function does is include all files inside the $pluginPath directory. The plugins themselves
  184. * will not be added to the registry unless you've properly added the code to do so inside your plugin file.
  185. *
  186. * @param string $pluginPath
  187. */
  188. public function loadPlugins ($pluginPath)
  189. {
  190. // strip the trailing slash if present
  191. if (substr($pluginPath, strlen($pluginPath) - 1, 1) == '/')
  192. {
  193. $pluginPath = substr($pluginPath, 0, strlen($pluginPath) - 1);
  194. }
  195. if ($handle = opendir($pluginPath))
  196. {
  197. while (false !== ($file = readdir($handle)))
  198. {
  199. if ($file == '.' || $file == '..' || $file == '.svn')
  200. {
  201. continue;
  202. }
  203. include_once($pluginPath . '/' . $file);
  204. }
  205. }
  206. }
  207. /**
  208. * Returns the plugin registry for the supplied implementation
  209. *
  210. * @return array
  211. * @param string $implementation
  212. */
  213. public function getPluginRegistry ($implementation)
  214. {
  215. $returnArray = array();
  216. foreach ($this->_registry as $plugin => $meta)
  217. {
  218. if ($meta['implementation'] == 'n/a' || $meta['implementation'] == $implementation)
  219. {
  220. $returnArray[$plugin] = $meta;
  221. }
  222. }
  223. return $returnArray;
  224. }
  225. }