here2share

$ JS Convert Base64 to Image

Dec 16th, 2022
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2.  
  3. // Read the image file into a buffer
  4. const imageBuffer = fs.readFileSync('image.png');
  5.  
  6. // Convert the buffer to a base64 string
  7. const imageBase64 = imageBuffer.toString('base64');
  8.  
  9. // Create the data URL for the image
  10. const dataURL = `data:image/png;base64,${imageBase64}`;
  11.  
  12. // Set the src attribute of an image element to the data URL
  13. const imageElement = document.getElementById('my-image');
  14. imageElement.src = dataURL;
  15.  
  16. // https://www.base64-image.de
Add Comment
Please, Sign In to add comment