Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions Classes/Traits/SourceSetViewHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -34,18 +35,34 @@ 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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space before and after =>.

];
$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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We better import FileReference

}
$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((int)$width/$dimensions['ratio']) . $dimensions['postHeight'];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use (integer) to cast, space after cast, space before and after division sign.

}

$width = $width . $dimensions['postWidth'];
$srcsetVariant = $this->getImgResource($src, $width, $height, $format, $quality, $treatIdAsReference, $crop);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know that's super picky, but could you transform the call to a multi-line one? The code style still fails on checking it.

$this->getImgResource(
  $src, 
  $width, 
  $height,
  $format, 
  $quality, 
  $treatIdAsReference, 
  $crop
);


$srcsetVariantSrc = rawurldecode($srcsetVariant[3]);
$srcsetVariantSrc = $this->preprocessSourceUri(GeneralUtility::rawUrlEncodeFP($srcsetVariantSrc));
Expand All @@ -72,18 +89,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;
Expand Down Expand Up @@ -117,4 +138,29 @@ public function getSourceSetWidths()
}
return $srcsets;
}

private function getDimensions($width, $height)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must have phpdoc, should not be a private method (in general we avoid those due to testing concerns).

{
$widthSplit = [];
$heightSplit = [];
if (false === empty($width)) {
preg_match("/(\\d+)([a-zA-Z]+)/", $width, $widthSplit);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary double quotes, please convert to single qoutes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the other pattern should explicitly scan for only the c and m modifiers.

}

if (false === empty($height)) {
preg_match("/(\\d+)([a-zA-Z]+)/", $height, $heightSplit);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary double quotes.

}

$dimensions = [
'width'=>(int)$widthSplit[1],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces before/after =>, use (integer) when casting.

'height'=>(int)$heightSplit[1],
'postWidth'=>$widthSplit[2],
'postHeight'=>$heightSplit[2],
'ratio'=> 0,
];
if (0 < $dimensions['height']) {
$dimensions['ratio'] = $dimensions['width']/$dimensions['height'];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces, division sign.

}
return $dimensions;
}
}