vendor/contao/core-bundle/src/Image/LegacyResizer.php line 101

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Image;
  11. use Contao\Config;
  12. use Contao\CoreBundle\Framework\FrameworkAwareInterface;
  13. use Contao\CoreBundle\Framework\FrameworkAwareTrait;
  14. use Contao\File;
  15. use Contao\Image as LegacyImage;
  16. use Contao\Image\DeferredImageInterface;
  17. use Contao\Image\DeferredResizer as ImageResizer;
  18. use Contao\Image\ImageInterface;
  19. use Contao\Image\ResizeConfiguration;
  20. use Contao\Image\ResizeCoordinates;
  21. use Contao\Image\ResizeOptions;
  22. use Contao\System;
  23. use Imagine\Exception\RuntimeException as ImagineRuntimeException;
  24. use Imagine\Gd\Imagine as GdImagine;
  25. use Imagine\Image\Box;
  26. use Imagine\Image\ImagineInterface;
  27. use Webmozart\PathUtil\Path;
  28. /**
  29.  * Resizes image objects and executes the legacy hooks.
  30.  */
  31. class LegacyResizer extends ImageResizer implements FrameworkAwareInterface
  32. {
  33.     use FrameworkAwareTrait;
  34.     /**
  35.      * @var LegacyImage|null
  36.      */
  37.     private $legacyImage;
  38.     public function resize(ImageInterface $imageResizeConfiguration $configResizeOptions $options): ImageInterface
  39.     {
  40.         $this->framework->initialize(true);
  41.         $projectDir System::getContainer()->getParameter('kernel.project_dir');
  42.         if ($this->hasExecuteResizeHook() || $this->hasGetImageHook()) {
  43.             trigger_deprecation('contao/core-bundle''4.0''Using the "executeResize" and "getImage" hooks has been deprecated and will no longer work in Contao 5.0. Replace the "contao.image.resizer" service instead.');
  44.             $this->legacyImage null;
  45.             $legacyPath $image->getPath();
  46.             if (Path::isBasePath($projectDir$legacyPath)) {
  47.                 $legacyPath Path::makeRelative($legacyPath$projectDir);
  48.                 $this->legacyImage = new LegacyImage(new File($legacyPath));
  49.                 $this->legacyImage->setTargetWidth($config->getWidth());
  50.                 $this->legacyImage->setTargetHeight($config->getHeight());
  51.                 $this->legacyImage->setResizeMode($config->getMode());
  52.                 $this->legacyImage->setZoomLevel($config->getZoomLevel());
  53.                 if (($targetPath $options->getTargetPath()) && Path::isBasePath($projectDir$targetPath)) {
  54.                     $this->legacyImage->setTargetPath(Path::makeRelative($targetPath$projectDir));
  55.                 }
  56.                 $importantPart $image->getImportantPart();
  57.                 $imageSize $image->getDimensions()->getSize();
  58.                 $this->legacyImage->setImportantPart([
  59.                     'x' => $importantPart->getX() * $imageSize->getWidth(),
  60.                     'y' => $importantPart->getY() * $imageSize->getHeight(),
  61.                     'width' => $importantPart->getWidth() * $imageSize->getWidth(),
  62.                     'height' => $importantPart->getHeight() * $imageSize->getHeight(),
  63.                 ]);
  64.             }
  65.         }
  66.         if ($this->legacyImage && $this->hasExecuteResizeHook()) {
  67.             foreach ($GLOBALS['TL_HOOKS']['executeResize'] as $callback) {
  68.                 $return System::importStatic($callback[0])->{$callback[1]}($this->legacyImage);
  69.                 if (\is_string($return)) {
  70.                     return $this->createImage($imagePath::join($projectDir$return));
  71.                 }
  72.             }
  73.         }
  74.         try {
  75.             return parent::resize($image$config$options);
  76.         } catch (ImagineRuntimeException $exception) {
  77.             throw $this->enhanceImagineException($exception$image);
  78.         }
  79.     }
  80.     public function resizeDeferredImage(DeferredImageInterface $imagebool $blocking true): ?ImageInterface
  81.     {
  82.         try {
  83.             return parent::resizeDeferredImage($image$blocking);
  84.         } catch (ImagineRuntimeException $exception) {
  85.             throw $this->enhanceImagineException($exception$image);
  86.         }
  87.     }
  88.     protected function executeResize(ImageInterface $imageResizeCoordinates $coordinatesstring $pathResizeOptions $options): ImageInterface
  89.     {
  90.         if ($this->legacyImage && $this->hasGetImageHook()) {
  91.             $projectDir System::getContainer()->getParameter('kernel.project_dir');
  92.             foreach ($GLOBALS['TL_HOOKS']['getImage'] as $callback) {
  93.                 $return System::importStatic($callback[0])->{$callback[1]}(
  94.                     $this->legacyImage->getOriginalPath(),
  95.                     $this->legacyImage->getTargetWidth(),
  96.                     $this->legacyImage->getTargetHeight(),
  97.                     $this->legacyImage->getResizeMode(),
  98.                     $this->legacyImage->getCacheName(),
  99.                     new File($this->legacyImage->getOriginalPath()),
  100.                     $this->legacyImage->getTargetPath(),
  101.                     $this->legacyImage
  102.                 );
  103.                 if (\is_string($return)) {
  104.                     return $this->createImage($imagePath::join($projectDir$return));
  105.                 }
  106.             }
  107.         }
  108.         if ($image->getImagine() instanceof GdImagine) {
  109.             $dimensions $image->getDimensions();
  110.             /** @var Config $config */
  111.             $config $this->framework->getAdapter(Config::class);
  112.             $gdMaxImgWidth $config->get('gdMaxImgWidth');
  113.             $gdMaxImgHeight $config->get('gdMaxImgHeight');
  114.             // Return the path to the original image if it cannot be handled
  115.             if (
  116.                 $dimensions->getSize()->getWidth() > $gdMaxImgWidth
  117.                 || $dimensions->getSize()->getHeight() > $gdMaxImgHeight
  118.                 || $coordinates->getSize()->getWidth() > $gdMaxImgWidth
  119.                 || $coordinates->getSize()->getHeight() > $gdMaxImgHeight
  120.             ) {
  121.                 return $this->createImage($image$image->getPath());
  122.             }
  123.         }
  124.         return parent::executeResize($image$coordinates$path$options);
  125.     }
  126.     private function hasExecuteResizeHook(): bool
  127.     {
  128.         return !empty($GLOBALS['TL_HOOKS']['executeResize']) && \is_array($GLOBALS['TL_HOOKS']['executeResize']);
  129.     }
  130.     private function hasGetImageHook(): bool
  131.     {
  132.         return !empty($GLOBALS['TL_HOOKS']['getImage']) && \is_array($GLOBALS['TL_HOOKS']['getImage']);
  133.     }
  134.     private function enhanceImagineException(ImagineRuntimeException $exceptionImageInterface $image): \Throwable
  135.     {
  136.         $format Path::getExtension($image->getPath(), true);
  137.         if (!$this->formatIsSupported($format$image->getImagine())) {
  138.             return new \RuntimeException(sprintf('Image format "%s" is not supported in %s on this environment. Consider removing this format from contao.image.valid_extensions or switch the contao.image.imagine_service to an implementation that supports it.'$format, \get_class($image->getImagine())), $exception->getCode(), $exception);
  139.         }
  140.         return $exception;
  141.     }
  142.     private function formatIsSupported(string $formatImagineInterface $imagine): bool
  143.     {
  144.         if ('' === $format) {
  145.             return false;
  146.         }
  147.         try {
  148.             $imagine->create(new Box(11))->get($format);
  149.         } catch (\Throwable $e) {
  150.             return false;
  151.         }
  152.         return true;
  153.     }
  154. }