http://wangking717.javaeye.com/blog/769021
function DrawImg(imgId,boxWidth,boxHeight)
{
var imgWidth=$("#"+imgId).width();
var imgHeight=$("#"+imgId).height();
//比较imgBox的长宽比与img的长宽比大小
if((boxWidth/boxHeight)>=(imgWidth/imgHeight))
{
//重新设置img的width和height
$("#"+imgId).width((boxHeight*imgWidth)/imgHeight);
$("#"+imgId).height(boxHeight);
//让图片居中显示
var margin=(boxWidth-$("#"+imgId).width())/2;
$("#"+imgId).css("margin-left",margin);
}
else
{
//重新设置img的width和height
$("#"+imgId).width(boxWidth);
$("#"+imgId).height((boxWidth*imgHeight)/imgWidth);
//让图片居中显示
var margin=(boxHeight-$("#"+imgId).height())/2;
$("#"+imgId).css("margin-top",margin);
}
}
<img id="'+data[m]['id']+'" src="'+ dimg+'" border="0" onload="DrawImg(this.id,100,100);" />