PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/susmitha.plts/photographer_portfolio
PHP | 159 lines | 80 code | 22 blank | 57 comment | 6 complexity | 4c331834420e7f40f73a0508ab664b17 MD5 | raw file
  1. <?php
  2. namespace Intervention\Image;
  3. use Closure;
  4. use Intervention\Image\ImageManager;
  5. use Illuminate\Routing\Controller as BaseController;
  6. use Illuminate\Http\Response as IlluminateResponse;
  7. use Config;
  8. class ImageCacheController extends BaseController
  9. {
  10. /**
  11. * Get HTTP response of either original image file or
  12. * template applied file.
  13. *
  14. * @param string $template
  15. * @param string $filename
  16. * @return Illuminate\Http\Response
  17. */
  18. public function getResponse($template, $filename)
  19. {
  20. switch (strtolower($template)) {
  21. case 'original':
  22. return $this->getOriginal($filename);
  23. case 'download':
  24. return $this->getDownload($filename);
  25. default:
  26. return $this->getImage($template, $filename);
  27. }
  28. }
  29. /**
  30. * Get HTTP response of template applied image file
  31. *
  32. * @param string $template
  33. * @param string $filename
  34. * @return Illuminate\Http\Response
  35. */
  36. public function getImage($template, $filename)
  37. {
  38. $template = $this->getTemplate($template);
  39. $path = $this->getImagePath($filename);
  40. // image manipulation based on callback
  41. $manager = new ImageManager(Config::get('image'));
  42. $content = $manager->cache(function ($image) use ($template, $path) {
  43. if ($template instanceof Closure) {
  44. // build from closure callback template
  45. $template($image->make($path));
  46. } else {
  47. // build from filter template
  48. $image->make($path)->filter($template);
  49. }
  50. }, config('imagecache.lifetime'));
  51. return $this->buildResponse($content);
  52. }
  53. /**
  54. * Get HTTP response of original image file
  55. *
  56. * @param string $filename
  57. * @return Illuminate\Http\Response
  58. */
  59. public function getOriginal($filename)
  60. {
  61. $path = $this->getImagePath($filename);
  62. return $this->buildResponse(file_get_contents($path));
  63. }
  64. /**
  65. * Get HTTP response of original image as download
  66. *
  67. * @param string $filename
  68. * @return Illuminate\Http\Response
  69. */
  70. public function getDownload($filename)
  71. {
  72. $response = $this->getOriginal($filename);
  73. return $response->header(
  74. 'Content-Disposition',
  75. 'attachment; filename=' . $filename
  76. );
  77. }
  78. /**
  79. * Returns corresponding template object from given template name
  80. *
  81. * @param string $template
  82. * @return mixed
  83. */
  84. private function getTemplate($template)
  85. {
  86. $template = config("imagecache.templates.{$template}");
  87. switch (true) {
  88. // closure template found
  89. case is_callable($template):
  90. return $template;
  91. // filter template found
  92. case class_exists($template):
  93. return new $template;
  94. default:
  95. // template not found
  96. abort(404);
  97. break;
  98. }
  99. }
  100. /**
  101. * Returns full image path from given filename
  102. *
  103. * @param string $filename
  104. * @return string
  105. */
  106. private function getImagePath($filename)
  107. {
  108. // find file
  109. foreach (config('imagecache.paths') as $path) {
  110. // don't allow '..' in filenames
  111. $image_path = $path.'/'.str_replace('..', '', $filename);
  112. if (file_exists($image_path) && is_file($image_path)) {
  113. // file found
  114. return $image_path;
  115. }
  116. }
  117. // file not found
  118. abort(404);
  119. }
  120. /**
  121. * Builds HTTP response from given image data
  122. *
  123. * @param string $content
  124. * @return Illuminate\Http\Response
  125. */
  126. private function buildResponse($content)
  127. {
  128. // define mime type
  129. $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
  130. // return http response
  131. return new IlluminateResponse($content, 200, array(
  132. 'Content-Type' => $mime,
  133. 'Cache-Control' => 'max-age='.(config('imagecache.lifetime')*60).', public',
  134. 'Etag' => md5($content)
  135. ));
  136. }
  137. }