-
Notifications
You must be signed in to change notification settings - Fork 231
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
base: development
Are you sure you want to change the base?
Changes from 4 commits
295be93
4c4cb06
b0ae1f5
6fca9b0
d650579
baefc77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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, | ||
]; | ||
$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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
} | ||
|
||
$width = $width . $dimensions['postWidth']; | ||
$srcsetVariant = $this->getImgResource($src, $width, $height, $format, $quality, $treatIdAsReference, $crop); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
@@ -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; | ||
|
@@ -117,4 +138,29 @@ public function getSourceSetWidths() | |
} | ||
return $srcsets; | ||
} | ||
|
||
private function getDimensions($width, $height) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary double quotes, please convert to single qoutes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and the other pattern should explicitly scan for only the |
||
} | ||
|
||
if (false === empty($height)) { | ||
preg_match("/(\\d+)([a-zA-Z]+)/", $height, $heightSplit); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary double quotes. |
||
} | ||
|
||
$dimensions = [ | ||
'width'=>(int)$widthSplit[1], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces before/after |
||
'height'=>(int)$heightSplit[1], | ||
'postWidth'=>$widthSplit[2], | ||
'postHeight'=>$heightSplit[2], | ||
'ratio'=> 0, | ||
]; | ||
if (0 < $dimensions['height']) { | ||
$dimensions['ratio'] = $dimensions['width']/$dimensions['height']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces, division sign. |
||
} | ||
return $dimensions; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space before and after
=>
.