Set maximum bounds for width and height.
function imgSetMaxWidthHeight(imgid, maxwidth, maxheight) {
var img = document.getElementById(imgid);
if (img) {
var srcwidth = img.width;
var srcheight =img.height;
if (srcwidth>maxwidth) {
newwidth = maxwidth;
newheight = srcheight / srcwidth * maxwidth;
img.width=newwidth;
img.height=newheight;
} else if (srcwidth>maxheight) {
newwidth = srcwidth / srcheight * maxheight;
newheight = maxheight;
img.width=newwidth;
img.height=newheight;
}
img.style.visibility='';
}
}