现在的位置: 首页 > 综合 > 正文

按比例显示缩略图,防止变形

2013年08月08日 ⁄ 综合 ⁄ 共 1742字 ⁄ 字号 评论关闭
 


<html>
<head>
<script language='javascript'>
function showImg1(smallWidth, smallHeight)
{
    document.all.image1.src 
= document.all.file1.value;
    
    var bigWidth 
= document.all.image1.width;
    var bigHeight 
= document.all.image1.height;
    var div1 
= bigWidth/bigHeight;
    var div2 
= bigHeight/bigWidth;

    var n1 
= 0;
    var n2 
= 0;
    
if(bigWidth>smallWidth)
    {
        n1 
= bigWidth/smallWidth;
    }
    
else
    {
        smallWidth 
= bigWidth;
    }
    
if(bigHeight>smallHeight)
    {
        n2 
= bigHeight/smallHeight;
    }
    
else
    {
        smallHeight 
= bigHeight;
    }
    
if(n1!=0 || n2!=0)
    {
        
if(n1>n2)
        {
            smallHeight 
= smallWidth*div2;
        }
        
else
        {
            smallWidth 
= smallHeight*div1;
        }
    }

    document.all.image2.width 
= smallWidth;
    document.all.image2.height 
= smallHeight;
    document.all.image2.src 
= document.all.file1.value;
}
</script>
</head>
<body>

<div id="div1">
      
<img id="image1">
</div>

<table border=1><tr><td width=100 height=100 align="center" valign="center">
    
<img id="image2" align="center">
</td></tr></table>

<form method="post" action="?action=get" action="">
<input type="file" id="file1" onpropertychange="showImg1(100, 100);"><br/>
</form>

</body>
</html>


<html>
<head>
<script language='javascript'>

function setImgSize(obj, defaultWidth, defaultHeight)
{
    var width 
= obj.width;
    var height 
= obj.height;
    
if(width>defaultWidth || height>defaultHeight)
    {
        
if(width>height)
        {
            obj.style.width 
= defaultWidth;
        }
        
else
        {
            obj.style.height 
= defaultHeight;
        }
    }
}

</script>
</head>
<body>

<img src="images/1.gif" border=0 onload="javascript:setImgSize(this, 100, 100)"><br/><br/>

</body>
</html>

 

抱歉!评论已关闭.