Cheetah
ImageServiceProviderLaravel4.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Intervention\Image;
4 
5 use Illuminate\Support\ServiceProvider;
6 use Illuminate\Http\Response as IlluminateResponse;
7 
8 class ImageServiceProviderLaravel4 extends ServiceProvider
9 {
15  public function boot()
16  {
17  $this->package('intervention/image');
18 
19  // try to create imagecache route only if imagecache is present
20  if (class_exists('Intervention\\Image\\ImageCache')) {
21 
22  $app = $this->app;
23 
24  // load imagecache config
25  $app['config']->package('intervention/imagecache', __DIR__.'/../../../../imagecache/src/config', 'imagecache');
26  $config = $app['config'];
27 
28  // create dynamic manipulation route
29  if (is_string($config->get('imagecache::route'))) {
30 
31  // add original to route templates
32  $config->set('imagecache::templates.original', null);
33 
34  // setup image manipulator route
35  $app['router']->get($config->get('imagecache::route').'/{template}/{filename}', array('as' => 'imagecache', function ($template, $filename) use ($app, $config) {
36 
37  // disable session cookies for image route
38  $app['config']->set('session.driver', 'array');
39 
40  // find file
41  foreach ($config->get('imagecache::paths') as $path) {
42  // don't allow '..' in filenames
43  $image_path = $path.'/'.str_replace('..', '', $filename);
44  if (file_exists($image_path) && is_file($image_path)) {
45  break;
46  } else {
47  $image_path = false;
48  }
49  }
50 
51  // abort if file not found
52  if ($image_path === false) {
53  $app->abort(404);
54  }
55 
56  // define template callback
57  $callback = $config->get("imagecache::templates.{$template}");
58 
59  if (is_callable($callback)) {
60 
61  // image manipulation based on callback
62  $content = $app['image']->cache(function ($image) use ($image_path, $callback) {
63  return $callback($image->make($image_path));
64  }, $config->get('imagecache::lifetime'));
65 
66  } else {
67 
68  // get original image file contents
69  $content = file_get_contents($image_path);
70  }
71 
72  // define mime type
73  $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
74 
75  // return http response
76  return new IlluminateResponse($content, 200, array(
77  'Content-Type' => $mime,
78  'Cache-Control' => 'max-age='.($config->get('imagecache::lifetime')*60).', public',
79  'Etag' => md5($content)
80  ));
81 
82  }))->where(array('template' => join('|', array_keys($config->get('imagecache::templates'))), 'filename' => '[ \w\\.\\/\\-]+'));
83  }
84  }
85  }
86 
92  public function register()
93  {
94  $app = $this->app;
95 
96  $app['image'] = $app->share(function ($app) {
97  return new ImageManager($app['config']->get('image::config'));
98  });
99 
100  $app->alias('image', 'Intervention\Image\ImageManager');
101  }
102 }
$config
$config
Definition: Filter.ExtractStyleBlocks.txt:33
Intervention\Image\ImageServiceProviderLaravel4
Definition: ImageServiceProviderLaravel4.php:9
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
php
Intervention\Image\ImageManager
Definition: ImageManager.php:8
$path
$path
Definition: header.inc.php:12
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
Intervention\Image\ImageServiceProviderLaravel4\boot
boot()
Definition: ImageServiceProviderLaravel4.php:15
Intervention\Image
Definition: AbstractColor.php:3