ImageServiceProviderLaravel4.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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}', array('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)) {
  46. // image manipulation based on callback
  47. $content = $app['image']->cache(function ($image) use ($image_path, $callback) {
  48. return $callback($image->make($image_path));
  49. }, $config->get('imagecache::lifetime'));
  50. } else {
  51. // get original image file contents
  52. $content = file_get_contents($image_path);
  53. }
  54. // define mime type
  55. $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
  56. // return http response
  57. return new IlluminateResponse($content, 200, array(
  58. 'Content-Type' => $mime,
  59. 'Cache-Control' => 'max-age='.($config->get('imagecache::lifetime')*60).', public',
  60. 'Etag' => md5($content)
  61. ));
  62. }))->where(array('template' => join('|', array_keys($config->get('imagecache::templates'))), 'filename' => '[ \w\\.\\/\\-]+'));
  63. }
  64. }
  65. }
  66. /**
  67. * Register the service provider.
  68. *
  69. * @return void
  70. */
  71. public function register()
  72. {
  73. $app = $this->app;
  74. $app['image'] = $app->share(function ($app) {
  75. return new ImageManager($app['config']->get('image::config'));
  76. });
  77. $app->alias('image', 'Intervention\Image\ImageManager');
  78. }
  79. }