JavaScript上传图片及时预览

/*******************************正面图片上传预览开始****************************/
        function previewImage(file)   //img便签调用该函数 onchange="previewImage(this),onchange 事件会在域的内容改变时发生。
        {
          var MAXWIDTH  = 100;   //宽度
          var MAXHEIGHT = 100;   //高度
          var div = document.getElementById('preview');   //获取div控件id为preview的元素 同jQuery “var div=$("#preview");”
          if (file.files && file.files[0])
          {
              //div.innerHTML ='<img id=imghead1>';//innerHTML:重新设置div内的html代码
              var img = document.getElementById('imghead1');   //获取img控件id为imghead1的元素 同jQuery “var div=$("#imghead1");”
              img.onload = function(){ //img控件点击事件,同于控件调用非匿名函数
              //裁剪图片尺寸
           var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight); //img.offsetWidth、img.offsetHeight img控件的高度和宽度
                img.width  =  rect.width;
                img.height =  rect.height;
//              img.style.marginLeft = rect.left+'px';
                img.style.marginTop = rect.top+'px';
              }
              var reader = new FileReader(); //读取本地图片文件并显示
              reader.onload = function(evt){img.src = evt.target.result;}//获取到成功读取的文件内容,并以插入一个img节点的方式显示选中的图片。
              reader.readAsDataURL(file.files[0]);//读取选中的图像文件
          }
          else //兼容IE
          {
            var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
            file.select();
            var src = document.selection.createRange().text;
           
            div.innerHTML = '<img id=imghead1>';
            var img = document.getElementById('imghead1');
            img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
            status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
            div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"'></div>";
          }
        }
          //裁剪图片尺寸
        function clacImgZoomParam( maxWidth, maxHeight, width, height ){
            var param = {top:0, left:0, width:width, height:height};
            if( width>maxWidth || height>maxHeight )
            {
                rateWidth = width / maxWidth;
                rateHeight = height / maxHeight;
                
                if( rateWidth > rateHeight )
                {
                    param.width =  maxWidth;
                    param.height = Math.round(height / rateWidth);
                }else
                {
                    param.width = Math.round(width / rateHeight);
                    param.height = maxHeight;
                }
            }
            
            param.left = Math.round((maxWidth - param.width) / 2);
            param.top = Math.round((maxHeight - param.height) / 2);
            
            return param;
        }
        /*******************************正面图片上传预览结束****************************/

源码云盘地址:  http://pan.baidu.com/s/1nuPx1xf    提取码: bnyg

<div class="pic" id="preview"  >
                   <img class="sfimg" runat="server" id="imghead1" src="" />
                   <input type="hidden" runat="server" id="pictruePet" value="0" />
                   </div>

上一篇:Photoshop图像合成教程:艺术化普通照片


下一篇:android viewpager左右循环滑动