Fix calcuation of canvas size
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Mar 2019 16:23:27 +0000 (17:23 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Mar 2019 16:23:27 +0000 (17:23 +0100)
The previous version failed if an image should be scaled to a square and
the original is higher than wide.

wcfsetup/install/files/js/WoltLabSuite/Core/Image/Resizer.js

index f427f6159e4bc5040868127e7d399e821fd179bb..1c06c484924a34ac84574c9e234aa0aa35018e1a 100644 (file)
@@ -174,14 +174,9 @@ define([
                        }
                        
                        // Keep image ratio
-                       if (newWidth >= newHeight) {
-                               canvas.width = newWidth;
-                               canvas.height = newWidth * (image.height / image.width);
-                       }
-                       else {
-                               canvas.width = newHeight * (image.width / image.height);
-                               canvas.height = newHeight;
-                       }
+                       var ratio = Math.min(newWidth / image.width, newHeight / image.height);
+                       canvas.width = Math.floor(image.width * ratio);
+                       canvas.height = Math.floor(image.height * ratio);
                        
                        // Map to Pica's quality
                        var resizeQuality = 1;