To convert a base64 image URL to a Buffer in JavaScript, you can use the Buffer.from() method. Here's an example:

const fetch = require('node-fetch');
const fs = require('fs');

async function base64ImageToBuffer(url) {
  const response = await fetch(url);
  const data = await response.buffer();
  const buffer = Buffer.from(data, 'base64');
  return buffer;
}

// Example usage
const imageUrl = 'data:image/png;base64,iVBORw0KG...'; // Base64 image URL
base64ImageToBuffer(imageUrl)
  .then(buffer => {
    fs.writeFileSync('image.png', buffer); // Saving buffer to a file
    console.log('Image saved!');
  })
  .catch(err => {
    console.error(err);
  });

In this example, the base64ImageToBuffer function asynchronously fetches the image data using node-fetch, converts it to a buffer using Buffer.from(), and returns the buffer. You can then save the buffer to a file or use it as needed

js base64 image url to Buffer

原文地址: https://www.cveoy.top/t/topic/ixKe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录