图片压缩小方法
// canvas重绘图片
const resizeImage = (base64, scale, cb) => {
const img = new Image();
img.onload = () => {
const {
width,
height
} = img;
const reWidth = width * scale,
reHeight = height * scale;
const canvas = document.createElement(\’canvas\’);
canvas.width = reWidth;
canvas.height = reHeight;
const contxt = canvas.getContext(\’2d\’);
contxt.drawImage(img, 0, 0, reWidth, reHeight);
cb(contxt.canvas.toDataURL());
}
img.src = base64;
}
// blob转为base64
const blobToDataURL = (blob, callback) => {
var a = new FileReader();
a.onload = function (e) { callback(e.target.result); }
a.readAsDataURL(blob);
}
// base64转为blob
const convertBase64UrlToBlob = (urlData) => {
var bytes = window.atob(urlData.split(\’,\’)[1]); //去掉url的头,并转换为byte
//处理异常,将ascii码小于0的转换为大于0
var ab = new ArrayBuffer(bytes.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < bytes.length; i++) {
ia[i] = bytes.charCodeAt(i);
}
return new Blob([ab], { type: \’image/png\’ });
}
版权声明:本文为coco-bear原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。