File indexing completed on 2025-01-26 05:29:15
0001 <?php 0002 0003 namespace Intervention\Image; 0004 0005 use Illuminate\Support\ServiceProvider; 0006 use Illuminate\Http\Response as IlluminateResponse; 0007 0008 class ImageServiceProviderLaravel4 extends ServiceProvider 0009 { 0010 /** 0011 * Bootstrap the application events. 0012 * 0013 * @return void 0014 */ 0015 public function boot() 0016 { 0017 $this->package('intervention/image'); 0018 0019 // try to create imagecache route only if imagecache is present 0020 if (class_exists('Intervention\\Image\\ImageCache')) { 0021 0022 $app = $this->app; 0023 0024 // load imagecache config 0025 $app['config']->package('intervention/imagecache', __DIR__.'/../../../../imagecache/src/config', 'imagecache'); 0026 $config = $app['config']; 0027 0028 // create dynamic manipulation route 0029 if (is_string($config->get('imagecache::route'))) { 0030 0031 // add original to route templates 0032 $config->set('imagecache::templates.original', null); 0033 0034 // setup image manipulator route 0035 $app['router']->get($config->get('imagecache::route').'/{template}/{filename}', ['as' => 'imagecache', function ($template, $filename) use ($app, $config) { 0036 0037 // disable session cookies for image route 0038 $app['config']->set('session.driver', 'array'); 0039 0040 // find file 0041 foreach ($config->get('imagecache::paths') as $path) { 0042 // don't allow '..' in filenames 0043 $image_path = $path.'/'.str_replace('..', '', $filename); 0044 if (file_exists($image_path) && is_file($image_path)) { 0045 break; 0046 } else { 0047 $image_path = false; 0048 } 0049 } 0050 0051 // abort if file not found 0052 if ($image_path === false) { 0053 $app->abort(404); 0054 } 0055 0056 // define template callback 0057 $callback = $config->get("imagecache::templates.{$template}"); 0058 0059 if (is_callable($callback) || class_exists($callback)) { 0060 0061 // image manipulation based on callback 0062 $content = $app['image']->cache(function ($image) use ($image_path, $callback) { 0063 0064 switch (true) { 0065 case is_callable($callback): 0066 return $callback($image->make($image_path)); 0067 break; 0068 0069 case class_exists($callback): 0070 return $image->make($image_path)->filter(new $callback); 0071 break; 0072 } 0073 0074 }, $config->get('imagecache::lifetime')); 0075 0076 } else { 0077 0078 // get original image file contents 0079 $content = file_get_contents($image_path); 0080 } 0081 0082 // define mime type 0083 $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content); 0084 0085 // return http response 0086 return new IlluminateResponse($content, 200, [ 0087 'Content-Type' => $mime, 0088 'Cache-Control' => 'max-age='.($config->get('imagecache::lifetime')*60).', public', 0089 'Etag' => md5($content) 0090 ]); 0091 0092 }])->where(['template' => join('|', array_keys($config->get('imagecache::templates'))), 'filename' => '[ \w\\.\\/\\-]+']); 0093 } 0094 } 0095 } 0096 0097 /** 0098 * Register the service provider. 0099 * 0100 * @return void 0101 */ 0102 public function register() 0103 { 0104 $app = $this->app; 0105 0106 $app['image'] = $app->share(function ($app) { 0107 return new ImageManager($app['config']->get('image::config')); 0108 }); 0109 0110 $app->alias('image', 'Intervention\Image\ImageManager'); 0111 } 0112 }