/** * {@inheritdoc} */ public function resize(ImageInterface $image, ResizeConfigurationInterface $config, ResizeOptionsInterface $options) { if ($image->getDimensions()->isUndefined() || $config->isEmpty()) { $image = $this->createImage($image, $image->getPath()); } else { $image = $this->processResize($image, $config, $options); } if (null !== $options->getTargetPath()) { $this->filesystem->copy($image->getPath(), $options->getTargetPath(), true); $image = $this->createImage($image, $options->getTargetPath()); } return $image; }
/** * {@inheritdoc} */ public function resize(ImageInterface $image, ResizeConfigurationInterface $config, ResizeOptionsInterface $options) { if (!empty($GLOBALS['TL_HOOKS']['executeResize']) && is_array($GLOBALS['TL_HOOKS']['executeResize']) || !empty($GLOBALS['TL_HOOKS']['getImage']) && is_array($GLOBALS['TL_HOOKS']['getImage'])) { @trigger_error('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.', E_USER_DEPRECATED); $this->legacyImage = null; $legacyPath = $image->getPath(); if (0 === strpos($legacyPath, TL_ROOT . '/') || 0 === strpos($legacyPath, TL_ROOT . '\\')) { $legacyPath = substr($legacyPath, strlen(TL_ROOT) + 1); $this->legacyImage = new LegacyImage(new File($legacyPath)); $this->legacyImage->setTargetWidth($config->getWidth()); $this->legacyImage->setTargetHeight($config->getHeight()); $this->legacyImage->setResizeMode($config->getMode()); $this->legacyImage->setZoomLevel($config->getZoomLevel()); if ($options->getTargetPath() && (0 === strpos($options->getTargetPath(), TL_ROOT . '/') || 0 === strpos($options->getTargetPath(), TL_ROOT . '\\'))) { $this->legacyImage->setTargetPath(substr($options->getTargetPath(), strlen(TL_ROOT) + 1)); } $importantPart = $image->getImportantPart(); $this->legacyImage->setImportantPart(['x' => $importantPart->getPosition()->getX(), 'y' => $importantPart->getPosition()->getY(), 'width' => $importantPart->getSize()->getWidth(), 'height' => $importantPart->getSize()->getHeight()]); } } if (isset($GLOBALS['TL_HOOKS']['executeResize']) && is_array($GLOBALS['TL_HOOKS']['executeResize']) && $this->legacyImage) { foreach ($GLOBALS['TL_HOOKS']['executeResize'] as $callback) { $return = System::importStatic($callback[0])->{$callback[1]}($this->legacyImage); if (is_string($return)) { return $this->createImage($image, TL_ROOT . '/' . $return); } } } return parent::resize($image, $config, $options); }
/** * {@inheritdoc} */ public function calculate(ResizeConfigurationInterface $config, ImageDimensionsInterface $dimensions, ImportantPartInterface $importantPart = null) { $zoom = max(0, min(1, (int) $config->getZoomLevel() / 100)); $importantPart = $this->importantPartAsArray($dimensions, $importantPart); // If both dimensions are present, use the mode specific calculations if ($config->getWidth() && $config->getHeight()) { $widthHeight = [$config->getWidth(), $config->getHeight()]; switch ($config->getMode()) { case ResizeConfigurationInterface::MODE_CROP: return $this->calculateCrop($widthHeight, $dimensions, $importantPart, $zoom); case ResizeConfigurationInterface::MODE_PROPORTIONAL: return $this->calculateProportional($widthHeight, $dimensions, $importantPart, $zoom); case ResizeConfigurationInterface::MODE_BOX: return $this->calculateBox($widthHeight, $dimensions, $importantPart, $zoom); } throw new \InvalidArgumentException(sprintf('Unsupported resize mode "%s"', $config->getMode())); } // If no dimensions are specified, use the zoomed important part if (!$config->getWidth() && !$config->getHeight()) { $zoomedImportantPart = $this->zoomImportantPart($importantPart, $zoom, $dimensions->getSize()); return $this->buildCoordinates([$dimensions->getSize()->getWidth(), $dimensions->getSize()->getHeight()], [$zoomedImportantPart['x'], $zoomedImportantPart['y']], [$zoomedImportantPart['width'], $zoomedImportantPart['height']], $dimensions); } // If only one dimension is specified, use the single dimension calculation return $this->calculateSingleDimension([$config->getWidth(), $config->getHeight()], $dimensions, $this->zoomImportantPart($importantPart, $zoom, $dimensions->getSize())); }