ResizeImage

<script>
function ResizeImage(inputImg, maxWidth, maxHeight){
 var tempImg=new Image();
 tempImg.src=inputImg.src;
 var targetWidth=tempImg.width;
 var targetHeight=tempImg.height;

 if(tempImg.width>0 && tempImg.height>0){
  if(tempImg.width/tempImg.height >= maxWidth/maxHeight){
   if(tempImg.width>maxWidth){  
    targetWidth=maxWidth;
    targetHeight=(tempImg.height/tempImg.width)*targetWidth;
   }
  }
  else{
   if(tempImg.height>maxHeight){
    targetHeight=maxHeight;
    targetWidth=(tempImg.width/tempImg.height)*targetHeight;
   }
  }
  inputImg.width=targetWidth;
  inputImg.height=targetHeight;
 }

function ResizeImage2(inputImg, maxWidth, maxHeight){
 var tempImg=new Image();
 tempImg.src=inputImg.src;
 var sx=tempImg.width/maxWidth;
 var sy=tempImg.height/maxHeight;
 var scale=Math.max(sx,sy);
 if(scale>1)
 {
  inputImg.width=tempImg.width/scale
  inputImg.height=tempImg.height/scale
 }
}
</script>
<img src="1.jpg" onload="ResizeImage2(this, 100,200)">

 

http://www.shrinkpictures.com/tutorials/

你可能感兴趣的:(ResizeImage)