/**
  * Creates a picture configuration.
  *
  * @param int|array|null $size
  *
  * @return array<PictureConfiguration,array>
  */
 private function createConfig($size)
 {
     if (!is_array($size)) {
         $size = [0, 0, $size];
     }
     $config = new PictureConfiguration();
     $attributes = [];
     if (!isset($size[2]) || !is_numeric($size[2])) {
         $resizeConfig = new ResizeConfiguration();
         if (isset($size[0]) && $size[0]) {
             $resizeConfig->setWidth($size[0]);
         }
         if (isset($size[1]) && $size[1]) {
             $resizeConfig->setHeight($size[1]);
         }
         if (isset($size[2]) && $size[2]) {
             $resizeConfig->setMode($size[2]);
         }
         $configItem = new PictureConfigurationItem();
         $configItem->setResizeConfig($resizeConfig);
         if ($this->defaultDensities) {
             $configItem->setDensities($this->defaultDensities);
         }
         $config->setSize($configItem);
         return [$config, $attributes];
     }
     /** @var ImageSizeModel $imageSizeModel */
     $imageSizeModel = $this->framework->getAdapter(ImageSizeModel::class);
     $imageSizes = $imageSizeModel->findByPk($size[2]);
     $config->setSize($this->createConfigItem($imageSizes));
     if ($imageSizes && $imageSizes->cssClass) {
         $attributes['class'] = $imageSizes->cssClass;
     }
     /** @var ImageSizeItemModel $imageSizeItemModel */
     $imageSizeItemModel = $this->framework->getAdapter(ImageSizeItemModel::class);
     $imageSizeItems = $imageSizeItemModel->findVisibleByPid($size[2], ['order' => 'sorting ASC']);
     if (null !== $imageSizeItems) {
         $configItems = [];
         foreach ($imageSizeItems as $imageSizeItem) {
             $configItems[] = $this->createConfigItem($imageSizeItem);
         }
         $config->setSizeItems($configItems);
     }
     return [$config, $attributes];
 }
Exemple #2
0
 /**
  * Get the picture element definition array
  *
  * @return array The picture element definition
  */
 public function getTemplateData()
 {
     $image = \System::getContainer()->get('contao.image.image_factory')->create(TL_ROOT . '/' . $this->image->getOriginalPath());
     $config = new PictureConfiguration();
     $config->setSize($this->getConfigurationItem($this->imageSize));
     $sizeItems = array();
     foreach ($this->imageSizeItems as $imageSizeItem) {
         $sizeItems[] = $this->getConfigurationItem($imageSizeItem);
     }
     $config->setSizeItems($sizeItems);
     $importantPart = $this->image->getImportantPart();
     $image->setImportantPart(new ImportantPart(new Point($importantPart['x'], $importantPart['y']), new Box($importantPart['width'], $importantPart['height'])));
     $container = \System::getContainer();
     $picture = $container->get('contao.image.picture_generator')->generate($image, $config, (new ResizeOptions())->setImagineOptions($container->getParameter('contao.image.imagine_options'))->setBypassCache($container->getParameter('contao.image.bypass_cache')));
     return array('img' => $picture->getImg(TL_ROOT), 'sources' => $picture->getSources(TL_ROOT));
 }