Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created March 10, 2024 15:30
Show Gist options
  • Save senthilmpro/10f865d71f93fcfc40b0ca28402d96e8 to your computer and use it in GitHub Desktop.
Save senthilmpro/10f865d71f93fcfc40b0ca28402d96e8 to your computer and use it in GitHub Desktop.
Image URL to base64 using javascript
// 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