/**
  * This action applies a given filter to a given image, saves the image and
  * outputs it to the browser at the same time
  *
  * @param string $path
  * @param string $filter
  *
  * @return Response
  */
 public function filter($path, $filter)
 {
     $path = '/' . ltrim($path, '/');
     //TODO: find out why I need double urldecode to get a valid path
     $browserPath = urldecode(urldecode($this->cachePathResolver->getBrowserPath($path, $filter)));
     $basePath = $this->request->getBaseUrl();
     if (!empty($basePath) && 0 === strpos($browserPath, $basePath)) {
         $browserPath = substr($browserPath, strlen($basePath));
     }
     // if cache path cannot be determined, return 404
     if (null === $browserPath) {
         throw new NotFoundHttpException('Image doesn\'t exist');
     }
     $realPath = $this->webRoot . $browserPath;
     $sourcePath = $this->sourceRoot . $path;
     if (!file_exists($sourcePath)) {
         throw new NotFoundHttpException(sprintf('Source image not found in "%s"', $sourcePath));
     }
     $ext = pathinfo($sourcePath, PATHINFO_EXTENSION);
     if ($ext == 'gif' && $this->isAnimatedGif(fopen($sourcePath, 'r'))) {
         ob_start();
         echo stream_get_contents(fopen($sourcePath, 'r'));
         return new Response(ob_get_clean(), 201, array('content-type' => 'image/gif'));
     }
     // if the file has already been cached, we're probably not rewriting
     // correctly, hence make a 301 to proper location, so browser remembers
     if (file_exists($realPath)) {
         return new Response('', 301, array('location' => $this->request->getBasePath() . $browserPath));
     }
     $dir = pathinfo($realPath, PATHINFO_DIRNAME);
     if (!is_dir($dir)) {
         if (!$this->filesystem->mkdir($dir)) {
             throw new \RuntimeException(sprintf('Could not create directory %s', $dir));
         }
     }
     ob_start();
     try {
         // TODO: get rid of hard-coded quality and format
         $this->filterManager->get($filter)->apply($this->imagine->open($sourcePath))->save($realPath, array('quality' => $this->filterManager->getOption($filter, "quality", 100)))->show($this->filterManager->getOption($filter, "format", "png"));
         // TODO: add more media headers
         return new Response(ob_get_clean(), 201, array('content-type' => 'image/' . $this->filterManager->getOption($filter, "format", "png")));
     } catch (\Exception $e) {
         ob_end_clean();
         throw $e;
     }
 }
 /**
  * This action invalidates all filters of a given image
  *
  * @param string $path
  * @param string $filter
  *
  * @return Response
  */
 public function invalidate($imagePath, $filterFilter = array())
 {
     foreach ($this->filterManager->getFilters() as $filterName => $filter) {
         if (count($filterFilter) > 0 && !in_array($filterName, $filterFilter)) {
             continue;
         }
         $path = '/' . ltrim($imagePath, '/');
         //TODO: find out why I need double urldecode to get a valid path
         $browserPath = urldecode(urldecode($this->cachePathResolver->getBrowserPath($path, $filterName)));
         $basePath = $this->request->getBaseUrl();
         //remove e.g. app_dev.php
         if (!empty($basePath) && 0 === strpos($browserPath, $basePath)) {
             $browserPath = substr($browserPath, strlen($basePath));
         }
         $path = realpath($this->webRoot . $browserPath);
         if (is_file($path)) {
             unlink($path);
         }
     }
 }