+ // This will give parameters to scale up if max values are larger than original
+ function scaleToFit($width, $height, $maxWidth=null, $maxHeight=null, $square=true)
+ {
+ 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;
+ }
+ }
+ return array($rw, $rh, $cx, $cy, $cw, $ch);
+ }
+