/**
  * {@inheritDoc}
  */
 function load(array $options = array())
 {
     if (!isset($options['filters']) || !is_array($options['filters'])) {
         throw new InvalidArgumentException('Expected filters key and type of array');
     }
     $filters = array();
     foreach ($options['filters'] as $loaderName => $opts) {
         $filters[] = $this->filterManager->getLoader($loaderName)->load(is_array($opts) ? $opts : []);
     }
     return new ChainFilter($filters);
 }
 /**
  * {@inheritDoc}
  */
 function load(array $options = array())
 {
     if (false == isset($options['filters']) || false == is_array($options['filters'])) {
         throw new \InvalidArgumentException('Expected filters key and type of array');
     }
     if (false == $options['filters']) {
         throw new \InvalidArgumentException('At least one filter expected');
     }
     $filters = array();
     foreach ($options['filters'] as $loaderName => $loaderOptions) {
         $loader = $this->filterManager->getLoader($loaderName);
         $loaderOptions = is_array($loaderOptions) ? $loaderOptions : array();
         $filters[] = $loader->load($loaderOptions);
     }
     return new ChainFilter($filters);
 }
 private function findStreamContext($path, $filter)
 {
     if (false === ($at = strpos($path, '://'))) {
         return null;
     }
     $contextName = substr($path, 0, $at);
     $context = $this->filterManager->getOption($filter, $contextName . '_context', null);
     return is_array($context) ? stream_context_create([$contextName => $context]) : null;
 }
 /**
  * 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
  *
  * @throws Exception
  */
 public function filterAction($path, $filter)
 {
     $baseUrl = $this->request->getBaseUrl();
     try {
         try {
             $cachedPath = $this->cacheManager->cacheImage($baseUrl, $path, $filter);
         } catch (RuntimeException $e) {
             if (!isset($this->notFoundImages[$filter])) {
                 throw $e;
             }
             $path = $this->notFoundImages[$filter];
             $cachedPath = $this->cacheManager->cacheImage($baseUrl, $path, $filter);
         }
     } catch (RouteNotFoundException $e) {
         throw new NotFoundHttpException('Filter doesn\'t exist.');
     }
     // if cache path cannot be determined, return 404
     if (null === $cachedPath) {
         throw new NotFoundHttpException('Image doesn\'t exist');
     }
     try {
         // Using File instead of Imagine::open(), because i.e. image/x-icon is not widely supported.
         $file = new ImageFile($cachedPath, false);
         // TODO: add more media headers
         $headers = ['content-type' => $file->getMimeType(), 'content-length' => $file->getSize()];
         $response = new Response($file->getContents(), 201, $headers);
         // Cache
         if (!($cacheType = $this->filterManager->getOption($filter, 'cache_type', false))) {
             return $response;
         }
         $cacheType === 'public' ? $response->setPublic() : $response->setPrivate();
         $cacheExpires = $this->filterManager->getOption($filter, 'cache_expires', '1 day');
         $expirationDate = new DateTime('+' . $cacheExpires);
         $maxAge = $expirationDate->format('U') - time();
         if ($maxAge < 0) {
             throw new InvalidArgumentException('Invalid cache expiration date');
         }
         $response->setExpires($expirationDate);
         $response->setMaxAge($maxAge);
         return $response;
     } catch (Exception $e) {
         throw $e;
     }
 }
 private function skip($path, $filter)
 {
     if (!($skip = $this->manager->getOption($filter, 'skip'))) {
         return false;
     }
     foreach ((array) $skip as $regex) {
         if (preg_match($regex, $path)) {
             return true;
         }
     }
     return false;
 }
 /**
  * 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)
 {
     $cachedPath = $this->cacheManager->cacheImage($this->request->getBaseUrl(), $path, $filter);
     // if cache path cannot be determined, return 404
     if (null === $cachedPath) {
         throw new NotFoundHttpException('Image doesn\'t exist');
     }
     ob_start();
     try {
         $format = $this->filterManager->getOption($filter, "format", "png");
         $this->imagine->open($cachedPath)->show($format);
         $type = 'image/' . $format;
         $length = ob_get_length();
         $content = ob_get_clean();
         // TODO: add more media headers
         $response = new Response($content, 201, array('content-type' => $type, 'content-length' => $length));
         // Cache
         $cacheType = $this->filterManager->getOption($filter, "cache_type", false);
         if (false == $cacheType) {
             return $response;
         }
         $cacheType === "public" ? $response->setPublic() : $response->setPrivate();
         $cacheExpires = $this->filterManager->getOption($filter, "cache_expires", "1 day");
         $expirationDate = new \DateTime("+" . $cacheExpires);
         $maxAge = $expirationDate->format("U") - time();
         if ($maxAge < 0) {
             throw new \InvalidArgumentException("Invalid cache expiration date");
         }
         $response->setExpires($expirationDate);
         $response->setMaxAge($maxAge);
         return $response;
     } catch (\Exception $e) {
         ob_end_clean();
         throw $e;
     }
 }
 /**
  * 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 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));
     }
     if (!file_exists($sourcePath)) {
         throw new NotFoundHttpException(sprintf('Source image not found in "%s"', $sourcePath));
     }
     $dir = pathinfo($realPath, PATHINFO_DIRNAME);
     if (!is_dir($dir)) {
         if (false === $this->filesystem->mkdir($dir)) {
             throw new \RuntimeException(sprintf('Could not create directory %s', $dir));
         }
     }
     ob_start();
     try {
         $format = $this->filterManager->getOption($filter, "format", "png");
         // 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), 'format' => $this->filterManager->getOption($filter, "format", null)))->show($format);
         $type = 'image/' . $format;
         $length = ob_get_length();
         $content = ob_get_clean();
         // TODO: add more media headers
         return new Response($content, 201, array('content-type' => $type, 'content-length' => $length));
     } catch (\Exception $e) {
         ob_end_clean();
         throw $e;
     }
 }