Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add crop feature to media.image srcset #1096

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
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
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
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
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
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
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
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
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
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
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
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;
}
}