图片大小自动缩略 显示

function resizePic_temp(o, Mw, Mh, need_margin) {
    var _Mw = Mw || 120;
    var _Mh = Mh || 120;
    var need_resize = false;
    var _image = new Image();
    _image.src = o.src;
    function getRightWH(Rw, Rh, Mw, Mh) {
        var index = 0,
                _Rw = Rw,
                _Rh = Rh;
        if (Rw > Mw) index += 1;
        if (Rh > Mh) index += 2;
        switch (index) {
            case 1:
                _Rw = Mw;
                _Rw = Rh * Mw / Rw;
            case 2:
                _Rh = Mh;
                _Rw = Rw * Mh / Rh;
            case 3:
                _Rh = (Rh / Mh > Rw / Mw) ? Mh : Rh * Mw / Rw;
                _Rw = (Rh / Mh > Rw / Mw) ? Rw * Mh / Rh : Mw
        }
        if (index != 0) {
            need_resize = true;
        }
        return [_Rw, _Rh];
    }
    var wh = getRightWH(_image.width, _image.height, _Mw, _Mh);
    o.style.width = wh[0] + 'px';
    o.style.height = wh[1] + 'px';
    o.setAttribute("width", wh[0]);
    o.setAttribute("height", wh[1]);
    o.style.visibility = 'visible';
    if (need_margin == true) {
        o.style.marginTop = (Mh - parseInt(wh[1])) / 2 + 'px'
    }
    _image = null;
    return need_resize;
}

 

posted @ 2013-03-05 17:30  南潇湘  阅读(346)  评论(0编辑  收藏  举报