设置预览图片自适应(保持原比例)
function resizeImage(image, max) { if (Math.max(image.width, image.height) > max) { if (image.width > image.height) image.width = max; else image.height = max; } return true; }
<img onload="var max=64;if(Math.max(this.width,this.height)>max){if(this.width>this.height)this.width=max;else this.height=max;}" src="/test.jpg" />
或者
<img onload="return resizeImage(this,64)" src="/test.jpg" />