- $aspect = $maxWidth / $maxHeight;
- $w1 = $maxWidth;
- $h1 = intval($height * $maxWidth / $width);
- if ($h1 > $maxHeight) {
- $w2 = intval($width * $maxHeight / $height);
- $h2 = $maxHeight;
- return array($w2, $h2);
+ if ($width <= 0 || $height <= 0
+ || ($maxWidth !== null && $maxWidth <= 0)
+ || ($maxHeight !== null && $maxHeight <= 0)) {
+ throw new ServerException('Bad scaleToFit parameters for MediaFile');
+ } elseif ($maxWidth === null) {
+ // maxWidth must be a positive number
+ throw new ServerException('MediaFile::scaleToFit maxWidth is null');
+ } elseif ($square || $maxHeight === null) {
+ // if square thumb ratio or if maxHeight is null,
+ // we set maxHeight to equal maxWidth
+ $maxHeight = $maxWidth;
+ $square = true;
+ }
+
+ // cropping data
+ $cx = 0; // crop x
+ $cy = 0; // crop y
+ $cw = null; // crop area width
+ $ch = null; // crop area height
+
+ if ($square) {
+ // resulting width and height
+ $rw = $maxWidth;
+ $rh = $maxHeight;
+
+ // minSide will determine the smallest image size
+ // and crop-values are determined from this
+ $minSide = $width > $height ? $height : $width;
+ $cx = $width / 2 - $minSide / 2;
+ $cy = $height / 2 - $minSide / 2;
+ $cw = $minSide;
+ $ch = $minSide;
+ } else {
+ // resulting sizes
+ $rw = $maxWidth;
+ $rh = floor($height * $maxWidth / $width);
+
+ if ($rh > $maxHeight) {
+ $rw = floor($width * $maxHeight / $height);
+ $rh = $maxHeight;
+ }