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')); }; }