javascript等比例压缩图片

<! DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
 
< head >
    
< title > jscript等比例压缩图片 </ title >
    
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312"   />
    
< script  type ="text/javascript"   >
        
function ctlImg(img,w,h)
        
{
            
var img_Width  = w;
            
var img_Height = h;
            
var Owth = img.width;
            
var Ohth = img.height;
            
if(img.width>img_Width && img.height>img_Height){
                
if(Owth/img_Width > Ohth/img_Height){
                    img.width 
= img_Width;
                    img.height 
= parseInt(img_Width/Owth * Ohth);
                }

                
else{
                    img.height 
= img_Height;
                    img.width  
= parseInt(img_Height/Ohth * Owth);
                }

                
return true;
            }

            
if(Owth>img_Width){
                img.width 
= img_Width;
                img.height 
= parseInt(img_Width * (Ohth/Owth));
            }
else if(Ohth>img_Height){
                img.height 
= img_Height;
                img.width  
= parseInt(img_Height * (Owth/Ohth));
            }

            
return true;
        }

</ script >
 
</ head >

 
< body >
    
< h1 > jscript等比例压缩图片 </ h1 >
    
< img  onload ="ctlImg(this,300,200);"  src ="http://www.tu888.cn/tesetuku/minghuabizhi/md61.jpg"   />
    
< img  onload ="ctlImg(this,200,100);"  src ="http://www.tu888.cn/tesetuku/minghuabizhi/md61.jpg"   />
    
< img  onload ="ctlImg(this,100,100);"  src ="http://www.tu888.cn/tesetuku/minghuabizhi/md61.jpg"   />
    
< img  onload ="ctlImg(this,200,50);"  src ="http://www.tu888.cn/tesetuku/minghuabizhi/md61.jpg"   />
 
</ body >
</ html >

你可能感兴趣的:(JavaScript)