vendor/contao/core-bundle/src/Resources/contao/library/Contao/Picture.php line 232

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Contao\Image\ImportantPart;
  11. use Contao\Image\PictureConfiguration;
  12. use Contao\Image\PictureConfigurationItem;
  13. use Contao\Image\ResizeConfiguration;
  14. use Contao\Image\ResizeOptions;
  15. use Contao\Model\Collection;
  16. trigger_deprecation('contao/core-bundle''4.3''Using the "Contao\Picture" class has been deprecated and will no longer work in Contao 5.0. Use the "contao.image.picture_factory" service instead.');
  17. /**
  18.  * Resizes images and creates picture data
  19.  *
  20.  * The class resizes images and prepares data for the `<picture>` element.
  21.  *
  22.  * Usage:
  23.  *
  24.  *     $picture = new Picture(new File('example.jpg'));
  25.  *
  26.  *     $data = $picture
  27.  *         ->setImportantPart(array('x'=>10, 'y'=>10, 'width'=>100, 'height'=>100))
  28.  *         ->setImageSize(ImageSizeModel::findByPk(1))
  29.  *         ->setImageSizeItems(ImageSizeItemModel::findVisibleByPid(1, array('order'=>'sorting ASC')))
  30.  *         ->getTemplateData()
  31.  *     ;
  32.  *
  33.  *     // Shortcut
  34.  *     $data = Picture::create('example.jpg', 1)->getTemplateData();
  35.  *     $data = Picture::create('example.jpg', array(100, 100, 'crop'))->getTemplateData();
  36.  *
  37.  * @deprecated Deprecated since Contao 4.3, to be removed in Contao 5.0.
  38.  *             Use the contao.image.picture_factory service instead.
  39.  */
  40. class Picture
  41. {
  42.     /**
  43.      * The Image instance of the source image
  44.      *
  45.      * @var Image
  46.      */
  47.     protected $image;
  48.     /**
  49.      * The image size
  50.      *
  51.      * @var ImageSizeModel|object
  52.      */
  53.     protected $imageSize;
  54.     /**
  55.      * The image size items collection
  56.      *
  57.      * @var array<ImageSizeItemModel|object>|Collection
  58.      */
  59.     protected $imageSizeItems = array();
  60.     /**
  61.      * Create a new object to handle a picture element
  62.      *
  63.      * @param File $file A file instance of the source image
  64.      */
  65.     public function __construct(File $file)
  66.     {
  67.         $this->image = new Image($file);
  68.     }
  69.     /**
  70.      * Create a picture instance from the given image path and size
  71.      *
  72.      * @param string|File          $file The image path or File instance
  73.      * @param array|integer|string $size The image size as array (width, height, resize mode) or a tl_image_size ID or a predefined image size key
  74.      *
  75.      * @return static The created picture instance
  76.      */
  77.     public static function create($file$size=null)
  78.     {
  79.         if (\is_string($file))
  80.         {
  81.             $file = new File(rawurldecode($file));
  82.         }
  83.         $picture = new static($file);
  84.         if (\is_array($size) && !empty($size[2]))
  85.         {
  86.             // tl_image_size ID as resize mode
  87.             if (is_numeric($size[2]))
  88.             {
  89.                 $size = (int) $size[2];
  90.             }
  91.             // Predefined image size as resize mode
  92.             elseif (\is_string($size[2]) && $size[2][0] === '_')
  93.             {
  94.                 $size $size[2];
  95.             }
  96.         }
  97.         $imageSize null;
  98.         if (!\is_array($size) && (!\is_string($size) || $size[0] !== '_'))
  99.         {
  100.             $imageSize ImageSizeModel::findByPk($size);
  101.             if ($imageSize === null)
  102.             {
  103.                 $size = array();
  104.             }
  105.         }
  106.         if (\is_string($size) && $size[0] === '_')
  107.         {
  108.             $imageSize $size;
  109.         }
  110.         if (\is_array($size))
  111.         {
  112.             $size += array(00'crop');
  113.             $imageSize = new \stdClass();
  114.             $imageSize->width $size[0];
  115.             $imageSize->height $size[1];
  116.             $imageSize->resizeMode $size[2];
  117.             $imageSize->zoom 0;
  118.         }
  119.         $picture->setImageSize($imageSize);
  120.         if ($imageSize !== null && !empty($imageSize->id))
  121.         {
  122.             $picture->setImageSizeItems(ImageSizeItemModel::findVisibleByPid($imageSize->id, array('order'=>'sorting ASC')));
  123.         }
  124.         $fileRecord FilesModel::findByPath($file->path);
  125.         $currentSize $file->imageViewSize;
  126.         if ($fileRecord !== null && $fileRecord->importantPartWidth && $fileRecord->importantPartHeight)
  127.         {
  128.             $picture->setImportantPart(array
  129.             (
  130.                 'x' => (int) ($fileRecord->importantPartX $currentSize[0]),
  131.                 'y' => (int) ($fileRecord->importantPartY $currentSize[1]),
  132.                 'width' => (int) ($fileRecord->importantPartWidth $currentSize[0]),
  133.                 'height' => (int) ($fileRecord->importantPartHeight $currentSize[1]),
  134.             ));
  135.         }
  136.         return $picture;
  137.     }
  138.     /**
  139.      * Set the important part settings
  140.      *
  141.      * @param array $importantPart The settings array
  142.      *
  143.      * @return $this The picture object
  144.      */
  145.     public function setImportantPart(array $importantPart null)
  146.     {
  147.         $this->image->setImportantPart($importantPart);
  148.         return $this;
  149.     }
  150.     /**
  151.      * Set the image size
  152.      *
  153.      * @param ImageSizeModel|object|string $imageSize The image size or a predefined image size key
  154.      *
  155.      * @return $this The picture object
  156.      */
  157.     public function setImageSize($imageSize)
  158.     {
  159.         $this->imageSize $imageSize;
  160.         return $this;
  161.     }
  162.     /**
  163.      * Set the image size items collection
  164.      *
  165.      * @param array<ImageSizeItemModel|object>|Collection $imageSizeItems The image size items collection
  166.      *
  167.      * @return $this The picture object
  168.      */
  169.     public function setImageSizeItems($imageSizeItems)
  170.     {
  171.         if ($imageSizeItems === null)
  172.         {
  173.             $imageSizeItems = array();
  174.         }
  175.         $this->imageSizeItems $imageSizeItems;
  176.         return $this;
  177.     }
  178.     /**
  179.      * Get the picture element definition array
  180.      *
  181.      * @return array The picture element definition
  182.      */
  183.     public function getTemplateData()
  184.     {
  185.         $projectDir System::getContainer()->getParameter('kernel.project_dir');
  186.         $image System::getContainer()->get('contao.image.factory')->create($projectDir '/' $this->image->getOriginalPath());
  187.         if (\is_string($this->imageSize) && $this->imageSize[0] === '_')
  188.         {
  189.             $config $this->imageSize;
  190.         }
  191.         else
  192.         {
  193.             $config = new PictureConfiguration();
  194.             $config->setSize($this->getConfigurationItem($this->imageSize));
  195.             $sizeItems = array();
  196.             foreach ($this->imageSizeItems as $imageSizeItem)
  197.             {
  198.                 $sizeItems[] = $this->getConfigurationItem($imageSizeItem);
  199.             }
  200.             $config->setSizeItems($sizeItems);
  201.         }
  202.         $importantPart $this->image->getImportantPart();
  203.         $imageSize $image->getDimensions()->getSize();
  204.         $image->setImportantPart(
  205.             new ImportantPart(
  206.                 $importantPart['x'] / $imageSize->getWidth(),
  207.                 $importantPart['y'] / $imageSize->getHeight(),
  208.                 $importantPart['width'] / $imageSize->getWidth(),
  209.                 $importantPart['height'] / $imageSize->getHeight()
  210.             )
  211.         );
  212.         $container System::getContainer();
  213.         $staticUrl $container->get('contao.assets.files_context')->getStaticUrl();
  214.         $picture $container
  215.             ->get('contao.image.picture_factory')
  216.             ->create(
  217.                 $image,
  218.                 $config,
  219.                 (new ResizeOptions())
  220.                     ->setImagineOptions($container->getParameter('contao.image.imagine_options'))
  221.                     ->setBypassCache($container->getParameter('contao.image.bypass_cache'))
  222.                     ->setSkipIfDimensionsMatch(true)
  223.             )
  224.         ;
  225.         return array
  226.         (
  227.             'img' => $picture->getImg($projectDir$staticUrl),
  228.             'sources' => $picture->getSources($projectDir$staticUrl),
  229.         );
  230.     }
  231.     /**
  232.      * Get the config for one picture source element
  233.      *
  234.      * @param ImageSizeModel|ImageSizeItemModel|object $imageSize The image size or image size item model
  235.      *
  236.      * @return PictureConfigurationItem
  237.      */
  238.     protected function getConfigurationItem($imageSize)
  239.     {
  240.         $configItem = new PictureConfigurationItem();
  241.         $resizeConfig = new ResizeConfiguration();
  242.         $mode $imageSize->resizeMode;
  243.         if (substr_count($mode'_') === 1)
  244.         {
  245.             $importantPart $this->image->setImportantPart()->getImportantPart();
  246.             $mode explode('_'$mode);
  247.             if ($mode[0] === 'left')
  248.             {
  249.                 $importantPart['width'] = 1;
  250.             }
  251.             elseif ($mode[0] === 'right')
  252.             {
  253.                 $importantPart['x'] = $importantPart['width'] - 1;
  254.                 $importantPart['width'] = 1;
  255.             }
  256.             if ($mode[1] === 'top')
  257.             {
  258.                 $importantPart['height'] = 1;
  259.             }
  260.             elseif ($mode[1] === 'bottom')
  261.             {
  262.                 $importantPart['y'] = $importantPart['height'] - 1;
  263.                 $importantPart['height'] = 1;
  264.             }
  265.             $this->image->setImportantPart($importantPart);
  266.             $mode ResizeConfiguration::MODE_CROP;
  267.         }
  268.         $resizeConfig
  269.             ->setWidth((int) $imageSize->width)
  270.             ->setHeight((int) $imageSize->height)
  271.             ->setZoomLevel((int) $imageSize->zoom)
  272.         ;
  273.         if ($mode)
  274.         {
  275.             $resizeConfig->setMode($mode);
  276.         }
  277.         $configItem->setResizeConfig($resizeConfig);
  278.         if (isset($imageSize->sizes))
  279.         {
  280.             $configItem->setSizes($imageSize->sizes);
  281.         }
  282.         if (isset($imageSize->densities))
  283.         {
  284.             $configItem->setDensities($imageSize->densities);
  285.         }
  286.         if (isset($imageSize->media))
  287.         {
  288.             $configItem->setMedia($imageSize->media);
  289.         }
  290.         return $configItem;
  291.     }
  292. }
  293. class_alias(Picture::class, 'Picture');