﻿function picResize(imgId,maxWidth,maxHeight)
{
    var imgObj = new Image();
    imgObj.src = document.getElementById(imgId).src;

    if(imgObj.width > maxWidth)
    {
        var rw = imgObj.width;
        document.getElementById(imgId).width = maxWidth;
        document.getElementById(imgId).height = maxWidth/rw * document.all(imgId).height;
    }
    
    if(imgObj.height > maxHeight)
    {
        var rh = imgObj.height;
        document.getElementById(imgId).height = maxHeight;
        document.getElementById(imgId).width = maxHeight/rh * document.all(imgId).width;
    }
}

function picResizeV2(sourceImage,maxWidth,maxHeight)
{
    var imgObj = new Image();
    imgObj.src = sourceImage.src;
    
    var oWidth = imgObj.width; //原来长度
    var oHeight = imgObj.height; //原来高度
    var oRate = oWidth / oHeight; //原来长宽比
    var nRate = maxWidth / maxHeight; //后来长宽比
    var wRate = maxWidth / oWidth; //长度比
    var hRate = maxHeight / oHeight; //高度比
    
    var iWidth; //最终长度
    var iHeight; //最终高度
    
    if(oRate >= nRate) //老图较长，优先定长度
    {
        iWidth = maxWidth;
        iHeight = oHeight * wRate;
    }
    else //老图较高，优先定高度
    {
        iHeight = maxHeight;
        iWidth = oWidth * hRate
    }
    
    if(oWidth <= maxWidth && oHeight <= maxHeight) return;
    
    sourceImage.width = iWidth;
    sourceImage.height = iHeight;

}
