vendor/pimcore/pimcore/models/Asset/Image/Thumbnail.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Asset\Image;
  15. use Pimcore\Event\AssetEvents;
  16. use Pimcore\Event\FrontendEvents;
  17. use Pimcore\Logger;
  18. use Pimcore\Model\Asset;
  19. use Pimcore\Model\Asset\Image;
  20. use Pimcore\Model\Asset\Image\Thumbnail\Config;
  21. use Pimcore\Model\Asset\Thumbnail\ImageThumbnailTrait;
  22. use Pimcore\Model\Exception\NotFoundException;
  23. use Pimcore\Tool;
  24. use Symfony\Component\EventDispatcher\GenericEvent;
  25. final class Thumbnail
  26. {
  27.     use ImageThumbnailTrait;
  28.     /**
  29.      * @internal
  30.      *
  31.      * @var bool[]
  32.      */
  33.     protected static $hasListenersCache = [];
  34.     /**
  35.      * @param Image $asset
  36.      * @param string|array|Thumbnail\Config|null $config
  37.      * @param bool $deferred
  38.      */
  39.     public function __construct($asset$config null$deferred true)
  40.     {
  41.         $this->asset $asset;
  42.         $this->deferred $deferred;
  43.         $this->config $this->createConfig($config);
  44.     }
  45.     /**
  46.      * TODO: Pimcore 11: Change method signature to getPath($args = [])
  47.      *
  48.      * @param mixed $args,...
  49.      *
  50.      * @return string
  51.      */
  52.     public function getPath(...$args)
  53.     {
  54.         // TODO: Pimcore 11: remove calling the covertArgsBcLayer() method
  55.         $args $this->convertArgsBcLayer($args);
  56.         // set defaults
  57.         $deferredAllowed $args['deferredAllowed'] ?? true;
  58.         $cacheBuster $args['cacheBuster'] ?? false;
  59.         $frontend $args['frontend'] ?? \Pimcore\Tool::isFrontend();
  60.         $pathReference null;
  61.         if ($this->getConfig()) {
  62.             if ($this->useOriginalFile($this->asset->getFilename()) && $this->getConfig()->isSvgTargetFormatPossible()) {
  63.                 // we still generate the raster image, to get the final size of the thumbnail
  64.                 // we use getRealFullPath() here, to avoid double encoding (getFullPath() returns already encoded path)
  65.                 $pathReference = [
  66.                     'src' => $this->asset->getRealFullPath(),
  67.                     'type' => 'asset',
  68.                 ];
  69.             }
  70.         }
  71.         if (!$pathReference) {
  72.             $pathReference $this->getPathReference($deferredAllowed);
  73.         }
  74.         $path $this->convertToWebPath($pathReference$frontend);
  75.         if ($cacheBuster) {
  76.             $path $this->addCacheBuster($path, ['cacheBuster' => true], $this->getAsset());
  77.         }
  78.         if ($this->hasListeners(FrontendEvents::ASSET_IMAGE_THUMBNAIL)) {
  79.             $event = new GenericEvent($this, [
  80.                 'pathReference' => $pathReference,
  81.                 'frontendPath' => $path,
  82.             ]);
  83.             \Pimcore::getEventDispatcher()->dispatch($eventFrontendEvents::ASSET_IMAGE_THUMBNAIL);
  84.             $path $event->getArgument('frontendPath');
  85.         }
  86.         return $path;
  87.     }
  88.     /**
  89.      * @param string $eventName
  90.      *
  91.      * @return bool
  92.      */
  93.     protected function hasListeners(string $eventName): bool
  94.     {
  95.         if (!isset(self::$hasListenersCache[$eventName])) {
  96.             self::$hasListenersCache[$eventName] = \Pimcore::getEventDispatcher()->hasListeners($eventName);
  97.         }
  98.         return self::$hasListenersCache[$eventName];
  99.     }
  100.     /**
  101.      * @param string $filename
  102.      *
  103.      * @return bool
  104.      */
  105.     protected function useOriginalFile($filename)
  106.     {
  107.         if ($this->getConfig()) {
  108.             if (!$this->getConfig()->isRasterizeSVG() && preg_match("@\.svgz?$@"$filename)) {
  109.                 return true;
  110.             }
  111.         }
  112.         return false;
  113.     }
  114.     /**
  115.      * @internal
  116.      *
  117.      * @param bool $deferredAllowed
  118.      */
  119.     public function generate($deferredAllowed true)
  120.     {
  121.         $deferred false;
  122.         $generated false;
  123.         if ($this->asset && empty($this->pathReference)) {
  124.             // if no correct thumbnail config is given use the original image as thumbnail
  125.             if (!$this->config) {
  126.                 $this->pathReference = [
  127.                     'type' => 'asset',
  128.                     'src' => $this->asset->getRealFullPath(),
  129.                 ];
  130.             } else {
  131.                 try {
  132.                     $deferred $deferredAllowed && $this->deferred;
  133.                     $this->pathReference Thumbnail\Processor::process($this->asset$this->confignull$deferred$generated);
  134.                 } catch (\Exception $e) {
  135.                     Logger::error("Couldn't create thumbnail of image " $this->asset->getRealFullPath() . ': ' $e);
  136.                 }
  137.             }
  138.         }
  139.         if (empty($this->pathReference)) {
  140.             $this->pathReference = [
  141.                 'type' => 'error',
  142.                 'src' => '/bundles/pimcoreadmin/img/filetype-not-supported.svg',
  143.             ];
  144.         }
  145.         if ($this->hasListeners(AssetEvents::IMAGE_THUMBNAIL)) {
  146.             $event = new GenericEvent($this, [
  147.                 'deferred' => $deferred,
  148.                 'generated' => $generated,
  149.             ]);
  150.             \Pimcore::getEventDispatcher()->dispatch($eventAssetEvents::IMAGE_THUMBNAIL);
  151.         }
  152.     }
  153.     /**
  154.      * @return string Public path to thumbnail image.
  155.      */
  156.     public function __toString()
  157.     {
  158.         return $this->getPath();
  159.     }
  160.     /**
  161.      * @param string $path
  162.      * @param array $options
  163.      * @param Asset $asset
  164.      *
  165.      * @return string
  166.      */
  167.     private function addCacheBuster(string $path, array $optionsAsset $asset): string
  168.     {
  169.         if (isset($options['cacheBuster']) && $options['cacheBuster']) {
  170.             if (!str_starts_with($path'http')) {
  171.                 $path '/cache-buster-' $asset->getVersionCount() . $path;
  172.             }
  173.         }
  174.         return $path;
  175.     }
  176.     private function getSourceTagHtml(Config $thumbConfigstring $mediaQueryImage $image, array $options): string
  177.     {
  178.         $sourceTagAttributes = [];
  179.         $sourceTagAttributes['srcset'] = $this->getSrcset($thumbConfig$image$options$mediaQuery);
  180.         $thumb $image->getThumbnail($thumbConfigtrue);
  181.         if ($mediaQuery) {
  182.             $sourceTagAttributes['media'] = $mediaQuery;
  183.             $thumb->reset();
  184.         }
  185.         if (isset($options['previewDataUri'])) {
  186.             $sourceTagAttributes['data-srcset'] = $sourceTagAttributes['srcset'];
  187.             unset($sourceTagAttributes['srcset']);
  188.         }
  189.         if (!isset($options['disableWidthHeightAttributes'])) {
  190.             if ($thumb->getWidth()) {
  191.                 $sourceTagAttributes['width'] = $thumb->getWidth();
  192.             }
  193.             if ($thumb->getHeight()) {
  194.                 $sourceTagAttributes['height'] = $thumb->getHeight();
  195.             }
  196.         }
  197.         $sourceTagAttributes['type'] = $thumb->getMimeType();
  198.         $sourceCallback $options['sourceCallback'] ?? null;
  199.         if ($sourceCallback) {
  200.             $sourceTagAttributes $sourceCallback($sourceTagAttributes);
  201.         }
  202.         return '<source ' array_to_html_attribute_string($sourceTagAttributes) . ' />';
  203.     }
  204.     /**
  205.      * Get generated HTML for displaying the thumbnail image in a HTML document.
  206.      *
  207.      * @param array $options Custom configuration
  208.      *
  209.      * @return string
  210.      */
  211.     public function getHtml($options = [])
  212.     {
  213.         $emptyGif 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
  214.         /** @var Image $image */
  215.         $image $this->getAsset();
  216.         $thumbConfig $this->getConfig();
  217.         $pictureTagAttributes $options['pictureAttributes'] ?? []; // this is used for the html5 <picture> element
  218.         if (($options['lowQualityPlaceholder'] ?? false) && !Tool::isFrontendRequestByAdmin()) {
  219.             // this gets used in getImagTag() later, use a 1x1 transparent GIF as a fallback if no LQIP exists
  220.             $options['previewDataUri'] =  $image->getLowQualityPreviewDataUri() ?: $emptyGif;
  221.         }
  222.         $isAutoFormat $thumbConfig instanceof Config strtolower($thumbConfig->getFormat()) === 'source' false;
  223.         if ($isAutoFormat) {
  224.             // ensure the default image is not WebP
  225.             $this->pathReference = [];
  226.         }
  227.         $pictureCallback $options['pictureCallback'] ?? null;
  228.         if ($pictureCallback) {
  229.             $pictureTagAttributes $pictureCallback($pictureTagAttributes);
  230.         }
  231.         $html '<picture ' array_to_html_attribute_string($pictureTagAttributes) . '>' "\n";
  232.         if ($thumbConfig instanceof Config) {
  233.             $thumbConfigRes = clone $thumbConfig;
  234.             $html.= $this->getMediaConfigHtml($thumbConfigRes$image$options$isAutoFormat);
  235.         }
  236.         if (!($options['disableImgTag'] ?? null)) {
  237.             $html .= "\t" $this->getImageTag($options) . "\n";
  238.         }
  239.         $html .= '</picture>' "\n";
  240.         if ($options['useDataSrc'] ?? false) {
  241.             $html preg_replace('/ src(set)?=/i'' data-src$1='$html);
  242.         }
  243.         return $html;
  244.     }
  245.     protected function getMediaConfigHtml(Config $thumbConfigImage $image, array $optionsbool $isAutoFormat): string
  246.     {
  247.         $html '';
  248.         $mediaConfigs $thumbConfig->getMedias();
  249.         // currently only max-width is supported, the key of the media is WIDTHw (eg. 400w) according to the srcset specification
  250.         ksort($mediaConfigsSORT_NUMERIC);
  251.         array_push($mediaConfigs$thumbConfig->getItems()); //add the default config at the end - picturePolyfill v4
  252.         foreach ($mediaConfigs as $mediaQuery => $config) {
  253.             $thumbConfig->setItems($config);
  254.             $sourceHtml $this->getSourceTagHtml($thumbConfig$mediaQuery$image$options);
  255.             if (!empty($sourceHtml)) {
  256.                 if ($isAutoFormat) {
  257.                     foreach ($thumbConfig->getAutoFormatThumbnailConfigs() as $autoFormatConfig) {
  258.                         $autoFormatThumbnailHtml $this->getSourceTagHtml($autoFormatConfig$mediaQuery$image$options);
  259.                         if (!empty($autoFormatThumbnailHtml)) {
  260.                             $html .= "\t" $autoFormatThumbnailHtml "\n";
  261.                         }
  262.                     }
  263.                 }
  264.                 $html .= "\t" $sourceHtml "\n";
  265.             }
  266.         }
  267.         return $html;
  268.     }
  269.     /**
  270.      * @param array $options
  271.      * @param array $removeAttributes
  272.      *
  273.      * @return string
  274.      */
  275.     public function getImageTag(array $options = [], array $removeAttributes = []): string
  276.     {
  277.         /** @var Image $image */
  278.         $image $this->getAsset();
  279.         $attributes $options['imgAttributes'] ?? [];
  280.         $callback $options['imgCallback'] ?? null;
  281.         if (isset($options['previewDataUri'])) {
  282.             $attributes['src'] = $options['previewDataUri'];
  283.         } else {
  284.             $path $this->getPath();
  285.             $attributes['src'] = $this->addCacheBuster($path$options$image);
  286.         }
  287.         if (!isset($options['disableWidthHeightAttributes'])) {
  288.             if ($this->getWidth()) {
  289.                 $attributes['width'] = $this->getWidth();
  290.             }
  291.             if ($this->getHeight()) {
  292.                 $attributes['height'] = $this->getHeight();
  293.             }
  294.         }
  295.         $altText = !empty($options['alt']) ? $options['alt'] : (!empty($attributes['alt']) ? $attributes['alt'] : '');
  296.         $titleText = !empty($options['title']) ? $options['title'] : (!empty($attributes['title']) ? $attributes['title'] : '');
  297.         if (empty($titleText) && (!isset($options['disableAutoTitle']) || !$options['disableAutoTitle'])) {
  298.             if ($image->getMetadata('title')) {
  299.                 $titleText $image->getMetadata('title');
  300.             }
  301.         }
  302.         if (empty($altText) && (!isset($options['disableAutoAlt']) || !$options['disableAutoAlt'])) {
  303.             if ($image->getMetadata('alt')) {
  304.                 $altText $image->getMetadata('alt');
  305.             } elseif (isset($options['defaultalt'])) {
  306.                 $altText $options['defaultalt'];
  307.             } else {
  308.                 $altText $titleText;
  309.             }
  310.         }
  311.         // get copyright from asset
  312.         if ($image->getMetadata('copyright') && (!isset($options['disableAutoCopyright']) || !$options['disableAutoCopyright'])) {
  313.             if (!empty($altText)) {
  314.                 $altText .= ' | ';
  315.             }
  316.             if (!empty($titleText)) {
  317.                 $titleText .= ' | ';
  318.             }
  319.             $altText .= ('© ' $image->getMetadata('copyright'));
  320.             $titleText .= ('© ' $image->getMetadata('copyright'));
  321.         }
  322.         $attributes['alt'] = $altText;
  323.         if (!empty($titleText)) {
  324.             $attributes['title'] = $titleText;
  325.         }
  326.         if (!isset($attributes['loading'])) {
  327.             $attributes['loading'] = 'lazy';
  328.         }
  329.         foreach ($removeAttributes as $attribute) {
  330.             unset($attributes[$attribute]);
  331.         }
  332.         if ($callback) {
  333.             $attributes $callback($attributes);
  334.         }
  335.         $thumbConfig $this->getConfig();
  336.         if ($thumbConfig) {
  337.             $srcsetAttribute = isset($options['previewDataUri']) ? 'data-srcset' 'srcset';
  338.             $attributes[$srcsetAttribute] = $this->getSrcset($thumbConfig$image$options);
  339.         }
  340.         $htmlImgTag '';
  341.         if (!empty($attributes)) {
  342.             $htmlImgTag '<img ' array_to_html_attribute_string($attributes) . ' />';
  343.         }
  344.         return $htmlImgTag;
  345.     }
  346.     /**
  347.      * @param string $name
  348.      * @param int $highRes
  349.      *
  350.      * @return Thumbnail
  351.      *
  352.      * @throws \Exception
  353.      */
  354.     public function getMedia($name$highRes 1)
  355.     {
  356.         $thumbConfig $this->getConfig();
  357.         $mediaConfigs $thumbConfig->getMedias();
  358.         if (isset($mediaConfigs[$name])) {
  359.             $thumbConfigRes = clone $thumbConfig;
  360.             $thumbConfigRes->selectMedia($name);
  361.             $thumbConfigRes->setHighResolution($highRes);
  362.             $thumbConfigRes->setMedias([]);
  363.             /** @var Image $asset */
  364.             $asset $this->getAsset();
  365.             $thumb $asset->getThumbnail($thumbConfigRes);
  366.             return $thumb;
  367.         } else {
  368.             throw new \Exception("Media query '" $name "' doesn't exist in thumbnail configuration: " $thumbConfig->getName());
  369.         }
  370.     }
  371.     /**
  372.      * Get a thumbnail image configuration.
  373.      *
  374.      * @param string|array|Thumbnail\Config $selector Name, array or object describing a thumbnail configuration.
  375.      *
  376.      * @return Thumbnail\Config
  377.      *
  378.      * @throws NotFoundException
  379.      */
  380.     private function createConfig($selector)
  381.     {
  382.         $thumbnailConfig Thumbnail\Config::getByAutoDetect($selector);
  383.         if (!empty($selector) && $thumbnailConfig === null) {
  384.             throw new NotFoundException('Thumbnail definition "' . (is_string($selector) ? $selector '') . '" does not exist');
  385.         }
  386.         return $thumbnailConfig;
  387.     }
  388.     /**
  389.      * Get value that can be directly used ina srcset HTML attribute for images.
  390.      *
  391.      * @param Config $thumbConfig
  392.      * @param Image $image
  393.      * @param array $options
  394.      * @param string|null $mediaQuery Can be empty string if no media queries are defined.
  395.      *
  396.      * @return string Relative paths to different thunbnail images with 1x and 2x resolution
  397.      */
  398.     private function getSrcset(Config $thumbConfigImage $image, array $options, ?string $mediaQuery null): string
  399.     {
  400.         $srcSetValues = [];
  401.         foreach ([12] as $highRes) {
  402.             $thumbConfigRes = clone $thumbConfig;
  403.             if ($mediaQuery) {
  404.                 $thumbConfigRes->selectMedia($mediaQuery);
  405.             }
  406.             $thumbConfigRes->setHighResolution($highRes);
  407.             $thumb $image->getThumbnail($thumbConfigRestrue);
  408.             $descriptor $highRes 'x';
  409.             // encode comma in thumbnail path as srcset is a comma separated list
  410.             $srcSetValues[] = str_replace(',''%2C'$this->addCacheBuster($thumb ' ' $descriptor$options$image));
  411.             if ($this->useOriginalFile($this->asset->getFilename()) && $this->getConfig()->isSvgTargetFormatPossible()) {
  412.                 break;
  413.             }
  414.         }
  415.         return implode(', '$srcSetValues);
  416.     }
  417. }