Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Nodejs » Node js convert image to base64 Example

Node-js-convert-image-to-base64-Example

Node js convert image to base64 Example

Posted on January 1, 2022February 17, 2022 By XpertPhp

In this article, we will explain to you how to convert image to base64 string in node js(Node js convert image to base64 Example). sometimes we need to convert image file to base64 encoding.

If you want to convert an image file to base64 then you can there are two ways such as simple process and third-party npm package.

Table of Contents

  • Node Js Application Setup
  • Install NPM Package
  • Example 1
  • Example 2
  • Run Node js Application

Node Js Application Setup

First, we will open the command prompt and create the application in our directory. for this, you can follow the below command.

1
2
3
mkdir my_node_app
cd my_node_app
npm init

Install NPM Package

After the done setup node js application, we will install the express npm package for routing. so you can see the following npm command.

1
npm install express --save

Example 1

In this example, we will convert the image to a string using simple ways. so you can see the following example.
server.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var express=require('express');
var app = express();
var fs = require('fs');
 
function base64_encode(file) {
    return "data:image/png;base64,"+fs.readFileSync(file, 'base64');
}
 
app.get('/',function(req,res){
   var base64str = base64_encode('test.png');
});
 
app.listen(3000,function(){
     console.log("Express Started on Port 3000");
});

Example 2

In this example, we will encode image into base64 string using the node-base64-image npm module. so you can see the following example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var express=require('express');
var app = express();
var base64 = require('node-base64-image');
 
app.get('/',function(req,res){
   var options = {string: true};
   base64.base64encoder('test.png', options, function (err, image) {
    if (err) {
        console.log(err);
    }
    console.log(image);
   });
});
 
app.listen(3000,function(){
     console.log("Express Started on Port 3000");
});

Run Node js Application

we will run the node js application using the below command. so you can follow the below command.

1
node server.js

Now you can run the example using the below Url in the browser.

1
http://localhost:3000

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • How to install node js on ubuntu 16.04 and 18.04
  • Node JS Axios HTTP PUT Request Example
  • How To Send Mail Using Nodemailer In Node Js
  • How to Render HTML File in Node JS Express
  • node js routes in separate file using express
Nodejs

Post navigation

Previous Post: How to Render HTML File in Node JS Express
Next Post: how to use ckeditor 5 in php

Latest Posts

  • Laravel Check If Foreach is Empty Example
  • Laravel Check Array Empty in Blade
  • PHP Datatables CRUD Example
  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com