public static function run($config, $request) { $imagecache = new Manager($config); try { $final_file = $imagecache->handleRequest($request['preset'], $request['file']); } catch (InvalidPresetException $e) { header('HTTP/1.0 404 Not Found'); echo $e->getMessage(); return; } catch (NotFoundException $e) { header('HTTP/1.0 404 Not Found'); echo $e->getMessage(); return; } catch (\RuntimeException $e) { header('HTTP/1.0 500 Internal Server Error'); echo $e->getMessage(); return; } $transfer = new Transfer($final_file); // if the status is 304, we don't // need to send the content with it if ($transfer->getStatus() == 304) { header('HTTP/1.1 304 Not Modified'); return; } header('HTTP/1.1 200 OK'); foreach ($transfer->getFormattedHeaders() as $header) { header($header); } $transfer->stream(); }
/** * Register the service provider. * * @return void */ public function register() { // Add the namespace to config $this->app['config']->package('onigoetz/imagecache', __DIR__ . '/../../config'); $config = $this->app['config']->get('imagecache::imagecache'); //TODO :: externalize that $toolkit = 'gd'; // Stopwatch - must be registered so the application doesn't fail if the profiler is disabled $this->app['imagecache'] = $this->app->share(function () use($config, $toolkit) { return new Manager($config, $toolkit); }); //PHP 5.3 compatibility $app = $this->app; $url = "{$config['path_images']}/{$config['path_cache']}/{preset}/{file}"; $this->app['router']->get($url, function ($preset, $file) use($app) { try { $final_file = $app['imagecache']->handleRequest($preset, $file); } catch (InvalidPresetException $e) { return \Response::make('Invalid preset', 404); } catch (NotFoundException $e) { return \Response::make('File not found', 404); } if (!$final_file) { return \Response::make('Dunno what happened', 500); } //TODO :: be more "symfony reponse" friendly $transfer = new Transfer(); $transfer->transfer($final_file); exit; })->where('file', '.*'); }
public static function register($app, $config) { //TODO :: externalize that $toolkit = 'gd'; $app->container->singleton('imagecache', function () use($config, $toolkit) { return new Manager($config, $toolkit); }); $url = "/{$config['images_url']}/:preset/:file"; $app->get($url, function ($preset, $file) use($app) { try { $final_file = $app->imagecache->handleRequest($preset, $file); } catch (InvalidPresetException $e) { header('HTTP/1.0 404 Not Found'); echo $e->getMessage(); exit; } catch (NotFoundException $e) { header('HTTP/1.0 404 Not Found'); echo $e->getMessage(); exit; } if (!$final_file) { header('HTTP/1.0 500 Internal Server Error'); echo 'dunno ...'; exit; } $transfer = new Transfer(); $transfer->transfer($final_file); exit; })->conditions(array('file' => '.*')); }
public function request() { return function (ServerRequestInterface $req, ResponseInterface $res, $args) { $preset = $args['preset']; $file = $args['file']; try { $final_file = $this->imagecache->handleRequest($preset, $file); } catch (InvalidPresetException $e) { $res->getBody()->write($e->getMessage()); return $res->withStatus(404); } catch (NotFoundException $e) { $res->getBody()->write($e->getMessage()); return $res->withStatus(404); } catch (RuntimeException $e) { $res->getBody()->write($e->getMessage()); return $res->withStatus(500); } $transfer = new Transfer($final_file); foreach ($transfer->getHeaders() as $key => $value) { $res = $res->withHeader($key, $value); } return $res->withStatus($transfer->getStatus())->withBody(new LazyOpenStream($final_file, 'r')); }; }