<pre name="code" class="html"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<img width="100" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAHgCAYAAADUj..." />
<span id="txtBaseCode" style="word-wrap: break-word;"></span>
<script>
var txtBaseCode = document.getElementById("txtBaseCode");
function convertImgToBase64(url, callback, outputFormat) {
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
img = new Image;
img.crossOrigin = 'Anonymous';
img.onload = function() {
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL(outputFormat || 'image/png');
callback.call(this, dataURL);
canvas = null;
};
img.src = url;
}
convertImgToBase64('img/splash1.png', function(base64Img) {
txtBaseCode.innerHTML = base64Img;
});
</script>
</body>
</html>
javascript 图片解析Base64字符串
最新推荐文章于 2024-07-25 10:57:43 发布