PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/intervention/imagecache/src/Intervention/Image/ImageCache.php

https://gitlab.com/susmitha.plts/photographer_portfolio
PHP | 328 lines | 137 code | 56 blank | 135 comment | 13 complexity | 4e60d0b9f72e03659a57825892c105b5 MD5 | raw file
  1. <?php
  2. namespace Intervention\Image;
  3. use Exception;
  4. use Illuminate\Cache\Repository as Cache;
  5. class ImageCache
  6. {
  7. /**
  8. * Cache lifetime in minutes
  9. *
  10. * @var integer
  11. */
  12. public $lifetime = 5;
  13. /**
  14. * History of name and arguments of calls performed on image
  15. *
  16. * @var array
  17. */
  18. public $calls = array();
  19. /**
  20. * Additional properties included in checksum
  21. *
  22. * @var array
  23. */
  24. public $properties = array();
  25. /**
  26. * Processed Image
  27. *
  28. * @var Intervention\Image\Image
  29. */
  30. public $image;
  31. /**
  32. * Intervention Image Manager
  33. *
  34. * @var Intervention\Image\ImageManager
  35. */
  36. public $manager;
  37. /**
  38. * Illuminate Cache Manager
  39. *
  40. * @var Illuminate\Cache\CacheManager
  41. */
  42. public $cache;
  43. /**
  44. * Create a new instance
  45. */
  46. public function __construct(ImageManager $manager = null, Cache $cache = null)
  47. {
  48. $this->manager = $manager ? $manager : new ImageManager;
  49. if (is_null($cache)) {
  50. // get laravel app
  51. $app = function_exists('app') ? app() : null;
  52. // if laravel app cache exists
  53. if (is_a($app, 'Illuminate\Foundation\Application')) {
  54. $cache = $app->make('cache');
  55. }
  56. if (is_a($cache, 'Illuminate\Cache\CacheManager')) {
  57. // add laravel cache
  58. $this->cache = $cache;
  59. } else {
  60. // define path in filesystem
  61. if (isset($manager->config['cache']['path'])) {
  62. $path = $manager->config['cache']['path'];
  63. } else {
  64. $path = __DIR__.'/../../../storage/cache';
  65. }
  66. // create new default cache
  67. $filesystem = new \Illuminate\Filesystem\Filesystem;
  68. $storage = new \Illuminate\Cache\FileStore($filesystem, $path);
  69. $this->cache = new \Illuminate\Cache\Repository($storage);
  70. }
  71. } else {
  72. $this->cache = $cache;
  73. }
  74. }
  75. /**
  76. * Magic method to capture action calls
  77. *
  78. * @param String $name
  79. * @param Array $arguments
  80. * @return Intervention\Image\ImageCache
  81. */
  82. public function __call($name, $arguments)
  83. {
  84. $this->registerCall($name, $arguments);
  85. return $this;
  86. }
  87. /**
  88. * Special make method to add modifed data to checksum
  89. *
  90. * @param mixed $data
  91. * @return Intervention\Image\ImageCache
  92. */
  93. public function make($data)
  94. {
  95. // include "modified" property for any files
  96. if ($this->isFile($data)) {
  97. $this->setProperty('modified', filemtime((string) $data));
  98. }
  99. // register make call
  100. $this->__call('make', array($data));
  101. return $this;
  102. }
  103. /**
  104. * Checks if given data is file, handles mixed input
  105. *
  106. * @param mixed $value
  107. * @return boolean
  108. */
  109. private function isFile($value)
  110. {
  111. $value = strval(str_replace("\0", "", $value));
  112. return is_file($value);
  113. }
  114. /**
  115. * Set custom property to be included in checksum
  116. *
  117. * @param mixed $key
  118. * @param mixed $value
  119. * @return Intervention\Image\ImageCache
  120. */
  121. public function setProperty($key, $value)
  122. {
  123. $this->properties[$key] = $value;
  124. return $this;
  125. }
  126. /**
  127. * Returns checksum of current image state
  128. *
  129. * @return string
  130. */
  131. public function checksum()
  132. {
  133. $properties = serialize($this->properties);
  134. $calls = serialize($this->getSanitizedCalls());
  135. return md5($properties.$calls);
  136. }
  137. /**
  138. * Register static call for later use
  139. *
  140. * @param string $name
  141. * @param array $arguments
  142. * @return void
  143. */
  144. private function registerCall($name, $arguments)
  145. {
  146. $this->calls[] = array('name' => $name, 'arguments' => $arguments);
  147. }
  148. /**
  149. * Clears history of calls
  150. *
  151. * @return void
  152. */
  153. private function clearCalls()
  154. {
  155. $this->calls = array();
  156. }
  157. /**
  158. * Clears all currently set properties
  159. *
  160. * @return void
  161. */
  162. private function clearProperties()
  163. {
  164. $this->properties = array();
  165. }
  166. /**
  167. * Return unprocessed calls
  168. *
  169. * @return array
  170. */
  171. private function getCalls()
  172. {
  173. return count($this->calls) ? $this->calls : array();
  174. }
  175. /**
  176. * Replace Closures in arguments with SerializableClosure
  177. *
  178. * @return array
  179. */
  180. private function getSanitizedCalls()
  181. {
  182. $calls = $this->getCalls();
  183. foreach ($calls as $i => $call) {
  184. foreach ($call['arguments'] as $j => $argument) {
  185. if (is_a($argument, 'Closure')) {
  186. $calls[$i]['arguments'][$j] = $this->buildSerializableClosure($argument);
  187. }
  188. }
  189. }
  190. return $calls;
  191. }
  192. /**
  193. * Build SerializableClosure from Closure
  194. *
  195. * @param Closure $closure
  196. * @return Jeremeamia\SuperClosure\SerializableClosure|SuperClosure\SerializableClosure
  197. */
  198. private function buildSerializableClosure(\Closure $closure)
  199. {
  200. switch (true) {
  201. case class_exists('SuperClosure\\SerializableClosure'):
  202. return new \SuperClosure\SerializableClosure($closure);
  203. default:
  204. return new \Jeremeamia\SuperClosure\SerializableClosure($closure);
  205. }
  206. }
  207. /**
  208. * Process call on current image
  209. *
  210. * @param array $call
  211. * @return void
  212. */
  213. private function processCall($call)
  214. {
  215. $this->image = call_user_func_array(array($this->image, $call['name']), $call['arguments']);
  216. }
  217. /**
  218. * Process all saved image calls on Image object
  219. *
  220. * @return Intervention\Image\Image
  221. */
  222. public function process()
  223. {
  224. // first call on manager
  225. $this->image = $this->manager;
  226. // process calls on image
  227. foreach ($this->getCalls() as $call) {
  228. $this->processCall($call);
  229. }
  230. // append checksum to image
  231. $this->image->cachekey = $this->checksum();
  232. // clean-up
  233. $this->clearCalls();
  234. $this->clearProperties();
  235. return $this->image;
  236. }
  237. /**
  238. * Get image either from cache or directly processed
  239. * and save image in cache if it's not saved yet
  240. *
  241. * @param int $lifetime
  242. * @param bool $returnObj
  243. * @return mixed
  244. */
  245. public function get($lifetime = null, $returnObj = false)
  246. {
  247. $lifetime = is_null($lifetime) ? $this->lifetime : intval($lifetime);
  248. $key = $this->checksum();
  249. // try to get image from cache
  250. $cachedImageData = $this->cache->get($key);
  251. // if imagedata exists in cache
  252. if ($cachedImageData) {
  253. // transform into image-object
  254. if ($returnObj) {
  255. $image = $this->manager->make($cachedImageData);
  256. $cachedImage = new CachedImage;
  257. return $cachedImage->setFromOriginal($image, $key);
  258. }
  259. // return raw data
  260. return $cachedImageData;
  261. } else {
  262. // process image data
  263. $image = $this->process();
  264. // encode image data only if image is not encoded yet
  265. $encoded = $image->encoded ? $image->encoded : (string) $image->encode();
  266. // save to cache...
  267. $this->cache->put($key, $encoded, $lifetime);
  268. // return processed image
  269. return $returnObj ? $image : $encoded;
  270. }
  271. }
  272. }