PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/julienv/joomleague
PHP | 323 lines | 116 code | 26 blank | 181 comment | 4 complexity | 20b5534aa12f0f7c5f4af6b8a11c39d5 MD5 | raw file
  1. <?php
  2. /**
  3. * PhpThumb Base Class Definition File
  4. *
  5. * This file contains the definition for the ThumbBase object
  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. * ThumbBase Class Definition
  26. *
  27. * This is the base class that all implementations must extend. It contains the
  28. * core variables and functionality common to all implementations, as well as the functions that
  29. * allow plugins to augment those classes.
  30. *
  31. * @package PhpThumb
  32. * @subpackage Core
  33. */
  34. abstract class ThumbBase
  35. {
  36. /**
  37. * All imported objects
  38. *
  39. * An array of imported plugin objects
  40. *
  41. * @var array
  42. */
  43. protected $imported;
  44. /**
  45. * All imported object functions
  46. *
  47. * An array of all methods added to this class by imported plugin objects
  48. *
  49. * @var array
  50. */
  51. protected $importedFunctions;
  52. /**
  53. * The last error message raised
  54. *
  55. * @var string
  56. */
  57. protected $errorMessage;
  58. /**
  59. * Whether or not the current instance has any errors
  60. *
  61. * @var bool
  62. */
  63. protected $hasError;
  64. /**
  65. * The name of the file we're manipulating
  66. *
  67. * This must include the path to the file (absolute paths recommended)
  68. *
  69. * @var string
  70. */
  71. protected $fileName;
  72. /**
  73. * What the file format is (mime-type)
  74. *
  75. * @var string
  76. */
  77. protected $format;
  78. /**
  79. * Whether or not the image is hosted remotely
  80. *
  81. * @var bool
  82. */
  83. protected $remoteImage;
  84. /**
  85. * Whether or not the current image is an actual file, or the raw file data
  86. *
  87. * By "raw file data" it's meant that we're actually passing the result of something
  88. * like file_get_contents() or perhaps from a database blob
  89. *
  90. * @var bool
  91. */
  92. protected $isDataStream;
  93. /**
  94. * Class constructor
  95. *
  96. * @return ThumbBase
  97. */
  98. public function __construct ($fileName, $isDataStream = false)
  99. {
  100. $this->imported = array();
  101. $this->importedFunctions = array();
  102. $this->errorMessage = null;
  103. $this->hasError = false;
  104. $this->fileName = $fileName;
  105. $this->remoteImage = false;
  106. $this->isDataStream = $isDataStream;
  107. $this->fileExistsAndReadable();
  108. }
  109. /**
  110. * Imports plugins in $registry to the class
  111. *
  112. * @param array $registry
  113. */
  114. public function importPlugins ($registry)
  115. {
  116. foreach ($registry as $plugin => $meta)
  117. {
  118. $this->imports($plugin);
  119. }
  120. }
  121. /**
  122. * Imports a plugin
  123. *
  124. * This is where all the plugins magic happens! This function "loads" the plugin functions, making them available as
  125. * methods on the class.
  126. *
  127. * @param string $object The name of the object to import / "load"
  128. */
  129. protected function imports ($object)
  130. {
  131. // the new object to import
  132. $newImport = new $object();
  133. // the name of the new object (class name)
  134. $importName = get_class($newImport);
  135. // the new functions to import
  136. $importFunctions = get_class_methods($newImport);
  137. // add the object to the registry
  138. array_push($this->imported, array($importName, $newImport));
  139. // add the methods to the registry
  140. foreach ($importFunctions as $key => $functionName)
  141. {
  142. $this->importedFunctions[$functionName] = &$newImport;
  143. }
  144. }
  145. /**
  146. * Checks to see if $this->fileName exists and is readable
  147. *
  148. */
  149. protected function fileExistsAndReadable ()
  150. {
  151. if ($this->isDataStream === true)
  152. {
  153. return;
  154. }
  155. if (stristr($this->fileName, 'http://') !== false)
  156. {
  157. $this->remoteImage = true;
  158. return;
  159. }
  160. if (!file_exists($this->fileName))
  161. {
  162. $this->triggerError('Image file not found: ' . $this->fileName);
  163. }
  164. elseif (!is_readable($this->fileName))
  165. {
  166. $this->triggerError('Image file not readable: ' . $this->fileName);
  167. }
  168. }
  169. /**
  170. * Sets $this->errorMessage to $errorMessage and throws an exception
  171. *
  172. * Also sets $this->hasError to true, so even if the exceptions are caught, we don't
  173. * attempt to proceed with any other functions
  174. *
  175. * @param string $errorMessage
  176. */
  177. protected function triggerError ($errorMessage)
  178. {
  179. $this->hasError = true;
  180. $this->errorMessage = $errorMessage;
  181. throw new Exception ($errorMessage);
  182. }
  183. /**
  184. * Calls plugin / imported functions
  185. *
  186. * This is also where a fair amount of plugins magaic happens. This magic method is called whenever an "undefined" class
  187. * method is called in code, and we use that to call an imported function.
  188. *
  189. * You should NEVER EVER EVER invoke this function manually. The universe will implode if you do... seriously ;)
  190. *
  191. * @param string $method
  192. * @param array $args
  193. */
  194. public function __call ($method, $args)
  195. {
  196. if( array_key_exists($method, $this->importedFunctions))
  197. {
  198. $args[] = $this;
  199. return call_user_func_array(array($this->importedFunctions[$method], $method), $args);
  200. }
  201. throw new BadMethodCallException ('Call to undefined method/class function: ' . $method);
  202. }
  203. /**
  204. * Returns $imported.
  205. * @see ThumbBase::$imported
  206. * @return array
  207. */
  208. public function getImported ()
  209. {
  210. return $this->imported;
  211. }
  212. /**
  213. * Returns $importedFunctions.
  214. * @see ThumbBase::$importedFunctions
  215. * @return array
  216. */
  217. public function getImportedFunctions ()
  218. {
  219. return $this->importedFunctions;
  220. }
  221. /**
  222. * Returns $errorMessage.
  223. *
  224. * @see ThumbBase::$errorMessage
  225. */
  226. public function getErrorMessage ()
  227. {
  228. return $this->errorMessage;
  229. }
  230. /**
  231. * Sets $errorMessage.
  232. *
  233. * @param object $errorMessage
  234. * @see ThumbBase::$errorMessage
  235. */
  236. public function setErrorMessage ($errorMessage)
  237. {
  238. $this->errorMessage = $errorMessage;
  239. }
  240. /**
  241. * Returns $fileName.
  242. *
  243. * @see ThumbBase::$fileName
  244. */
  245. public function getFileName ()
  246. {
  247. return $this->fileName;
  248. }
  249. /**
  250. * Sets $fileName.
  251. *
  252. * @param object $fileName
  253. * @see ThumbBase::$fileName
  254. */
  255. public function setFileName ($fileName)
  256. {
  257. $this->fileName = $fileName;
  258. }
  259. /**
  260. * Returns $format.
  261. *
  262. * @see ThumbBase::$format
  263. */
  264. public function getFormat ()
  265. {
  266. return $this->format;
  267. }
  268. /**
  269. * Sets $format.
  270. *
  271. * @param object $format
  272. * @see ThumbBase::$format
  273. */
  274. public function setFormat ($format)
  275. {
  276. $this->format = $format;
  277. }
  278. /**
  279. * Returns $hasError.
  280. *
  281. * @see ThumbBase::$hasError
  282. */
  283. public function getHasError ()
  284. {
  285. return $this->hasError;
  286. }
  287. /**
  288. * Sets $hasError.
  289. *
  290. * @param object $hasError
  291. * @see ThumbBase::$hasError
  292. */
  293. public function setHasError ($hasError)
  294. {
  295. $this->hasError = $hasError;
  296. }
  297. }