// 將圖片轉換為Base64
imageUrlToBase64(img) {
// 一定要設置為let,不然圖片不顯示
let image = new Image();
// 解決跨域問題
image.setAttribute('crossOrigin', 'anonymous');
let imageUrl = img;
image.src = imageUrl;
// image.onload為異步加載
image.onload = () => {
this.getImage(image);
};
},
getImage(image) {
let canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
let context = canvas.getContext('2d');
context.drawImage(image, 0, 0, image.width, image.height);
let quality = 0.8;
// 這里的dataurl就是base64類型
let dataURL = canvas.toDataURL('image/jpeg', quality);
},