-
Notifications
You must be signed in to change notification settings - Fork 233
[FEATURE] Add crop feature to media.image srcset #1096
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
Open
muehle28
wants to merge
6
commits into
FluidTYPO3:development
Choose a base branch
from
muehle28:patch-1
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
295be93
Update SourceSetViewHelperTrait.php
muehle28 4c4cb06
[FEATURE] Add crop feature to media.image srcset
muehle28 b0ae1f5
Class not found error
muehle28 6fca9b0
Spaces adjusted
muehle28 d650579
Adjust Codestyle
cedricziel baefc77
Adjust CGL
NamelessCoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,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']; | ||
|
Member
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; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We better import FileReference