Created
December 6, 2018 05:07
-
-
Save SeanMcP/4d65b8782d249b3c66372786b893d913 to your computer and use it in GitHub Desktop.
Convert an SVG to Base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
</head> | |
<body> | |
<img id="image" /> | |
<?xml version="1.0" standalone="no"?> | |
<svg width="200" height="250" version="1.1" xmlns="http://www.w3.org/2000/svg"> | |
<rect x="10" y="10" width="30" height="30" stroke="black" fill="transparent" stroke-width="5" /> | |
</svg> | |
</body> | |
<script type="text/javascript"> | |
var serializedString = new XMLSerializer().serializeToString(document.querySelector('svg')); | |
var encodedData = window.btoa(serializedString); | |
document.getElementById('image').src = `data:image/svg+xml;base64,${encodedData}`; | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The plan is to use this to programmatically create images for Twitter cards on a Gatsby site