vendor/liip/imagine-bundle/Controller/ImagineController.php line 83

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the `liip/LiipImagineBundle` project.
  4.  *
  5.  * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE.md
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Liip\ImagineBundle\Controller;
  11. use Imagine\Exception\RuntimeException;
  12. use Liip\ImagineBundle\Config\Controller\ControllerConfig;
  13. use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
  14. use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
  15. use Liip\ImagineBundle\Imagine\Cache\Helper\PathHelper;
  16. use Liip\ImagineBundle\Imagine\Cache\SignerInterface;
  17. use Liip\ImagineBundle\Imagine\Data\DataManager;
  18. use Liip\ImagineBundle\Service\FilterService;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  22. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  23. class ImagineController
  24. {
  25.     /**
  26.      * @var FilterService
  27.      */
  28.     private $filterService;
  29.     /**
  30.      * @var DataManager
  31.      */
  32.     private $dataManager;
  33.     /**
  34.      * @var SignerInterface
  35.      */
  36.     private $signer;
  37.     /**
  38.      * @var ControllerConfig
  39.      */
  40.     private $controllerConfig;
  41.     public function __construct(
  42.         FilterService $filterService,
  43.         DataManager $dataManager,
  44.         SignerInterface $signer,
  45.         ?ControllerConfig $controllerConfig null
  46.     ) {
  47.         $this->filterService $filterService;
  48.         $this->dataManager $dataManager;
  49.         $this->signer $signer;
  50.         if (null === $controllerConfig) {
  51.             @trigger_error(sprintf(
  52.                 'Instantiating "%s" without a forth argument of type "%s" is deprecated since 2.2.0 and will be required in 3.0.'self::class, ControllerConfig::class
  53.             ), E_USER_DEPRECATED);
  54.         }
  55.         $this->controllerConfig $controllerConfig ?? new ControllerConfig(301);
  56.     }
  57.     /**
  58.      * This action applies a given filter to a given image, saves the image and redirects the browser to the stored
  59.      * image.
  60.      *
  61.      * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
  62.      * filter and storing the image again.
  63.      *
  64.      * @param string $path
  65.      * @param string $filter
  66.      *
  67.      * @throws RuntimeException
  68.      * @throws NotFoundHttpException
  69.      *
  70.      * @return RedirectResponse
  71.      */
  72.     public function filterAction(Request $request$path$filter)
  73.     {
  74.         $path PathHelper::urlPathToFilePath($path);
  75.         $resolver $request->get('resolver');
  76.         return $this->createRedirectResponse(function () use ($path$filter$resolver$request) {
  77.             return $this->filterService->getUrlOfFilteredImage(
  78.                 $path,
  79.                 $filter,
  80.                 $resolver,
  81.                 $this->isWebpSupported($request)
  82.             );
  83.         }, $path$filter);
  84.     }
  85.     /**
  86.      * This action applies a given filter -merged with additional runtime filters- to a given image, saves the image and
  87.      * redirects the browser to the stored image.
  88.      *
  89.      * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
  90.      * filter and storing the image again.
  91.      *
  92.      * @param string $hash
  93.      * @param string $path
  94.      * @param string $filter
  95.      *
  96.      * @throws RuntimeException
  97.      * @throws BadRequestHttpException
  98.      * @throws NotFoundHttpException
  99.      *
  100.      * @return RedirectResponse
  101.      */
  102.     public function filterRuntimeAction(Request $request$hash$path$filter)
  103.     {
  104.         $resolver $request->get('resolver');
  105.         $path PathHelper::urlPathToFilePath($path);
  106.         $runtimeConfig $request->query->get('filters', []);
  107.         if (!\is_array($runtimeConfig)) {
  108.             throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"'$runtimeConfig));
  109.         }
  110.         if (true !== $this->signer->check($hash$path$runtimeConfig)) {
  111.             throw new BadRequestHttpException(sprintf('Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s'$path$filterjson_encode($runtimeConfig)));
  112.         }
  113.         return $this->createRedirectResponse(function () use ($path$filter$runtimeConfig$resolver$request) {
  114.             return $this->filterService->getUrlOfFilteredImageWithRuntimeFilters(
  115.                 $path,
  116.                 $filter,
  117.                 $runtimeConfig,
  118.                 $resolver,
  119.                 $this->isWebpSupported($request)
  120.             );
  121.         }, $path$filter$hash);
  122.     }
  123.     private function createRedirectResponse(\Closure $urlstring $pathstring $filter, ?string $hash null): RedirectResponse
  124.     {
  125.         try {
  126.             return new RedirectResponse($url(), $this->controllerConfig->getRedirectResponseCode());
  127.         } catch (NotLoadableException $exception) {
  128.             if (null !== $this->dataManager->getDefaultImageUrl($filter)) {
  129.                 return new RedirectResponse($this->dataManager->getDefaultImageUrl($filter));
  130.             }
  131.             throw new NotFoundHttpException(sprintf('Source image for path "%s" could not be found'$path), $exception);
  132.         } catch (NonExistingFilterException $exception) {
  133.             throw new NotFoundHttpException(sprintf('Requested non-existing filter "%s"'$filter), $exception);
  134.         } catch (RuntimeException $exception) {
  135.             throw new \RuntimeException(vsprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', [$hash sprintf('%s/%s'$hash$path) : $path$filter$exception->getMessage()]), 0$exception);
  136.         }
  137.     }
  138.     private function isWebpSupported(Request $request): bool
  139.     {
  140.         return false !== mb_stripos($request->headers->get('accept'''), 'image/webp');
  141.     }
  142. }