PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Core/Handlers/PostImageHandler.php

https://gitlab.com/wubbajack/insided-test
PHP | 288 lines | 130 code | 39 blank | 119 comment | 13 complexity | abe396f5c02ff0f836231b37cb15adbc MD5 | raw file
  1. <?php namespace App\Core\Handlers;
  2. use Intervention\Image\Constraint;
  3. use Intervention\Image\ImageManager;
  4. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
  5. /**
  6. * Class PostImageHandler
  7. *
  8. * @author Chris Stolk <stolkchris@gmail.com>
  9. * @package App\Core\Handlers
  10. * @copyright Chris Stolk
  11. * @since 03/07/16 11:23
  12. */
  13. class PostImageHandler
  14. {
  15. /**
  16. * @var ImageManager
  17. */
  18. protected $image;
  19. /**
  20. * @var MimeTypeGuesser
  21. */
  22. protected $guesser;
  23. /**
  24. * @var string
  25. */
  26. protected $base_path;
  27. /**
  28. * @var int
  29. */
  30. protected $thumb_width = 640;
  31. /**
  32. * @var int
  33. */
  34. protected $thumb_height = 480;
  35. /**
  36. * PostImageHandler constructor.
  37. *
  38. * @param ImageManager $imageManager
  39. * @param string $base_path
  40. */
  41. public function __construct(ImageManager $imageManager, $base_path)
  42. {
  43. $this->image = $imageManager;
  44. $this->setBasePath($base_path);
  45. // Not instantiable using the container, follows singleton principle in object
  46. $this->guesser = MimeTypeGuesser::getInstance();
  47. }
  48. /**
  49. * Returns the base path
  50. *
  51. * @return string
  52. */
  53. public function getBasePath()
  54. {
  55. return $this->base_path;
  56. }
  57. /**
  58. * Gets the thumbnail width
  59. *
  60. * @return int
  61. */
  62. public function getThumbWidth()
  63. {
  64. return $this->thumb_width;
  65. }
  66. /**
  67. * Sets the thumbnail width
  68. *
  69. * @param int $thumb_width
  70. * @return PostImageHandler
  71. */
  72. public function setThumbWidth($thumb_width)
  73. {
  74. $this->thumb_width = (int) $thumb_width;
  75. return $this;
  76. }
  77. /**
  78. * Gets the thumbnail height
  79. *
  80. * @return int
  81. */
  82. public function getThumbHeight()
  83. {
  84. return $this->thumb_height;
  85. }
  86. /**
  87. * Sets the thumbnail height
  88. *
  89. * @param int $thumb_height
  90. * @return PostImageHandler
  91. */
  92. public function setThumbHeight($thumb_height)
  93. {
  94. $this->thumb_height = (int) $thumb_height;
  95. return $this;
  96. }
  97. /**
  98. * Stores the original image
  99. *
  100. * @param \SplFileInfo $file
  101. * @param $extension
  102. * @return string
  103. * @throws \Exception
  104. */
  105. public function storeImage(\SplFileInfo $file, $extension)
  106. {
  107. $filename = $this->makeFilename($file, null, $extension);
  108. $path = $this->makePath($filename);
  109. try {
  110. $this->copyFile($file, $path);
  111. } catch (\Exception $e) {
  112. $this->cleanup($path);
  113. throw $e;
  114. }
  115. return str_replace($this->base_path, '', $path);
  116. }
  117. /**
  118. * Stores the thumbnail for the image
  119. *
  120. * @param \SplFileInfo $file
  121. * @return string
  122. * @throws \Exception
  123. */
  124. public function storeThumbnail(\SplFileInfo $file)
  125. {
  126. $filename = $this->makeFilename($file, 'thumbnail_', '.jpg');
  127. $path = $this->makePath($filename);
  128. $image = $this->image->make($file->getRealPath());
  129. $height = $image->height();
  130. $width = $image->width();
  131. if ($height > $this->thumb_height || $width > $this->thumb_width) {
  132. $constraint = function (Constraint $constraint) {
  133. $constraint->aspectRatio();
  134. $constraint->upsize();
  135. };
  136. if ($height > $width) {
  137. $image->heighten($this->thumb_height, $constraint);
  138. } else {
  139. $image->widen($this->thumb_width, $constraint);
  140. }
  141. }
  142. try {
  143. file_put_contents($path, $image->encode('jpeg', 75));
  144. } catch (\Exception $e) {
  145. $this->cleanup($path);
  146. throw $e;
  147. }
  148. return str_replace($this->base_path, '', $path);
  149. }
  150. /**
  151. * Copies a file to a target path
  152. *
  153. * @param \SplFileInfo $file
  154. * @param string $path
  155. * @return bool
  156. */
  157. protected function copyFile(\SplFileInfo $file, $path)
  158. {
  159. $source = fopen($file, 'r');
  160. $target = fopen($path, 'w+');
  161. stream_copy_to_stream($source, $target);
  162. fclose($source);
  163. fclose($target);
  164. return true;
  165. }
  166. /**
  167. * Checks whether the file is a Gif
  168. *
  169. * @param \SplFileInfo $file
  170. * @return bool
  171. */
  172. protected function isGif(\SplFileInfo $file)
  173. {
  174. return $this->guesser->guess($file->getRealPath()) == 'image/gif';
  175. }
  176. /**
  177. * Sets the base path
  178. *
  179. * @param string $base_path
  180. * @return void
  181. */
  182. protected function setBasePath($base_path)
  183. {
  184. if (substr($base_path, -1, 1) != '/') {
  185. $base_path .= '/';
  186. }
  187. $this->base_path = $base_path;
  188. }
  189. /**
  190. * Makes the path
  191. *
  192. * @param string $filename
  193. * @return string
  194. */
  195. protected function makePath($filename)
  196. {
  197. $path = $this->base_path . $this->shard($filename);
  198. $dir = dirname($path);
  199. if (!is_dir($dir)) {
  200. mkdir($dir, 0777, true);
  201. }
  202. return $path;
  203. }
  204. /**
  205. * Returns the filename
  206. *
  207. * @param \SplFileInfo $file
  208. * @param string $prefix
  209. * @param null $extension
  210. * @return string
  211. */
  212. protected function makeFilename(\SplFileInfo $file, $prefix = null, $extension = null)
  213. {
  214. if ($extension && substr($extension, 0, 1) != '.') {
  215. $extension = '.'.$extension;
  216. }
  217. return hash('sha1', $prefix . microtime() . $file->getBasename()) . $extension;
  218. }
  219. /**
  220. * Shards the filename (for a correct path)
  221. *
  222. * @param string $filename
  223. * @param int $parts
  224. * @param int $size
  225. * @return mixed
  226. */
  227. protected function shard($filename, $parts = 3, $size = 4)
  228. {
  229. $shards = [];
  230. for ($i = 0; $i < $parts; $i++) {
  231. $shards[] = substr($filename, 0 + ($size * $i), $size);
  232. }
  233. // Quicker than array merge
  234. $shards[] = $filename;
  235. return implode(DIRECTORY_SEPARATOR, $shards);
  236. }
  237. /**
  238. * If a file has been created we perform cleanup
  239. *
  240. * @param string $path
  241. */
  242. protected function cleanup($path)
  243. {
  244. if (!is_file($path)) {
  245. return;
  246. }
  247. @unlink($path);
  248. }
  249. }