码迷,mamicode.com
首页 > 其他好文 > 详细

图片转base64并压缩大小

时间:2020-01-04 16:48:47      阅读:1012      评论:0      收藏:0      [点我收藏+]

标签:图片   大小   hat   att   image   this   ||   nbsp   etc   

<input id="file" type="file"  capture="microphone" accept="image/*"> 
 1 $("#file").change(function(){
 2 var m_this = this;
 3       cutImageBase64(m_this,null,400,0.8);  
 4 })
 5 function cutImageBase64(m_this,id,wid,quality) {
 6  
 7     var file = m_this.files[0];
 8     var URL = window.URL || window.webkitURL;
 9     var blob = URL.createObjectURL(file);
10     var base64;
11 var img = new Image();
12     img.src = blob;
13     img.onload = function() {
14         var that = this;
15         //生成比例
16         var w = that.width,
17             h = that.height,
18             scale = w / h;
19             w = wid || w;
20             h = w / scale;
21 //生成canvas
22         var canvas = document.createElement(‘canvas‘);
23         var ctx = canvas.getContext(‘2d‘);
24         $(canvas).attr({
25             width: w,
26             height: h
27         });
28         ctx.drawImage(that, 0, 0, w, h);
29         // 生成base64            
30         base64 = canvas.toDataURL(‘image/jpeg‘, quality || 0.8);
31 console.log(base64)
32     };
33 }

图片转base64并压缩大小

标签:图片   大小   hat   att   image   this   ||   nbsp   etc   

原文地址:https://www.cnblogs.com/chenpeikun/p/12149269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!