diff --git a/Classes/Traits/SourceSetViewHelperTrait.php b/Classes/Traits/SourceSetViewHelperTrait.php index 5722ae1ac..46ec00f98 100644 --- a/Classes/Traits/SourceSetViewHelperTrait.php +++ b/Classes/Traits/SourceSetViewHelperTrait.php @@ -2,6 +2,7 @@ namespace FluidTYPO3\Vhs\Traits; use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility; +use TYPO3\CMS\Core\Utility\MathUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; @@ -34,18 +35,42 @@ public function addSourceSet($tag, $src) FrontendSimulationUtility::simulateFrontendEnvironment(); } + $width = $this->arguments['width']; + $height = $this->arguments['height']; $format = $this->arguments['format']; $quality = $this->arguments['quality']; + $dimensions = [ + 'ratio' => 0, + ]; $treatIdAsReference = (boolean) $this->arguments['treatIdAsReference']; if (true === $treatIdAsReference) { $src = $this->arguments['src']; + $crop = $this->arguments['crop']; + if ($crop === null) { + $crop = $src instanceof FileReference ? $src->getProperty('crop') : null; + } + $dimensions = $this->getDimensions($width, $height); } + $imageSources = []; $srcsetVariants = []; foreach ($srcsets as $key => $width) { - $srcsetVariant = $this->getImgResource($src, $width, $format, $quality, $treatIdAsReference); + if (0 < $dimensions['ratio']) { + $height = floor((integer) $width / $dimensions['ratio']) . $dimensions['postHeight']; + } + + $width = $width . $dimensions['postWidth']; + $srcsetVariant = $this->getImgResource( + $src, + $width, + $height, + $format, + $quality, + $treatIdAsReference, + $crop + ); $srcsetVariantSrc = rawurldecode($srcsetVariant[3]); $srcsetVariantSrc = $this->preprocessSourceUri(GeneralUtility::rawUrlEncodeFP($srcsetVariantSrc)); @@ -72,18 +97,22 @@ public function addSourceSet($tag, $src) * * @param string $src path of the image to convert * @param integer $width width to convert the image to + * @param integer $height height to convert the image to * @param string $format format of the resulting copy * @param string $quality quality of the resulting copy * @param string $treatIdAsReference given src argument is a sys_file_reference record + * @param string $crop image crop string * @param array $params additional params for the image rendering * @return string */ - public function getImgResource($src, $width, $format, $quality, $treatIdAsReference, $params = null) + public function getImgResource($src, $width, $height, $format, $quality, $treatIdAsReference, $crop, $params = null) { $setup = [ 'width' => $width, - 'treatIdAsReference' => $treatIdAsReference + 'height' => $height, + 'treatIdAsReference' => $treatIdAsReference, + 'crop' => $crop, ]; if (false === empty($format)) { $setup['ext'] = $format; @@ -117,4 +146,35 @@ public function getSourceSetWidths() } return $srcsets; } + + /** + * @param integer $width + * @param integer $height + * + * @return array + */ + protected function getDimensions($width, $height) + { + $widthSplit = []; + $heightSplit = []; + if (false === empty($width)) { + preg_match('/(\\d+)([a-zA-Z]+)/', $width, $widthSplit); + } + + if (false === empty($height)) { + preg_match('/(\\d+)([a-zA-Z]+)/', $height, $heightSplit); + } + + $dimensions = [ + 'width' => (integer) $widthSplit[1], + 'height' => (integer) $heightSplit[1], + 'postWidth' => $widthSplit[2], + 'postHeight' => $heightSplit[2], + 'ratio' => 0, + ]; + if (0 < $dimensions['height']) { + $dimensions['ratio'] = $dimensions['width'] / $dimensions['height']; + } + return $dimensions; + } }