function resize()

in modules/js/htdocs/ui.js [1101:1157]


    function resize(img, onload, width, height, oncrop) {
        debug('ui.drawImage.resize');
        var iwidth = img.width;
        var iheight = img.height;
        if (width || height || crop) {
            if (crop) {
                // Crop to fit target canvas size
                var tratio = width / height;
                var oratio = iwidth / iheight;
                if (tratio > oratio) {
                    var scale = width / iwidth;
                    var cwidth = iwidth;
                    var cheight = height / scale;
                    var cut = (iheight - cheight) / 2;
                    // Crop top and bottom edges, then resize
                    return draw(img, onload, 0, cut, cwidth, cut + cheight, 0, 0, width, height);
                } else if (tratio < oratio) {
                    var scale = height / iheight;
                    var cwidth = width / scale;
                    var cheight = iheight;
                    var cut = (iwidth - cwidth) / 2;
                    // Crop left and right edges, then resize
                    return draw(img, onload, cut, 0, cut + cwidth, cheight, 0, 0, width, height);
                } else {
                    // Just resize
                    return draw(img, onload, 0, 0, iwidth, iheight, 0, 0, width, height);
                }
            } else {
                // Resize to make the image fit
                if (iwidth <= width && iheight == height) {
                    return draw(img, onload, 0, 0, iwidth, iheight, 0, 0, iwidth, iheight);
                } else {
                    var tratio = width / height;
                    var oratio = iwidth / iheight;
                    if (tratio > oratio) {
                        // Resize to make height fit
                        var scale = height / iheight;
                        var swidth = iwidth * scale;
                        var sheight = height;
                        return draw(img, onload, 0, 0, iwidth, iheight, 0, 0, swidth, sheight);
                    } else if (tratio < oratio) {
                        // Resize to make width fit
                        var scale = width / iwidth;
                        var swidth = width;
                        var sheight = iheight * scale;
                        return draw(img, onload, 0, 0, iwidth, iheight, 0, 0, swidth, sheight);
                    } else {
                        // Resize to make both width and height fit
                        return draw(img, onload, 0, 0, iwidth, iheight, 0, 0, width, height);
                    }
                }
            }
        } else {
            // Draw image as is
            return draw(img, onload, 0, 0, iwidth, iheight, 0, 0, iwidth, iheight);
        }
    }