There are multiple ways in which you can convert an image into its Base64 String, one of those ways is using Canvas,
Example:<!html>
<head>
<title>Image to Base64 String Example: Code2care.org</title>
</head>
<body>
<h2>Image Displayed using IMG Tag:</h2>
<img src="code2care-logo.jpg" id="myImg" alt="myImg"/>
<h2>Image Displayed using Base64 String Tag:</h2>
<div id="myImg64" alt="myImg64"></div>
<script type='text/javascript'>
var image = document.getElementById('myImg');
var canvas = document.createElement('canvas');
canvas.getContext('2d').drawImage(image, 0, 0, image.naturalHeight, image.naturalWidth);
var base64ImageString = canvas.toDataURL();
document.getElementById("myImg64").innerHTML = base64ImageString;
</script>
</body>
</html>

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!