jquery将图片转换成base64格式字符串

<script>
    /**
         * 将图片转换为Base64
         */
        function image2Base64(img) {
            var canvas = document.createElement("canvas");
            canvas.width = img.width;
            canvas.height = img.height;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, img.width, img.height);
            var dataURL = canvas.toDataURL("image/png");
            return dataURL;
        }

        $("#tijiao").click(function(){

            var imgurl = $(".z_addImg img").attr("src");  //获取的图片路径


            var img = new Image();

            img.src=imgurl;
         
            var base64 = image2Base64(img);

          	alert(base64);   //弹出base64图片字符串看看,如果很长很长就成功啦
        })
</script>

 

    原文作者:司江龙
    原文地址: https://blog.csdn.net/weixin_40896800/article/details/82997538
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞