ImageServiceProviderLaravel4.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Intervention\Image;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Http\Response as IlluminateResponse;
  5. class ImageServiceProviderLaravel4 extends ServiceProvider
  6. {
  7. /**
  8. * Bootstrap the application events.
  9. *
  10. * @return void
  11. */
  12. public function boot()
  13. {
  14. $this->package('intervention/image');
  15. // try to create imagecache route only if imagecache is present
  16. if (class_exists('Intervention\\Image\\ImageCache')) {
  17. $app = $this->app;
  18. // load imagecache config
  19. $app['config']->package('intervention/imagecache', __DIR__.'/../../../../imagecache/src/config', 'imagecache');
  20. $config = $app['config'];
  21. // create dynamic manipulation route
  22. if (is_string($config->get('imagecache::route'))) {
  23. // add original to route templates
  24. $config->set('imagecache::templates.original', null);
  25. // setup image manipulator route
  26. $app['router']->get($config->get('imagecache::route').'/{template}/{filename}', ['as' => 'imagecache', function ($template, $filename) use ($app, $config) {
  27. // disable session cookies for image route
  28. $app['config']->set('session.driver', 'array');
  29. // find file
  30. foreach ($config->get('imagecache::paths') as $path) {
  31. // don't allow '..' in filenames
  32. $image_path = $path.'/'.str_replace('..', '', $filename);
  33. if (file_exists($image_path) && is_file($image_path)) {
  34. break;
  35. } else {
  36. $image_path = false;
  37. }
  38. }
  39. // abort if file not found
  40. if ($image_path === false) {
  41. $app->abort(404);
  42. }
  43. // define template callback
  44. $callback = $config->get("imagecache::templates.{$template}");
  45. if (is_callable($callback) || class_exists($callback)) {
  46. // image manipulation based on callback
  47. $content = $app['image']->cache(function ($image) use ($image_path, $callback) {
  48. switch (true) {
  49. case is_callable($callback):
  50. return $callback($image->make($image_path));
  51. break;
  52. case class_exists($callback):
  53. return $image->make($image_path)->filter(new $callback);
  54. break;
  55. }
  56. }, $config->get('imagecache::lifetime'));
  57. } else {
  58. // get original image file contents
  59. $content = file_get_contents($image_path);
  60. }
  61. // define mime type
  62. $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
  63. // return http response
  64. return new IlluminateResponse($content, 200, [
  65. 'Content-Type' => $mime,
  66. 'Cache-Control' => 'max-age='.($config->get('imagecache::lifetime')*60).', public',
  67. 'Etag' => md5($content)
  68. ]);
  69. }])->where(['template' => join('|', array_keys($config->get('imagecache::templates'))), 'filename' => '[ \w\\.\\/\\-]+']);
  70. }
  71. }
  72. }
  73. /**
  74. * Register the service provider.
  75. *
  76. * @return void
  77. */
  78. public function register()
  79. {
  80. $app = $this->app;
  81. $app['image'] = $app->share(function ($app) {
  82. return new ImageManager($app['config']->get('image::config'));
  83. });
  84. $app->alias('image', 'Intervention\Image\ImageManager');
  85. }
  86. }