限制图片的显示尺寸
图片上传的大小

解决方案 »

  1.   

    判断上传图片大小:
    -------------------------
    <input   id=inp   type="file">   
      <button   onclick="ys()">Test</button>   
      <script>   
      var   img=null;   
      function   ys()   
      {   
      if(img)img.removeNode(true);   
      img=document.createElement("img");   
      img.style.position="absolute";   
      img.style.visibility="hidden";   
      img.attachEvent("onreadystatechange",orsc);   
      img.attachEvent("onerror",oe);   
      document.body.insertAdjacentElement("beforeend",img);   
      img.src=inp.value;   
      }   
      function   oe()   
      {   
      alert("cant   load   img");   
      }   
      function   orsc()   
      {   
      if(img.readyState!="complete")return   false;   
      alert("图片大小:"+img.offsetWidth+"X"+img.offsetHeight);   
      alert("图片尺寸:"+img.fileSize);   
      }   
      </script>
    -----------------------------
    不过这代码的缺点是还使用插件,所以如果把他防在web里面,是不好用的。文件大小一般还是在服务器端来检测的.
    *****************************
    下面的程序是控制图片显示大小,可以把图片等比例缩放:
    -----------------------------
    <script type="text/javascript">
    //  让图片成比例缩小function changeImage(ImgD){
    var image=new Image(); 
    image.src=ImgD.src; 

    if(image.width>185){
    ImgD.width=185;
    ImgD.height=(185*image.height)/image.width;
    image.width=ImgD.width;
    image.height=ImgD.height;
    }

    if(image.height>169){
    ImgD.height=169;
    ImgD.width=(169*image.width)/image.height;
    image.height = ImgD.height;
    image.width = ImgD.width;
    }

    ImgD.alt = image.width + "×" + image.height;
    }
    </script>
    <img src="" width="10" height="70" onload="changeImage(this)"/>
    ------------------------------
    这个程序需要主义的是,在加载大图片的时候,要给图片加载留够充足的时间否则,会出现错误的.