惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
J
Java Code Geeks
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
腾讯CDC
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
V
V2EX
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
Jina AI
Jina AI
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
B
Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium

博客园 - Nuke'Blog

Linux下架设rsync服务器 Web网站压力及性能测试工具WebBench使用指南 url 中文转码 Linux Crontab 定时任务 命令详解 404、500、502等HTTP状态码介绍 谈谈IT公司常见的离职潮 网易组图新闻显示效果,代码刚抠下来整理好分享大家 土鳖网站是怎样炼成的 SQL语法操作全集 在 Apache 上实现 META 切换 请执行sp_addlinkedserver 将该服务器添加到sysserver解决办法 C#.Net的WinForm中使用水晶报表 iBATIS的like 经典,句句噎死人! 表单元素与提示文字无法对齐的问题(input,checkbox文字对齐) 关于网络的IP地址 使用正则表达式过滤html标签,属性,样式表,挂马脚本 - Nuke'Blog - 博客园 linux 解压命令总结 JVM内存模型以及垃圾回收 Siege for linux
前台JS限制上传图片质量大小和尺寸 - Nuke'Blog - 博客园
Nuke'Blog · 2010-12-09 · via 博客园 - Nuke'Blog

前台JS限制上传图片质量大小和尺寸!(转)<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=??????">
<script language="javascript">
<!--
function imgSel()
{
var img = new Image();
img.src = document.imageForm.file.value;
document.imageForm.width.value = img.width;
document.imageForm.height.value = img.height;
document.imageForm.size.value = img.fileSize;
document.images['image'].src = img.src;
}
-->
</script>
</head>
<body>
<form name="imageForm">
宽: <input name="width" type="text" size="6"> 高: <input name="height" type="text" size="6"> 大小: <input name="size" type="text" size="6"><br>
<input name="file" type="file" onChange="imgSel()"><br>
<img src="" name="image">
</form>
</body>
</html>

用CSS实现的方法:

    把一副大图片按比例缩小或者放大到某个尺寸,对于标准浏览器(如Firefox),或者最新都IE7浏览器,直接使用max-width,max-height;或者min-width,min-height的CSS属性即可。如:
img{max-width:100px;max-height:100px;}
img{min-width:100px;min-height:100px;}

对于IE6及其以下版本的浏览器,则可以利用其支持的expression属性,在css code中嵌入javascript code来实现图片的缩放
.thumbImage {max-width: 100px;max-height: 100px;} /* for Firefox & IE7 */
* html .thumbImage { /* for IE6 */
width: expression(this.width > 100 && this.width > this.height ? 100 : auto);
height: expression(this.height > 100 ? 100 : auto);
}

由于把图片放大,可能存在图片锯齿化的问题,一般用在图片缩小的情况是较多的。

-----------------------------------------------------------------------
有时候图片太大,会破环网页整齐的布局。这时可以用css来强制按比例压缩图片的高度或宽度
css代码如下:
img,a img{
border:0; 
margin:0; 
padding:0;
max-width:590px;
width:expression(this.width>590?"590px":this.width);
max-height:590px;
height:expression(this.height>590?"590px":this.height);
}
这样当图片的高度或宽度若超过590px,将会按比例压缩成590px,如果不超过则按原大小显示。

用JavaScript实现的方法:

用JavaScript实现网页图片等比例缩放

  如何让网页中的图片等比例缩放呢,我参考了一些代码并自己写了个图片缩放的脚本,可以点击放大,同时用鼠标滑轮自由缩放,希望提出不同意见。

  首先看看resizeimg函数的源代码:
function resizeimg(ImgD,iwidth,iheight) {
     var image=new Image();
     image.src=ImgD.src;
     if(image.width>0 && image.height>0){
        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;
            }
     ImgD.style.cursor= "pointer"; //改变鼠标指针
     ImgD.onclick = function() { window.open(this.src);} //点击打开大图片
    if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判断浏览器,如果是IE
      ImgD.title = "请使用鼠标滚轮缩放图片,点击图片可在新窗口打开";
      ImgD.onmousewheel = function img_zoom() //滚轮缩放
      {
          var zoom = parseInt(this.style.zoom, 10) || 100;
          zoom += event.wheelDelta / 12;
          if (zoom> 0) this.style.zoom = zoom + "%";
          return false;
      }
     } else { //如果不是IE
            ImgD.title = "点击图片可在新窗口打开";
         }
    }
}

  在需要实现等比缩放的图片上加上onload语句,图片装载时初始化大小。
  具体实现代码如下:
   <img name="" src="" onload="javascript:resizeimg(this,100,200)">

  赶快行动,把这个特效加入到你的网页图片中去。
----------------------------------------------------------------------------
<script   language="JavaScript">  
  {  
  var   flag=false;  
  function   DrawImage(ImgD){  
        var   image=new   Image();  
        image.src=ImgD.src;  
        if(image.width>0   &&   image.height>0){  
          flag=true;  
          if(image.width/image.height>=0){  
            if(image.width>520){      
            ImgD.width=520;  
            ImgD.height=(image.height*520)/image.width;  
            }else{  
            ImgD.width=image.width;      
            ImgD.height=image.height;  
            }  
            //ImgD.alt=image.width+"×"+image.height;
   ImgD.alt="点击此处在新窗口中打开!"
            }  
          else{  
            if(image.height>235){      
            ImgD.height=235;  
            ImgD.width=(image.width*235)/image.height;            
            }else{  
            ImgD.width=image.width;      
            ImgD.height=image.height;  
            }  
            //ImgD.alt=image.width+"×"+image.height;  
   ImgD.alt="点击此处在新窗口中打开!"
            }  
          }  
  }    
  }  
  </script>

例:<img src="2.gif" border="0" class="borderpic" onload="javascript:DrawImage(this);"></a>