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 all 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
66 changes: 63 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,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;
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((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));
Expand All @@ -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;
Expand Down Expand Up @@ -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'];
}
return $dimensions;
}
}