Skip to content
the poor coder

convert base64 to image nodejs

Poor Man
Published
Reading time
2 min read

Base64 encoding is a way of representing binary data using only characters from the ASCII character set. It is commonly used to send small binary files such as images over the internet, and is also used to encode data in XML and JSON documents. To convert a base64 string to an image in Node.js, you can use the Buffer module to decode the base64 string and create the image from the decoded bytes:

JavaScript

const fs = require('fs');
const imageString = 'base64-encoded-string';
const imageBuffer = Buffer.from(imageString, 'base64');
fs.writeFile('my-image.jpg', imageBuffer, (err) => {
  if (err) {
    console.error(err);
  }
});

The Buffer.from method takes two parameters - the encoded string, and the encoding. In this case, it is 'base64'. The fs.writeFile method takes three parameters - the path of the file to be written, the data to be written, and a callback function to be executed once the write operation is complete. In this case, the data is the imageBuffer created from the base64 encoded string.

You can also use the Buffer.alloc method to create a buffer of a given size and fill it with the given data. This is useful for larger files as it avoids having to read the entire file into memory at once. For example:

JavaScript

const fs = require('fs');
const imageString = 'base64-encoded-string';
const imageSize = 1024 * 1024; // 1 MB
const imageBuffer = Buffer.alloc(imageSize, imageString, 'base64');
fs.writeFile('my-image.jpg', imageBuffer, (err) => {
  if (err) {
    console.error(err);
  }
});

The Buffer.alloc method takes three parameters - the size of the buffer to be created, the data to be written, and the encoding. In this case, it is 'base64'. Once again, the fs.writeFile method takes three parameters - the path of the file to be written, the data to be written, and a callback function to be executed once the write operation is complete.

There are also third-party modules available for Node.js that can be used to convert base64 strings to images. For example, the node-base64-image module provides methods for converting base64 strings to images. For example:

JavaScript

const base64Image = require('node-base64-image');
base64Image.base64decode('base64-encoded-string', 'my-image.jpg', (err) => {
  if (err) {
    console.error(err);
  }
});

The base64decode method takes three parameters - the encoded string, the path of the file to be written, and a callback function to be executed once the write operation is complete.

Whichever method you choose, the result will be an image file created from the base64 encoded string.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC