兼容IE、新版Chrome、Firefox,实现本地图片等比例缩放预览

以前转载过一篇《兼容IE8、火狐的本地图片预览+等比例缩放》的文章,时至今日在新版的Chrome和Firefox上已经不能用了,原因getAsDataURL方法已经失效。

当然在Chrome、Firefox上可以通过HTML5来实现,今天把它写出来了,见源码:

[html]  view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" >  
  3. <head>       
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />       
  5. <title>本地图片预览</title>       
  6. <style type="text/css">  
  7. #preview{width:100px;height:100px;border:1px solid #000;overflow:hidden;}  
  8. #imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);}  
  9. </style>  
  10. <script type="text/javascript">  
  11. function previewImage(file)  
  12. {  
  13.   var MAXWIDTH  = 100;  
  14.   var MAXHEIGHT = 100;  
  15.   var div = document.getElementById('preview');  
  16.   if (file.files && file.files[0])  
  17.   {  
  18.     div.innerHTML = '<img id=imghead>';  
  19.     var img = document.getElementById('imghead');  
  20.     img.onload = function(){  
  21.       var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);  
  22.       img.width = rect.width;  
  23.       img.height = rect.height;  
  24.       img.style.marginLeft = rect.left+'px';  
  25.       img.style.marginTop = rect.top+'px';  
  26.     }  
  27.     var reader = new FileReader();  
  28.     reader.onload = function(evt){img.src = evt.target.result;}  
  29.     reader.readAsDataURL(file.files[0]);  
  30.   }  
  31.   else  
  32.   {  
  33.     var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';  
  34.     file.select();  
  35.     var src = document.selection.createRange().text;  
  36.     div.innerHTML = '<img id=imghead>';  
  37.     var img = document.getElementById('imghead');  
  38.     img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;  
  39.     var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);  
  40.     status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);  
  41.     div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;margin-left:"+rect.left+"px;"+sFilter+src+"\"'></div>";  
  42.   }  
  43. }  
  44. function clacImgZoomParam( maxWidth, maxHeight, width, height ){  
  45.     var param = {top:0, left:0, width:width, height:height};  
  46.     if( width>maxWidth || height>maxHeight )  
  47.     {  
  48.         rateWidth = width / maxWidth;  
  49.         rateHeight = height / maxHeight;  
  50.           
  51.         if( rateWidth > rateHeight )  
  52.         {  
  53.             param.width =  maxWidth;  
  54.             param.height = Math.round(height / rateWidth);  
  55.         }else  
  56.         {  
  57.             param.width = Math.round(width / rateHeight);  
  58.             param.height = maxHeight;  
  59.         }  
  60.     }  
  61.       
  62.     param.left = Math.round((maxWidth - param.width) / 2);  
  63.     param.top = Math.round((maxHeight - param.height) / 2);  
  64.     return param;  
  65. }  
  66. </script>       
  67. </head>       
  68. <body>  
  69. <div id="preview">  
  70.     <img id="imghead" width=100 height=100 border=0 src='../images/head01_big.jpg'>  
  71. </div>  
  72.     <br/>       
  73.     <input type="file" onchange="previewImage(this)" />       
  74. </body>       
  75. </html>       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值