<script>
function trans_base64(url,) {
const img = url; //imgurl 就是你的图片路径

function getBase64Image(img) {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
const ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase();
const dataURL = canvas.toDataURL('image/' + ext);
return dataURL;
}

const image = new Image();
image.setAttribute('crossOrigin', 'anonymous');
image.src = img;
image.onload = function () {
var base64 = getBase64Image(image);
fn(base64);
};
}
trans_base64('./12.jpg', function (re) {
console.log(re)
})
</script>