java:
private static int[] getImageWidthAndHeight(int orgW , int orgH, int avW, int avH) {
int width=0;
int height = 0;
if(orgW>0&&orgH>0){
if(orgW/orgH>=avW/avH){
if(orgW>avW){
width = avW;
height = (orgH*avW)/orgW;
}else{
width = orgW;
height = orgH;
}
System.out.println("++Widht:"+width+" Height"+height);
}else{
if(orgH>avH){
height = avH;
width = (orgW*avH)/orgH;
}else{
width = orgW;
height = orgH;
}
System.out.println("++Widht:"+width+" Height"+height);
}
}
int[] arr = new int[2];
arr[0] = width;
arr[1] = height;
return arr;
}
javascript:
function DrawImage(ImgD,iwidth,iheight,altmsg){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=altmsg+" "+image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=altmsg+" "+image.width+"×"+image.height;
}
}
}
以上代码来自网络,挺实用的