跨域图片转base64

<script>
function ImgToBase64(img) {

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL('image/png');

img.removeAttribute("onload");
img.src = dataURL;
canvas = null;

}
</script>

<img  onload="ImgToBase64(this)" crossorigin="anonymous"  src="xxxxxxxx" />

posted @ 2023-05-22 11:31  奇迹之耀  阅读(237)  评论(0编辑  收藏  举报