项目场景:
在上传图片前,对图片进行压缩处理
解决方案:
1.自己写压缩方法(不推荐)
(1)创建公共类封装压缩方法
compressImg.js:
/* 图片压缩方法-canvas压缩 */
function compressUpload(image, file) {
let compressData
let canvas = document.createElement('canvas')
let ctx = canvas.getContext('2d')
let {
width } = image, {
height } = image
let fileSize = parseFloat(parseInt(file.size)/1024).toFixed(2);
canvas. width = width
canvas.height = height
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(image, 0, 0, width, height)
if(fileSize<=500){
compressData = canvas.toDataURL(file