天天看點

圖檔等比例縮略圖js

有關圖檔縮略圖變形的問題,成比例縮放方法!

//名稱:Drawlmage

//功能:完成圖形的成比例縮放

//日期:2010-3-20

//人員:RedWolf

//調用的方法:<img src="你的圖檔位址" οnlοad='javascript:DrawImage(this,最大寬度,最大高度);'>

<script language="javascript">

var flag=false;

function DrawImage(ImgD,iwidth,iheight){

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=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=image.width+"×"+image.height;

}

}

}

</script>

繼續閱讀