From 295be93e71fb294aac65b87475a810f7e518c8e4 Mon Sep 17 00:00:00 2001 From: muehle28 Date: Thu, 18 Aug 2016 21:49:34 +0200 Subject: [PATCH 1/6] Update SourceSetViewHelperTrait.php Crop feature for srcset pictures. Without this patch the srcset images are not rendered when a crop variable is set. --- Classes/Traits/SourceSetViewHelperTrait.php | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/Classes/Traits/SourceSetViewHelperTrait.php b/Classes/Traits/SourceSetViewHelperTrait.php index 5722ae1ac..6a480e055 100644 --- a/Classes/Traits/SourceSetViewHelperTrait.php +++ b/Classes/Traits/SourceSetViewHelperTrait.php @@ -34,18 +34,27 @@ public function addSourceSet($tag, $src) FrontendSimulationUtility::simulateFrontendEnvironment(); } + $width = $this->arguments['width']; + $height = $this->arguments['height']; + $dimendions = $this->getDimensions($width, $height); $format = $this->arguments['format']; $quality = $this->arguments['quality']; + $crop = $this->arguments['crop']; $treatIdAsReference = (boolean) $this->arguments['treatIdAsReference']; if (true === $treatIdAsReference) { $src = $this->arguments['src']; } + if ($crop === null) { + $crop = $src instanceof FileReference ? $src->getProperty('crop') : null; + } $imageSources = []; $srcsetVariants = []; foreach ($srcsets as $key => $width) { - $srcsetVariant = $this->getImgResource($src, $width, $format, $quality, $treatIdAsReference); + $height = floor((int)$width/$dimendions['ratio']) . $dimendions['postHeight']; + $width = $width . $dimendions['postWidth']; + $srcsetVariant = $this->getImgResource($src, $width, $height, $format, $quality, $treatIdAsReference, $crop); $srcsetVariantSrc = rawurldecode($srcsetVariant[3]); $srcsetVariantSrc = $this->preprocessSourceUri(GeneralUtility::rawUrlEncodeFP($srcsetVariantSrc)); @@ -72,18 +81,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 +130,16 @@ public function getSourceSetWidths() } return $srcsets; } + + private function getDimensions($width, $height){ + preg_match("/(\\d+)([a-zA-Z]+)/", $width, $width); + preg_match("/(\\d+)([a-zA-Z]+)/", $height, $height); + return array( + 'width'=>(int)$width[1], + 'height'=>(int)$height[1], + 'postWidth'=>$width[2], + 'postHeight'=>$height[2], + 'ratio'=>(int)$width[1]/(int)$height[1], + ); + } } From 4c4cb06bc190cf8cfb92648082521dd3dcf50b0c Mon Sep 17 00:00:00 2001 From: muehle28 Date: Fri, 19 Aug 2016 12:30:51 +0200 Subject: [PATCH 2/6] [FEATURE] Add crop feature to media.image srcset Division by zero check when calculating ratio Wrap can be ignored when $treatIdAsReference=TRUE Height calculation can be ignored when height attribute is missing When $treatIdAsReference = FALSE the generation of the srcset images does not work. There is a problem when processed images are getting processed again. If $treatIdAsReference = TRUE the srcset images are getting processed from the original image. Therefore the crop and ratio information is needed. --- Classes/Traits/SourceSetViewHelperTrait.php | 51 ++++++++++++++------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/Classes/Traits/SourceSetViewHelperTrait.php b/Classes/Traits/SourceSetViewHelperTrait.php index 6a480e055..613f20089 100644 --- a/Classes/Traits/SourceSetViewHelperTrait.php +++ b/Classes/Traits/SourceSetViewHelperTrait.php @@ -36,24 +36,31 @@ public function addSourceSet($tag, $src) $width = $this->arguments['width']; $height = $this->arguments['height']; - $dimendions = $this->getDimensions($width, $height); $format = $this->arguments['format']; $quality = $this->arguments['quality']; - $crop = $this->arguments['crop']; + $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); } - if ($crop === null) { - $crop = $src instanceof FileReference ? $src->getProperty('crop') : null; - } + $imageSources = []; $srcsetVariants = []; foreach ($srcsets as $key => $width) { - $height = floor((int)$width/$dimendions['ratio']) . $dimendions['postHeight']; - $width = $width . $dimendions['postWidth']; + if (0 < $dimensions['ratio']){ + $height = floor((int)$width/$dimensions['ratio']) . $dimensions['postHeight']; + } + + $width = $width . $dimensions['postWidth']; $srcsetVariant = $this->getImgResource($src, $width, $height, $format, $quality, $treatIdAsReference, $crop); $srcsetVariantSrc = rawurldecode($srcsetVariant[3]); @@ -132,14 +139,26 @@ public function getSourceSetWidths() } private function getDimensions($width, $height){ - preg_match("/(\\d+)([a-zA-Z]+)/", $width, $width); - preg_match("/(\\d+)([a-zA-Z]+)/", $height, $height); - return array( - 'width'=>(int)$width[1], - 'height'=>(int)$height[1], - 'postWidth'=>$width[2], - 'postHeight'=>$height[2], - 'ratio'=>(int)$width[1]/(int)$height[1], - ); + $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'=>(int)$widthSplit[1], + 'height'=>(int)$heightSplit[1], + 'postWidth'=>$widthSplit[2], + 'postHeight'=>$heightSplit[2], + 'ratio'=> 0, + ]; + if (0 < $dimensions['height']){ + $dimensions['ratio'] = $dimensions['width']/$dimensions['height']; + } + return $dimensions; } } From b0ae1f591a9b7242a10da16d44b99faa10d0b1fe Mon Sep 17 00:00:00 2001 From: Michael Mauracher Date: Fri, 19 Aug 2016 13:18:03 +0200 Subject: [PATCH 3/6] Class not found error When setting the quality attribute MathUtility was not found add use TYPO3\CMS\Core\Utility\MathUtility; --- Classes/Traits/SourceSetViewHelperTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Classes/Traits/SourceSetViewHelperTrait.php b/Classes/Traits/SourceSetViewHelperTrait.php index 613f20089..b3d57a83b 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; From 6fca9b010989d1488d5728c4147b933b5d5b21b6 Mon Sep 17 00:00:00 2001 From: Michael Mauracher Date: Fri, 19 Aug 2016 13:32:59 +0200 Subject: [PATCH 4/6] Spaces adjusted --- Classes/Traits/SourceSetViewHelperTrait.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Classes/Traits/SourceSetViewHelperTrait.php b/Classes/Traits/SourceSetViewHelperTrait.php index b3d57a83b..047b56712 100644 --- a/Classes/Traits/SourceSetViewHelperTrait.php +++ b/Classes/Traits/SourceSetViewHelperTrait.php @@ -57,7 +57,7 @@ public function addSourceSet($tag, $src) $srcsetVariants = []; foreach ($srcsets as $key => $width) { - if (0 < $dimensions['ratio']){ + if (0 < $dimensions['ratio']) { $height = floor((int)$width/$dimensions['ratio']) . $dimensions['postHeight']; } @@ -139,14 +139,15 @@ public function getSourceSetWidths() return $srcsets; } - private function getDimensions($width, $height){ + private function getDimensions($width, $height) + { $widthSplit = []; $heightSplit = []; - if (false === empty($width)){ + if (false === empty($width)) { preg_match("/(\\d+)([a-zA-Z]+)/", $width, $widthSplit); } - if (false === empty($height)){ + if (false === empty($height)) { preg_match("/(\\d+)([a-zA-Z]+)/", $height, $heightSplit); } @@ -157,7 +158,7 @@ private function getDimensions($width, $height){ 'postHeight'=>$heightSplit[2], 'ratio'=> 0, ]; - if (0 < $dimensions['height']){ + if (0 < $dimensions['height']) { $dimensions['ratio'] = $dimensions['width']/$dimensions['height']; } return $dimensions; From d650579ec636141e9814375240951ef6913c7063 Mon Sep 17 00:00:00 2001 From: Cedric Ziel Date: Tue, 13 Sep 2016 16:41:44 +0200 Subject: [PATCH 5/6] Adjust Codestyle --- Classes/Traits/SourceSetViewHelperTrait.php | 36 ++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/Classes/Traits/SourceSetViewHelperTrait.php b/Classes/Traits/SourceSetViewHelperTrait.php index 047b56712..b7b027ed4 100644 --- a/Classes/Traits/SourceSetViewHelperTrait.php +++ b/Classes/Traits/SourceSetViewHelperTrait.php @@ -40,7 +40,7 @@ public function addSourceSet($tag, $src) $format = $this->arguments['format']; $quality = $this->arguments['quality']; $dimensions = [ - 'ratio'=>0, + 'ratio' => 0, ]; $treatIdAsReference = (boolean) $this->arguments['treatIdAsReference']; if (true === $treatIdAsReference) { @@ -58,11 +58,19 @@ public function addSourceSet($tag, $src) foreach ($srcsets as $key => $width) { if (0 < $dimensions['ratio']) { - $height = floor((int)$width/$dimensions['ratio']) . $dimensions['postHeight']; + $height = floor((integer)$width / $dimensions['ratio']) . $dimensions['postHeight']; } $width = $width . $dimensions['postWidth']; - $srcsetVariant = $this->getImgResource($src, $width, $height, $format, $quality, $treatIdAsReference, $crop); + $srcsetVariant = $this->getImgResource( + $src, + $width, + $height, + $format, + $quality, + $treatIdAsReference, + $crop + ); $srcsetVariantSrc = rawurldecode($srcsetVariant[3]); $srcsetVariantSrc = $this->preprocessSourceUri(GeneralUtility::rawUrlEncodeFP($srcsetVariantSrc)); @@ -139,24 +147,30 @@ public function getSourceSetWidths() return $srcsets; } - private function getDimensions($width, $height) + /** + * @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); + preg_match('/(\\d+)([a-zA-Z]+)/', $width, $widthSplit); } if (false === empty($height)) { - preg_match("/(\\d+)([a-zA-Z]+)/", $height, $heightSplit); + preg_match('/(\\d+)([a-zA-Z]+)/', $height, $heightSplit); } $dimensions = [ - 'width'=>(int)$widthSplit[1], - 'height'=>(int)$heightSplit[1], - 'postWidth'=>$widthSplit[2], - 'postHeight'=>$heightSplit[2], - 'ratio'=> 0, + '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']; From baefc7738d405c8b5bdaebea3871f05f6bcc36e3 Mon Sep 17 00:00:00 2001 From: Claus Due Date: Tue, 13 Sep 2016 16:47:01 +0200 Subject: [PATCH 6/6] Adjust CGL --- Classes/Traits/SourceSetViewHelperTrait.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Classes/Traits/SourceSetViewHelperTrait.php b/Classes/Traits/SourceSetViewHelperTrait.php index b7b027ed4..46ec00f98 100644 --- a/Classes/Traits/SourceSetViewHelperTrait.php +++ b/Classes/Traits/SourceSetViewHelperTrait.php @@ -58,7 +58,7 @@ public function addSourceSet($tag, $src) foreach ($srcsets as $key => $width) { if (0 < $dimensions['ratio']) { - $height = floor((integer)$width / $dimensions['ratio']) . $dimensions['postHeight']; + $height = floor((integer) $width / $dimensions['ratio']) . $dimensions['postHeight']; } $width = $width . $dimensions['postWidth']; @@ -166,14 +166,14 @@ protected function getDimensions($width, $height) } $dimensions = [ - 'width' => (integer)$widthSplit[1], - 'height' => (integer)$heightSplit[1], + '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']; + $dimensions['ratio'] = $dimensions['width'] / $dimensions['height']; } return $dimensions; }