JS按比例缩放图片

//来自:新浪网-新闻中心(http://news.sina.com.cn/)
//调用:<img src="" onload="javascript:DrawImage(this)">
function DrawImage(ImgD){
    var image=new Image();
    var iwidth = 120; //默认宽
    var iheight = 120; //默认高
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
        if(image.width/image.height>= iwidth/iheight){
            if(image.width>iwidth){
                ImgD.width=iwidth;
                ImgD.height=(image.height*iwidth)/image.width;
            }else{
                ImgD.width=image.width;
                ImgD.height=image.height;
            }
        }else{
            if(image.height>iheight){
                ImgD.height=iheight;
                ImgD.width=(image.width*iheight)/image.height;
            }else{
                ImgD.width=image.width;
                ImgD.height=image.height;
            }
        }
    }
}

//来自:QZone(http://270250392.qzone.qq.com/)
function adjustSize(obj,w,h,openWindows){
    obj.onload=null;
    if(w<=0||h<=0)return ;
    var w0=obj.width,h0=obj.height,r=false;
    if(w0<1){
        var i=new Image();
        i.src=obj.src;
        w0=i.width;
        h0=i.height;
    }
    if((w0/h0)>(w/h)){
        if(w0>w){
            setTimeout(function (o,i){    return function(){o.style.width=i}}(obj,w),0);
            w0=w;
            r=true;
        }
    }else {
        if(h0>h){
            setTimeout(function (o,i){return function (){o.style.height=i}}(obj,h),0);
            h0=h;
            r=true;
        }
    }
    if(openWindows&&r){
        obj.style.cursor="pointer";
        obj.title="点击预览原图";
        obj.onclick=function (){window.open(obj.src);}
    }
    if(!!obj.transImg&&Browser.isIE&&!Browser.isIE7){
        obj.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,src="+obj.src+",sizingmethod=scale);";
        obj.style.height=h0;
        obj.style.width=w0;
        obj.src="/ac/qzone_v4/b.gif";
    }
}


评论: 0 | 引用: 0 | 查看次数: 4016
发表评论
登录后再发表评论!