img的url转base64

        function getBase64Image(img) {
            var canvas = document.createElement("canvas");
            $("canavs").hide();
            canvas.width = img.width;
            canvas.height = img.height;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, img.width, img.height);
            var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
            var dataURL = canvas.toDataURL("image/"+ext);
            return dataURL;
        }
        var imgLink = "图片地址";
        var tempImage = new Image();
        tempImage.src = imgLink;
        tempImage.crossOrigin = "*";
        tempImage.onload = function(){
            var base64 = getBase64Image(tempImage);
            console.log(base64);
            $("#resultImg").attr("src",base64);
        }

 

你可能感兴趣的:(js,技术,canvas)