js压缩上传图片base64长度
im发送图片,现将图片压缩再上传
1) 调用 FileReader 的 reader.readAsDataURL(img); 方法, 在其onload事件中, 将用户选择的图片读入 Image对象.
2) 在image对象的 onload 事件中, 通过 canvas 的 canvas.getContext('2d') 的 drawImage 方法, 将Image 改变大小绘制到canvas上.
3) 通过 canvas.toDataURL("image/jpeg", 1); 方法, 将图片变成base64字符串, 放到send_image_value
toDataURL
1 | canvas.toDataURL(type, encoderOptions); |
返回值
包含 data URI 的DOMString
type
图片格式,默认为 image/png
encoderOptions
在指定图片格式为 image/jpeg 或
image/webp的情况下,可以从 0 到 1 的区间内选择图片的质量
。如果超出取值范围,将会使用默认值 0.92
。其他参数会被忽略。
html
1 2 3 4 5 | <input id= "sendImage" title= "send a picture" type= "file" accept= "image/*" onchange= "imgChange(this)" > <input type= "hidden" value= "" id= "send_image_value" > <img id= "showLoadingimg" src= "'+CHAT_SITE_URL+'/templates/default/images/loading.gif" style= "position:relative;left:200px;top:200px;z-index:999;display:none;" > |
js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | function imgChange(e){ //检查是否有文件被选中 if (e.files.length != 0) { var file = e.files[0], fileType = file.type, reader = new FileReader(); if (!reader) { e.value = '' ; return ; }; var size = file.size; var max_size = 2*1024*1024; if (size>max_size){ e.value = '' ; $( "#send_alert" ).html( 'file is too large(>2M)' ); return ; } $( "#showLoadingimg" ).show(); reader.onload = function(e) { //读取成功,显示到页面并发送到服务器 e.value = '' ; var org_img = e.target.result; var image_base64 = org_img; if (size>1024*80){ //>80K的 var img = new Image(); img.src = org_img; img.onload=function(){ var scale = 1; if ( this .width > 300 || this .height > 300){ if ( this .width > this .height){ scale = 300 / this .width; } else { scale = 300 / this .height; } } var canvas = document.createElement( "canvas" ), drawer = canvas.getContext( "2d" ); if (scale!=1) { //按最大高度等比缩放 img.width *= scale; img.height *= scale; } canvas.width = img.width; canvas.height = img.width * (img.height / img.width); drawer.drawImage(img, 0, 0, canvas.width, canvas.height); var tmp_code = image_base64 = canvas.toDataURL(fileType); if (tmp_code!= 'data:,' ){ image_base64 = tmp_code; } img_send(image_base64); }; } else { img_send(image_base64); } }; reader.readAsDataURL(file); } } /** *为将图片赋值给消息 **/ function img_send(image_base64){ if (image_base64!= 'data:,' ){ $( "#send_image_value" ).val(image_base64); send_msg(); $( "#showLoadingimg" ).hide(); $( '#sendImage' ).val( "" ); } } |
开始时,toDataURL获取的值是data:,
$("#send_image_value").val(image_base64);
send_msg();
这两句写在
reader.onload方法的最下面这就导致图片并没有压缩
因为img.onload还没执行完
发送的还是原来的图片
在调整后就可以实现图片的压缩了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)