- $newWidth = (int) $_GET['w'];
- //if (isset($_GET['h'])) {
- // $newHeight = (int) $_GET['h'];
- //} else {
- $newHeight = intval($newWidth / imagesx($img) * imagesy($img));
- //}
+ if (isset($_GET['w']) && isset($_GET['h'])) {
+ //max. bounding box respecting aspect ratio
+ $newW = (int) $_GET['w'];
+ $newH = (int) $_GET['h'];
+
+ $rW = $oldW / $newW;
+ $rH = $oldH / $newH;
+ if ($rW > $rH) {
+ $newOldW = intval($oldW / $rW * $rH);
+ $oldX = $oldW / 2 - $newOldW / 2;
+ $oldW = $newOldW;
+ } else {
+ $newOldH = intval($oldH / $rH * $rW);
+ $oldY = $oldH / 2 - $newOldH / 2;
+ $oldH = $newOldH;
+ }
+ } else if (isset($_GET['w'])) {
+ $newW = (int) $_GET['w'];
+ $newH = intval($newW / $oldW * $oldH);
+ } else {
+ $newH = (int) $_GET['h'];
+ $newW = intval($newH / $oldH * $oldW);
+ }