[BUGFIX] Eliminate gremlins in image cropper
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / ImageManipulation / Area.php
index e74af9b..54388dd 100644 (file)
@@ -136,6 +136,27 @@ class Area
         );
     }
 
         );
     }
 
+    /**
+     * @param Ratio $ratio
+     * @return Area
+     */
+    public function applyRatioRestriction(Ratio $ratio): Area
+    {
+        if ($ratio->isFree()) {
+            return $this;
+        }
+        $expectedRatio = $ratio->getRatioValue();
+        $newArea = clone $this;
+        if ($newArea->height * $expectedRatio > $newArea->width) {
+            $newArea->height = $newArea->width / $expectedRatio;
+            $newArea->y += ($this->height - $newArea->height) / 2;
+        } else {
+            $newArea->width = $newArea->height * $expectedRatio;
+            $newArea->x += ($this->width - $newArea->width) / 2;
+        }
+        return $newArea;
+    }
+
     /**
      * @return bool
      */
     /**
      * @return bool
      */