/** * Creates a picture configuration item. * * @param ImageSizeModel|ImageSizeItemModel|null $imageSize * * @return PictureConfigurationItem */ private function createConfigItem($imageSize) { $configItem = new PictureConfigurationItem(); $resizeConfig = new ResizeConfiguration(); if (null !== $imageSize) { $resizeConfig->setWidth($imageSize->width)->setHeight($imageSize->height)->setMode($imageSize->resizeMode)->setZoomLevel($imageSize->zoom); $configItem->setResizeConfig($resizeConfig)->setSizes($imageSize->sizes)->setDensities($imageSize->densities); if (isset($imageSize->media)) { $configItem->setMedia($imageSize->media); } } return $configItem; }
/** * Creates a resize configuration object. * * @param int|array|null $size An image size or an array with width, height and resize mode * @param ImageInterface $image * * @return array */ private function createConfig($size, ImageInterface $image) { if (!is_array($size)) { $size = [0, 0, $size]; } $config = new ResizeConfiguration(); if (isset($size[2]) && is_numeric($size[2])) { /** @var ImageSizeModel $imageModel */ $imageModel = $this->framework->getAdapter(ImageSizeModel::class); $imageSize = $imageModel->findByPk($size[2]); if (null !== $imageSize) { $config->setWidth($imageSize->width)->setHeight($imageSize->height)->setMode($imageSize->resizeMode)->setZoomLevel($imageSize->zoom); } return [$config, null]; } if (isset($size[0]) && $size[0]) { $config->setWidth($size[0]); } if (isset($size[1]) && $size[1]) { $config->setHeight($size[1]); } if (!isset($size[2]) || 1 !== substr_count($size[2], '_')) { if (isset($size[2]) && $size[2]) { $config->setMode($size[2]); } return [$config, null]; } $config->setMode(ResizeConfigurationInterface::MODE_CROP); return [$config, $this->getImportantPartFromLegacyMode($image, $size[2])]; }
/** * Get the config for one picture source element * * @param Model|object $imageSize The image size or image size item model * * @return PictureConfigurationItem */ protected function getConfigurationItem($imageSize) { $configItem = new PictureConfigurationItem(); $resizeConfig = new ResizeConfiguration(); $mode = $imageSize->resizeMode; if (substr_count($mode, '_') === 1) { $importantPart = $this->image->setImportantPart(null)->getImportantPart(); $mode = explode('_', $mode); if ($mode[0] === 'left') { $importantPart['width'] = 1; } elseif ($mode[0] === 'right') { $importantPart['x'] = $importantPart['width'] - 1; $importantPart['width'] = 1; } if ($mode[1] === 'top') { $importantPart['height'] = 1; } elseif ($mode[1] === 'bottom') { $importantPart['y'] = $importantPart['height'] - 1; $importantPart['height'] = 1; } $this->image->setImportantPart($importantPart); $mode = ResizeConfiguration::MODE_CROP; } $resizeConfig->setWidth($imageSize->width)->setHeight($imageSize->height)->setZoomLevel($imageSize->zoom); if ($mode) { $resizeConfig->setMode($mode); } $configItem->setResizeConfig($resizeConfig); if (isset($imageSize->sizes)) { $configItem->setSizes($imageSize->sizes); } if (isset($imageSize->densities)) { $configItem->setDensities($imageSize->densities); } if (isset($imageSize->media)) { $configItem->setMedia($imageSize->media); } return $configItem; }
/** * Prepare resize configuration object. * * @return ResizeConfiguration */ protected function prepareResizeConfig() { $resizeConfig = new ResizeConfiguration(); $resizeConfig->setWidth($this->targetWidth); $resizeConfig->setHeight($this->targetHeight); $resizeConfig->setZoomLevel($this->zoomLevel); if (substr_count($this->resizeMode, '_') === 1) { $resizeConfig->setMode(ResizeConfiguration::MODE_CROP); $resizeConfig->setZoomLevel(0); } else { try { $resizeConfig->setMode($this->resizeMode); } catch (\InvalidArgumentException $exception) { $resizeConfig->setMode(ResizeConfiguration::MODE_CROP); } } return $resizeConfig; }
/** * Tests the setZoomLevel() method. * * @expectedException \InvalidArgumentException */ public function testSetZoomLevelTooHigh() { $config = new ResizeConfiguration(); $this->setExpectedException('InvalidArgumentException'); $config->setZoomLevel(101); }