Ejemplo n.º 1
0
 /**
  * @param Request $request
  * @param string  $filename
  *
  * @throws NotFoundHttpException If media is not found
  *
  * @return Response
  */
 public function showAction(Request $request, $filename)
 {
     if (!$this->filesystem->has($filename)) {
         throw new NotFoundHttpException(sprintf('Media "%s" not found', $filename));
     }
     $response = new Response($content = $this->filesystem->read($filename));
     $mime = $this->filesystem->mimeType($filename);
     if (($filter = $request->query->get('filter')) && null !== $mime && 0 === strpos($mime, 'image')) {
         try {
             $cachePath = $this->cacheManager->resolve($request, $filename, $filter);
             if ($cachePath instanceof Response) {
                 $response = $cachePath;
             } else {
                 $image = $this->imagine->load($content);
                 $response = $this->filterManager->get($request, $filter, $image, $filename);
                 $response = $this->cacheManager->store($response, $cachePath, $filter);
             }
         } catch (\RuntimeException $e) {
             if (0 === strpos($e->getMessage(), 'Filter not defined')) {
                 throw new HttpException(404, sprintf('The filter "%s" cannot be found', $filter), $e);
             }
             throw $e;
         }
     }
     if ($mime) {
         $response->headers->set('Content-Type', $mime);
     }
     return $response;
 }
 /**
  * This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
  *
  * @param Request $request
  * @param string $path
  * @param string $filter
  *
  * @return Response
  */
 public function filterAction(Request $request, $path, $filter)
 {
     $targetPath = $this->cacheManager->resolve($request, $path, $filter);
     if ($targetPath instanceof Response) {
         return $targetPath;
     }
     $image = $this->dataManager->find($filter, $path);
     $response = $this->filterManager->get($request, $filter, $image, $path);
     if ($targetPath) {
         $response = $this->cacheManager->store($response, $targetPath, $filter);
     }
     return $response;
 }
 public function testGetRequestKnowsContentType()
 {
     $thumbConfig = array('size' => array(180, 180), 'mode' => 'outbound');
     $config = $this->getMockFilterConfiguration();
     $config->expects($this->atLeastOnce())->method('get')->with('thumbnail')->will($this->returnValue(array('filters' => array('thumbnail' => $thumbConfig), 'format' => 'jpg')));
     $image = $this->getMockImage();
     $image->expects($this->once())->method('get')->with('jpg', array('quality' => 100))->will($this->returnSelf());
     $loader = $this->getMockLoader();
     $loader->expects($this->once())->method('load')->with($image, $thumbConfig)->will($this->returnValue($image));
     $filterManager = new FilterManager($config);
     $filterManager->addLoader('thumbnail', $loader);
     $request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request');
     $request->expects($this->once())->method('getMimeType')->with('jpg')->will($this->returnValue('image/jpeg'));
     $response = $filterManager->get($request, 'thumbnail', $image, $this->fixturesDir . '/assets/cats.jpeg');
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('image/jpeg', $response->headers->get('Content-Type'));
 }