网络图片转base64格式
网络图片转base64格式
// 定义网络图转base64的方法 function getBase64(url, ext, callback) { var canvas = document.createElement("canvas"); //创建canvas DOM元素 var ctx = canvas.getContext("2d"); var img = new Image; img.crossOrigin = 'Anonymous'; img.src = url; img.onload = function () { canvas.width = img.width; //指定画板的宽度,自定义 canvas.height = img.height; //指定画板的高度,自定义 ctx.drawImage(img, 0, 0, img.width, img.height); //参数可自定义 var dataURL = canvas.toDataURL("image/" + ext); callback.call(this, dataURL); //回掉函数获取Base64编码 canvas = null; }; } var imgUrl = 'http://124.205.255.18:8003/sstbk/xpkpsmsz/getEwm_open?uuid=2188d0406784467a97d6d12ef6851fdc&je=4.00&isClient=true'; getBase64(imgUrl, 'png', function(base64){ // 给原生img对象的src属性赋值 img.src = base64; })