wpfunnels
/
vendor
/
intervention
/
image
/
src
/
Intervention
/
Image
/
ImageServiceProviderLaravel4.php
ImageServiceProviderLaravel4.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.1, at vendor/intervention/image/src/Intervention/Image/ImageServiceProviderLaravel4.php
| 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 | { |
| 10 | /** |
| 11 | * Bootstrap the application events. |
| 12 | * |
| 13 | * @return void |
| 14 | */ |
| 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}', ['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) || class_exists($callback)) { |
| 60 | |
| 61 | // image manipulation based on callback |
| 62 | $content = $app['image']->cache(function ($image) use ($image_path, $callback) { |
| 63 | |
| 64 | switch (true) { |
| 65 | case is_callable($callback): |
| 66 | return $callback($image->make($image_path)); |
| 67 | break; |
| 68 | |
| 69 | case class_exists($callback): |
| 70 | return $image->make($image_path)->filter(new $callback); |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | }, $config->get('imagecache::lifetime')); |
| 75 | |
| 76 | } else { |
| 77 | |
| 78 | // get original image file contents |
| 79 | $content = file_get_contents($image_path); |
| 80 | } |
| 81 | |
| 82 | // define mime type |
| 83 | $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content); |
| 84 | |
| 85 | // return http response |
| 86 | return new IlluminateResponse($content, 200, [ |
| 87 | 'Content-Type' => $mime, |
| 88 | 'Cache-Control' => 'max-age='.($config->get('imagecache::lifetime')*60).', public', |
| 89 | 'Etag' => md5($content) |
| 90 | ]); |
| 91 | |
| 92 | }])->where(['template' => join('|', array_keys($config->get('imagecache::templates'))), 'filename' => '[ \w\\.\\/\\-]+']); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Register the service provider. |
| 99 | * |
| 100 | * @return void |
| 101 | */ |
| 102 | public function register() |
| 103 | { |
| 104 | $app = $this->app; |
| 105 | |
| 106 | $app['image'] = $app->share(function ($app) { |
| 107 | return new ImageManager($app['config']->get('image::config')); |
| 108 | }); |
| 109 | |
| 110 | $app->alias('image', 'Intervention\Image\ImageManager'); |
| 111 | } |
| 112 | } |
| 113 |