src/Controller/Uniqskills/ImagineController.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Uniqskills;
  3. use Imagine\Exception\RuntimeException;
  4. use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
  5. use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
  6. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  7. use Liip\ImagineBundle\Imagine\Data\DataManager;
  8. use Liip\ImagineBundle\Imagine\Filter\FilterManager;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Mime\Exception\InvalidArgumentException;
  14. class ImagineController extends \Liip\ImagineBundle\Controller\ImagineController
  15. {
  16.     protected $environment;
  17.     private $dataManager;
  18.     private $filterManager;
  19.     private $cacheManager;
  20.     private $logger;
  21.     private $redirectResponseCode;
  22.     /**
  23.      * @param DataManager $dataManager
  24.      * @param FilterManager $filterManager
  25.      * @param CacheManager $cacheManager
  26.      * @param LoggerInterface|null $logger
  27.      * @param int $redirectResponseCode
  28.      * @param string $environment
  29.      */
  30.     public function __construct(
  31.         DataManager $dataManager,
  32.         FilterManager $filterManager,
  33.         CacheManager $cacheManager,
  34.         LoggerInterface $logger null,
  35.         $redirectResponseCode 301,
  36.         $environment 'dev'
  37.     )
  38.     {
  39.         $this->dataManager $dataManager;
  40.         $this->filterManager $filterManager;
  41.         $this->cacheManager $cacheManager;
  42.         $this->logger $logger;
  43.         $this->redirectResponseCode $redirectResponseCode;
  44.         $this->environment $environment;
  45.     }
  46.     /**
  47.      * This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
  48.      *
  49.      * @param Request $request
  50.      * @param string $path
  51.      * @param string $filter
  52.      *
  53.      * @return RedirectResponse
  54.      */
  55.     public function filterAction(Request $request$path$filter)
  56.     {
  57.         // decoding special characters and whitespaces from path obtained from urls
  58.         $path urldecode($path);
  59.         $resolver $request->get('resolver');
  60.         try {
  61.             if (($extension $request->query->get('extension')) !== null) {
  62.                 $path str_replace('.webp''.' $extension$path);
  63.             }
  64.             $oldPath $path;
  65.             if (!$this->cacheManager->isStored($path$filter$resolver)) {
  66.                 try {
  67.                     $binary $this->dataManager->find($filter$path);
  68.                 } catch (NotLoadableException InvalidArgumentException $e) {
  69.                     if ($defaultImageUrl $this->dataManager->getDefaultImageUrl($filter)) {
  70.                         return new RedirectResponse($defaultImageUrl);
  71.                     }
  72.                     throw new NotFoundHttpException('Source image could not be found'$e);
  73.                 }
  74.                 $this->cacheManager->store(
  75.                     $this->filterManager->applyFilter($binary$filter),
  76.                     $path,
  77.                     $filter,
  78.                     $resolver
  79.                 );
  80.             }
  81.             if ($_ENV['APP_ENV'] == 'prod') {
  82.                 $url str_replace('http://''https://'$this->cacheManager->resolve($oldPath$filter$resolver));
  83.             } else {
  84.                 $url $this->cacheManager->resolve($oldPath$filter$resolver);
  85.             }
  86.             return new RedirectResponse($url$this->redirectResponseCode);
  87.         } catch (NonExistingFilterException $e) {
  88.             $message sprintf('Could not locate filter "%s" for path "%s". Message was "%s"'$filter$path$e->getMessage());
  89.             if (null !== $this->logger) {
  90.                 $this->logger->debug($message);
  91.             }
  92.             throw new NotFoundHttpException($message$e);
  93.         } catch (RuntimeException $e) {
  94.             throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"'$path$filter$e->getMessage()), 0$e);
  95.         }
  96.     }
  97. }