ImageServiceProviderLaravel5.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Intervention\Image;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Http\Response as IlluminateResponse;
  5. class ImageServiceProviderLaravel5 extends ServiceProvider
  6. {
  7. /**
  8. * Determines if Intervention Imagecache is installed
  9. *
  10. * @return boolean
  11. */
  12. private function cacheIsInstalled()
  13. {
  14. return class_exists('Intervention\\Image\\ImageCache');
  15. }
  16. /**
  17. * Bootstrap the application events.
  18. *
  19. * @return void
  20. */
  21. public function boot()
  22. {
  23. $this->publishes(array(
  24. __DIR__.'/../../config/config.php' => config_path('image.php')
  25. ));
  26. // setup intervention/imagecache if package is installed
  27. $this->cacheIsInstalled() ? $this->bootstrapImageCache() : null;
  28. }
  29. /**
  30. * Register the service provider.
  31. *
  32. * @return void
  33. */
  34. public function register()
  35. {
  36. $app = $this->app;
  37. // merge default config
  38. $this->mergeConfigFrom(
  39. __DIR__.'/../../config/config.php',
  40. 'image'
  41. );
  42. // create image
  43. $app['image'] = $app->share(function ($app) {
  44. return new ImageManager($app['config']->get('image'));
  45. });
  46. $app->alias('image', 'Intervention\Image\ImageManager');
  47. }
  48. /**
  49. * Bootstrap imagecache
  50. *
  51. * @return void
  52. */
  53. private function bootstrapImageCache()
  54. {
  55. $app = $this->app;
  56. $config = __DIR__.'/../../../../imagecache/src/config/config.php';
  57. $this->publishes(array(
  58. $config => config_path('imagecache.php')
  59. ));
  60. // merge default config
  61. $this->mergeConfigFrom(
  62. $config,
  63. 'imagecache'
  64. );
  65. // imagecache route
  66. if (is_string(config('imagecache.route'))) {
  67. $filename_pattern = '[ \w\\.\\/\\-]+';
  68. // route to access template applied image file
  69. $app['router']->get(config('imagecache.route').'/{template}/{filename}', array(
  70. 'uses' => 'Intervention\Image\ImageCacheController@getResponse',
  71. 'as' => 'imagecache'
  72. ))->where(array('filename' => $filename_pattern));
  73. }
  74. }
  75. }