X-Git-Url: http://git.typo3.org/Packages/TYPO3.CMS.git/blobdiff_plain/121c9ca653e29f139f12105513ebb3140d6609f6..f4a73f2f10263b1a23d7b7a693bbf3adb6351fbc:/typo3/sysext/core/Classes/Imaging/ImageManipulation/CropVariant.php?ds=sidebyside diff --git a/typo3/sysext/core/Classes/Imaging/ImageManipulation/CropVariant.php b/typo3/sysext/core/Classes/Imaging/ImageManipulation/CropVariant.php index f84ec4b13a6..1645148ad7b 100644 --- a/typo3/sysext/core/Classes/Imaging/ImageManipulation/CropVariant.php +++ b/typo3/sysext/core/Classes/Imaging/ImageManipulation/CropVariant.php @@ -15,6 +15,8 @@ namespace TYPO3\CMS\Core\Imaging\ImageManipulation; * The TYPO3 project - inspiring people to share! */ +use TYPO3\CMS\Core\Resource\FileInterface; + class CropVariant { /** @@ -76,8 +78,12 @@ class CropVariant $this->cropArea = $cropArea; if ($allowedAspectRatios) { $this->setAllowedAspectRatios(...$allowedAspectRatios); + if ($selectedRatio && isset($this->allowedAspectRatios[$selectedRatio])) { + $this->selectedRatio = $selectedRatio; + } else { + $this->selectedRatio = current($this->allowedAspectRatios)->getId(); + } } - $this->selectedRatio = $selectedRatio; $this->focusArea = $focusArea; if ($coverAreas !== null) { $this->setCoverAreas(...$coverAreas); @@ -158,6 +164,22 @@ class CropVariant return $this->focusArea; } + /** + * @param FileInterface $file + * @return CropVariant + */ + public function applyRatioRestrictionToSelectedCropArea(FileInterface $file): CropVariant + { + if (!$this->selectedRatio) { + return $this; + } + $newVariant = clone $this; + $newArea = $this->cropArea->makeAbsoluteBasedOnFile($file); + $newArea = $newArea->applyRatioRestriction($this->allowedAspectRatios[$this->selectedRatio]); + $newVariant->cropArea = $newArea->makeRelativeBasedOnFile($file); + return $newVariant; + } + /** * @param Ratio[] $ratios * @throws InvalidConfigurationException