本文实例讲述了JS实现按比例缩放图片的方法。分享给大家供大家参考,具体如下:
js版本:
function resizeImage(obj, MaxW, MaxH) { var imageObject = obj; var state = imageObject.readyState; if(state!='complete') { setTimeout("resizeImage("+imageObject+","+MaxW+","+MaxH+")",50); return; } var oldImage = new Image(); oldImage.src = imageObject.src; var dW = oldImage.width; var dH = oldImage.height; if(dW>MaxW || dH>MaxH) { a = dW/MaxW; b = dH/MaxH; if( b>a ) a = b; dW = dW/a; dH = dH/a; } if(dW > 0 && dH > 0) { imageObject.width = dW; imageObject.height = dH; } }
使用很简单:js