canvas图片压缩--案例
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Document</title> </head> <body> <input type= "file" id= "upload" ></input> <script> const ACCEPT = [ 'image/jpg' , 'image/png' , 'image/jpeg' ]; const MAXSIZE = 3 * 1024 * 1024; const MAXSIZE_STR = '3MB' ; // 读取图片方法 function converImageToBase64(file, callback) { let reader = new FileReader(); reader.addEventListener( 'load' , function (e) { const base64Image = e.target.result; // 读取图片 callback && callback(base64Image) reader = null ; // 回收 }) reader.readAsDataURL(file); } // 压缩方法1 function compress(base64Image, callback) { console.log(base64Image); let maxW = 1024; let maxH = 1024; const image = new Image(); // 渲染图片 - 获取图片的宽高- 压缩宽高 - 压缩分辨率 image.addEventListener( 'load' , function (e) { let ratio; // 图片的压缩比 let needCompress = false ; // 是否需要压缩 if (maxW < image.naturalWidth) { needCompress = true ; ratio = image.naturalWidth / maxW; console.log( '压缩比例' , ratio); maxH = image.naturalHeight / ratio; // 获得高度压缩值 } // 经过处理后,实际图片的尺寸为1024 * 640 ; if (maxH < image.naturalHeight) { needCompress = true ; ratio = image.naturalHeight / maxH; maxW = image.naturalWidth / ratio; } if (!needCompress) { maxW = image.naturalWidth; maxH = image.naturalHeight; } // 如果不需要压缩,需要获取图片的实际尺寸 const canvas = document.createElement( 'canvas' ); canvas.setAttribute( 'id' , '__compress__' ); canvas.width = maxW; canvas.height = maxH; canvas.style.visibility = 'hidden' ; // visible可显示 document.body.appendChild(canvas); const ctx = canvas.getContext( '2d' ); ctx.clearRect(0, 0, maxW, maxH); ctx.drawImage(image, 0, 0, maxW, maxH); // 质量压缩-start const compressImage = canvas.toDataURL( 'image/jpeg' , 0.8); // 参数二:压缩比例0-1 callback && callback(compressImage); //挂载到页面-一下;对比 const _image = new Image(); _image.src = compressImage; document.body.append(_image); canvas.remove(); console.log( '压缩比:' + image.src.length / _image.src.length); }); image.src = base64Image; document.body.appendChild(image); // 挂在到页面 } // 压缩回调方法 function uploadToServer(compressImage) { console.log( 'upload to server .... ' , compressImage); } const upload = document.getElementById( 'upload' ); upload.addEventListener( 'change' , function (e) { const [file] = e.target.files; console.log(e.target.files); if (!file) { return ; } const { type: fileType, size: fileSize } = file; // if(ACCEPT.indexOf(fileType) < 0) { if (!ACCEPT.includes(fileType)) { // alert('不支持[' + fileType + ']文件类型!'); alert(`不支持[${fileType}]文件类型!`); upload.value = '' ; return ; } if (fileSize > MAXSIZE) { alert(`文件超出${MAXSIZE_STR}`); upload.value = '' ; return ; } // 调用方法--开始压缩1 converImageToBase64(file, (base64Image) => compress(base64Image, uploadToServer)) }) </script> </body> </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?