Example #1
0
 public function send(HttpRequest $request, HttpResponse $response)
 {
     $width = null;
     $height = null;
     $quality = null;
     try {
         $url = $request->getUrl();
         $name = Strings::after($url->getPath(), '/', -1);
         try {
             // only for case, when all images expected response from application and not from existing images
             $thumb = $this->repository->fetchThumbnail($name);
         } catch (\Imager\NotExistsException $e) {
             // thumbnail must be generate, because not exists
             $id = $url->getQueryParameter('id');
             $width = $url->getQueryParameter('width');
             $height = $url->getQueryParameter('height');
             $quality = $url->getQueryParameter('quality');
             $source = $this->repository->fetch($id);
             $thumb = $this->factory->create($source)->resize($width, $height, $quality);
             $thumb = $this->repository->save($thumb);
         }
         $response->setContentType($thumb->getMime());
         $response->setHeader('Content-Length', $thumb->getSize());
         echo $thumb->getContent();
     } catch (\Exception $e) {
         $width = $width ?: 200;
         $height = $height ?: 200;
         $this->sendError($response, $e, $width, $height);
     }
     exit(0);
 }
Example #2
0
 public function match(Http\IRequest $request)
 {
     $url = $request->getUrl();
     if (Strings::contains($url->path, $this->basePath) === false) {
         return;
     }
     $imgUrl = Strings::after($url->path, $this->basePath);
     $imgUrl = Strings::after($imgUrl, '/', -1);
     $parts = Imager\Helpers::parseName($imgUrl);
     if (!isset($parts['id'])) {
         return;
     }
     $url->setQueryParameter('id', $parts['id'])->setQueryParameter('width', $parts['width'])->setQueryParameter('height', $parts['height'])->setQueryParameter('quality', $parts['quality']);
     $response = new ImageResponse($this->repository, $this->imageFactory);
     $response->send(new Http\Request($url), new Http\Response());
 }