图片的等比例缩放

项目需求:固定长宽的容器,要求放入合适大小的图片(图片大的等比例缩小;图片小的等比例放大)

下列示例代码:

//<div><img src="http://dsb.66wz.com/images/2007-11/09/119454094354649494160708476276.jpg" onload="autoResizeImage(200,100,this)"></div>

function autoResizeImage(max_Width,max_Height,imgObj) {
    var image = new Image();
    image.src = imgObj.src;
    var cur_w = image.width;
    var cur_h = image.height;
    var radio = 1;
    var w_radio = max_Width/cur_w;
    var h_radio = max_Height/cur_h;
    if(max_Width == 0 || max_Height == 0) {
          radio = 1;
    }else {
          radio = w_radio <=h_radio ? w_radio :h_radio;
    }
    imgObj.width =  cur_w * radio;
    imgObj.height = cur_h * radio;
    
}
posted @ 2012-08-10 18:06  赤脚大仙~  阅读(434)  评论(0编辑  收藏  举报
View Code