Created
March 10, 2024 15:30
-
-
Save senthilmpro/10f865d71f93fcfc40b0ca28402d96e8 to your computer and use it in GitHub Desktop.
Image URL to base64 using javascript
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
// snippet from google gemini | |
function convertImageURLToBase64(imageUrl) { | |
fetch(imageUrl) | |
.then(response => response.blob()) | |
.then(blob => { | |
const reader = new FileReader(); | |
reader.readAsDataURL(blob); | |
reader.onloadend = function() { | |
const base64data = reader.result; | |
console.log(base64data); // Output: "data:image/png;base64,iVBORw0KGgoAA..." (Example) | |
} | |
}); | |
} | |
// Example usage: | |
const imageUrl = 'https://www.example.com/image.jpg'; | |
convertImageURLToBase64(imageUrl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment