var flag=false; function setimage(imgd,width,height){ var image=new image(); image.src=imgd.src; if(image.width>0 && image.height>0){ flag=true; if(image.width/image.height>=width/height){ if(image.width>width){ imgd.width=width; imgd.height=(image.height*width)/image.width; }else{ imgd.width=image.width; imgd.height=image.height; } } else{ if(image.height>height){ imgd.height=height; imgd.width=(image.width*height)/image.height; }else{ imgd.width=image.width; imgd.height=image.height; } } } }