Skip to content
the poor coder

base64 to png nodejs

Poor Man
Published
Reading time
2 min read
On this page

Converting Base64 to PNG with Node.js#

If you're working with images in Node.js, there may be times when you need to convert a Base64 encoded string to a PNG image file. Fortunately, this can be done using some built-in Node.js modules.

Method 1: Using the Buffer Module#

The first method involves using the Buffer module to decode the Base64 string and then writing the resulting binary data to a file with a .png extension.

Text
const fs = require('fs');

// Base64 encoded string
const base64String = 'iVBORw0KGgoAAAANSUhEUgAAAAUA...';

// Decode the string
const binaryData = Buffer.from(base64String, 'base64');

// Write the data to a file
fs.writeFile('image.png', binaryData, (err) => {
  if (err) throw err;
  console.log('The file has been saved!');
});

This code reads in a Base64 encoded string and decodes it using the Buffer.from() method, which takes the string as its first argument and the encoding type as its second argument (in this case, 'base64'). The resulting binary data is then written to a file with a .png extension using the fs.writeFile() method.

Method 2: Using the Node-Base64 Image Module#

If you prefer a more streamlined approach, you can use the node-base64-image module to convert the Base64 string directly to a PNG image file.

Text
const base64Img = require('base64-img');

// Base64 encoded string
const base64String = 'iVBORw0KGgoAAAANSUhEUgAAAAUA...';

// Convert to image file
base64Img.img(base64String, '.', 'image', (err, filepath) => {
  if (err) throw err;
  console.log('The file has been saved!');
});

Here, the base64-img module is used to convert the Base64 string to an image file. The img() method takes the string to convert as its first argument, the directory to save the image in as its second argument, and a file prefix as its third argument (in this case, 'image'). The resulting filepath is returned in the callback function.

Conclusion#

Both of these methods allow you to easily convert a Base64 encoded string to a PNG image file in Node.js. Which method you choose will depend on your specific use case and personal preference.

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