PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/_/app/Http/Controllers/ImageController.php

https://gitlab.com/billyprice1/MFAdmin
PHP | 148 lines | 117 code | 31 blank | 0 comment | 16 complexity | cf151722d652f5595190588d52ada5b5 MD5 | raw file
  1. <?php namespace App\Http\Controllers;
  2. use Intervention\Image\ImageManagerStatic;
  3. abstract class ImageController extends Controller
  4. {
  5. const DEFAULT_CANVAS_BACKGROUND_COLOR = '#FFFFFF';
  6. public static function deleteImage($reference_id)
  7. {
  8. $called_class = get_called_class();
  9. }
  10. public static function generateSizes($reference, \Intervention\Image\Image $original_image, array $image_data, $directory)
  11. {
  12. $called_class = get_called_class();
  13. if ( !method_exists($called_class, 'getSizes') )
  14. {
  15. throw new \Exception($called_class . ' is missing required function "getSizes".');
  16. }
  17. if ( !method_exists($called_class, 'getColumnID') )
  18. {
  19. throw new \Exception($called_class . ' is missing required function "getColumnID".');
  20. }
  21. foreach ( $called_class::getSizes() as $size_id => $size_data )
  22. {
  23. $generated_image = clone $original_image;
  24. $generated_image = self::generateSize($generated_image, $size_data);
  25. $filename = $reference->id . '-' . $size_id . ($image_data['index'] > 0 ? '-' . $image_data['index'] : '') . '.' . $image_data['file_extension'];
  26. $path = $directory . $filename;
  27. $generated_image->save($path, 100);
  28. if ( $image_data['index'] > 0 )
  29. {
  30. $old_filename = $reference->id . '-' . $size_id . ($image_data['index'] > 1 ? '-' . ($image_data['index'] - 1) : '') . '.' . $image_data['file_extension'];
  31. $old_path = $directory . $old_filename;
  32. if ( file_exists($old_path) )
  33. {
  34. unlink($old_path);
  35. }
  36. }
  37. $generated_image->destroy();
  38. }
  39. }
  40. private static function generateSize(\Intervention\Image\Image $image, $size_data)
  41. {
  42. $image->resize((isset($size_data['width']) ? $size_data['width'] : NULL), (isset($size_data['height']) ? $size_data['height'] : NULL), function ($constraint) use ($size_data)
  43. {
  44. if ( !isset($size_data['proportional']) || $size_data['proportional'] === TRUE )
  45. {
  46. $constraint->aspectRatio();
  47. }
  48. if ( !isset($size_data['upsize']) || $size_data['upsize'] === FALSE )
  49. {
  50. $constraint->upsize();
  51. }
  52. });
  53. if ( isset($size_data['crop']) )
  54. {
  55. $image->crop($size_data['crop']['width'], $size_data['crop']['height'], $size_data['crop']['x'], $size_data['crop']['y']);
  56. }
  57. if ( isset($size_data['canvas']) )
  58. {
  59. $image->resizeCanvas((isset($size_data['canvas']['width']) ? $size_data['canvas']['width'] : NULL), (isset($size_data['canvas']['height']) ? $size_data['canvas']['height'] : NULL), 'center', FALSE, (isset($size_data['canvas']['background_color']) ? $size_data['canvas']['background_color'] : self::DEFAULT_CANVAS_BACKGROUND_COLOR));
  60. }
  61. return $image;
  62. }
  63. public function render()
  64. {
  65. $called_class = get_called_class();
  66. $image_data = $called_class::subRender(func_get_args());
  67. $headers =
  68. [
  69. 'Cache-Control' => 'must-revalidate',
  70. 'Pragma' => 'public'
  71. ];
  72. if ( $image_data['processing'] === FALSE )
  73. {
  74. $lifetime = 0;
  75. if ( file_exists($image_data['path']) )
  76. {
  77. $file_modified_time = filemtime($image_data['path']);
  78. $etag = md5($file_modified_time . $image_data['path']);
  79. $time = gmdate('r', $file_modified_time);
  80. $expires = gmdate('r', $file_modified_time + $lifetime);
  81. $headers['Last-Modified'] = $time;
  82. $headers['Expires'] = $expires;
  83. $headers['Etag'] = $etag;
  84. $if_modified_since_header = (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] === $time);
  85. $if_none_match_header = (isset($_SERVER['HTTP_IF_NONE_MATCH']) && str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) === $etag);
  86. if ( $if_modified_since_header || $if_none_match_header )
  87. {
  88. return \Response::make('', 304, $headers);
  89. }
  90. $image_raw = ImageManagerStatic::make($image_data['path'])->response($image_data['file_extension'])->original;
  91. $image_size = strlen($image_raw);
  92. }
  93. else
  94. {
  95. $image_raw = ImageManagerStatic::make(public_path('libs/bower/semantic/examples/images/wireframe/image.png'));
  96. $image_raw = self::generateSize($image_raw, $image_data['size_data'])->response($image_data['file_extension'])->original;
  97. $image_size = strlen($image_raw);
  98. }
  99. }
  100. else
  101. {
  102. $image_raw = ImageManagerStatic::canvas(800, 600, '#CCCCCC');
  103. $image_raw = self::generateSize($image_raw, $image_data['size_data'])->response($image_data['file_extension'])->original;
  104. $image_size = strlen($image_raw);
  105. }
  106. $headers = array_merge
  107. (
  108. $headers,
  109. array
  110. (
  111. 'Content-Type' => 'image/jpeg',
  112. 'Content-Length' => $image_size,
  113. )
  114. );
  115. return \Response::make($image_raw, 200, $headers);
  116. }
  117. }