javascript按缩略比显示图片

        //按缩略比重新计算图片显示大小
        function ReSizeImage(ImgObj, maxWidth, maxHeight) {
            var image = new Image();
            image.onload = function () {
                // console.log('width:' + image.width + ',height:' + image.height);
                var tempWidth;
                var tempHeight;
                if (image.width > 0 && image.height > 0) {
                    if (image.width / image.height >= maxWidth / maxHeight) {
                        if (image.width > maxWidth) {
                            tempWidth = maxWidth;
                            tempHeight = (image.height * maxWidth) / image.width;
                        }
                        else {
                            tempWidth = image.width;
                            tempHeight = image.height;
                        }
                    } else {
                        if (image.height > maxHeight) {
                            tempHeight = maxHeight;
                            tempWidth = (image.width * maxHeight) / image.height;
                        } else {
                            tempWidth = image.width;
                            tempHeight = image.height;
                        }
                    }
                    ImgObj.height = tempHeight;
                    ImgObj.width = tempWidth;
                }
            }
            image.src = ImgObj.src;
        }

 

posted @ 2023-08-15 14:23  潇潇与偕  阅读(7)  评论(0编辑  收藏  举报