[BUGFIX] Eliminate gremlins in image cropper
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / ImageManipulation / CropVariant.php
index f84ec4b..1645148 100644 (file)
@@ -15,6 +15,8 @@ namespace TYPO3\CMS\Core\Imaging\ImageManipulation;
  * The TYPO3 project - inspiring people to share!
  */
 
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Resource\FileInterface;
+
 class CropVariant
 {
     /**
 class CropVariant
 {
     /**
@@ -76,8 +78,12 @@ class CropVariant
         $this->cropArea = $cropArea;
         if ($allowedAspectRatios) {
             $this->setAllowedAspectRatios(...$allowedAspectRatios);
         $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);
         $this->focusArea = $focusArea;
         if ($coverAreas !== null) {
             $this->setCoverAreas(...$coverAreas);
@@ -158,6 +164,22 @@ class CropVariant
         return $this->focusArea;
     }
 
         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
     /**
      * @param Ratio[] $ratios
      * @throws InvalidConfigurationException