Convert Base64 to Image using Angular - A Step by Step Guide | CodeLSC

Angular Base64 Image conversion Step by step guide CodeLSC Web development Front-end programming Data encoding/decoding

Step by Step Video Tutorial:




Base64 is a widely used binary-to-text encoding scheme that represents binary data in an ASCII string format. It is commonly used to transmit data over the web and can also be used to store images. However, to display an image on the web, it needs to be converted back to its original binary format. This guide will show you how to convert Base64 to Image using Angular.

Prerequisites

  • Basic knowledge of Angular
  • Base64 string of an image

Step 1: Create a new Angular project

To get started, you need to have Angular installed on your machine. If you don't have it installed, you can follow this guide to install it.

Once you have Angular installed, open your terminal or command prompt and run the following command to create a new Angular project:

ng new base64-to-image

 

Step 2: Create a Component

In this step, we'll create a component that will be responsible for converting the Base64 string to an image. Run the following command in your terminal or command prompt:

ng generate component base64-to-image

 

Step 3: Implement the HTML Template

Now, open the base64-to-image.component.html file and paste the following code:

<div id="main_div">

  <!-- Title div -->

  <div class="title">{{ title }}</div>

 

  <!-- Container div -->

  <div class="container">

    <!-- Input field for base64 string -->

    <input

      type="text"

      [(ngModel)]="base64String"

      class="input-field"

      placeholder="base64 string"

    />

 

    <!-- Convert button, disabled if base64 string is not valid -->

    <button

      (click)="convertToImage()"

      [disabled]="!isValidString()"

      class="convert-button"

    >

      Convert

    </button>

 

    <!-- Display image -->

    <img [src]="imageUrl" /><br />

 

    <!-- Download button for image, visible only if image is valid -->

    <div>

      <a

        [href]="imageUrl"

        *ngIf="isValidImage()"

        [download]="'image.jpeg'"

        class="download-button"

        >Download</a

      >

    </div>

  </div>

</div>

 

Step 4: Implement the TypeScript Code

In this step, we'll be implementing the TypeScript code for the component we created in step 2. The TypeScript code defines the logic for converting the Base64 string to an image and displaying it on the web page.

Let's break down the code into different parts:

Variable Declarations:

 

base64String: string;

imageUrl: any;

 

The base64String variable is used to store the Base64 string input by the user. It is declared as a string. The imageUrl variable is used to store the URL of the image after it is converted from the Base64 string. It is declared as type any so it can store any type of data.

convertToImage() Method:

 

convertToImage() {

  this.imageUrl = this.base64String;

}

 

The convertToImage() method sets the value of the imageUrl variable to the value of the base64String variable. This is the core logic of converting the Base64 string to an image.

isValidString() Method:

 

isValidString() {

  return this.base64String && this.base64String.length > 0;

}

 

The isValidString() method returns true if the base64String variable is not null and has a length greater than 0. It is used to determine if the Base64 string input by the user is valid or not.

isValidImage() Method:

 

isValidImage() {

  return this.imageUrl && this.imageUrl.length > 0;

}

 

The isValidImage() method returns true if the imageUrl variable is not null and has a length greater than 0. It is used to determine if the image has been successfully converted from the Base64 string or not.

With these four parts, the complete TypeScript code for the component will look like this:

export class AppComponent {

  // Define the title of the application

  title = 'Base64 to Image Converter';

 

  // Initialize the base64String variable as a string

  base64Stringstring;

 

  // Initialize the imageUrl variable as any type

  imageUrlany;

 

  // Define the convertToImage() method

  convertToImage() {

    // Set the imageUrl variable to the value of the base64String variable

    this.imageUrl = this.base64String;

  }

 

  // Define the isValidString() method

  isValidString() {

    // Return true if the base64String variable is not null and has a length greater than 0

    return this.base64String && this.base64String.length > 0;

  }

 

  // Define the isValidImage() method

  isValidImage() {

    // Return true if the imageUrl variable is not null and has a length greater than 0

    return this.imageUrl && this.imageUrl.length > 0;

  }

}

 

Step 5: Add Styles In this step

we'll add some styles to make the UI look nice. Open the base64-to-image.component.css file and paste the following code:

@import url('https://fonts.googleapis.com/css2?family=Bleeding+Cowboys&display=swap');

 

.title {

  font-family'Bleeding Cowboys'cursive;

  font-size48px;

  positionrelative;

  text-aligncenter;

}

.container {

  displayflex;

  flex-directioncolumn;

  align-itemscenter;

  padding20px;

}

 

.input-field {

  width60%;

  height40px;

  margin-bottom20px;

  padding10px;

  font-size16px;

  border1px solid gray;

  border-radius5px;

}

 

.convert-button {

  width60%;

  height40px;

  margin-bottom20px;

  background-colorrgb(2128255);

  colorwhite;

  font-size16px;

  bordernone;

  border-radius5px;

  cursorpointer;

}

 

.convert-button:hover {

  background-colorrgb(12194255);

}

 

.download-button {

  margin-bottom20px;

  background-colorrgb(0 177 51);

  colorwhite;

  font-size16px;

  bordernone;

  border-radius5px;

  cursorpointer;

  text-decorationnone;

  padding15%;

}

 

.download-button:hover {

  background-colorrgb(2 211 62);

}

 

img {

  width60%;

  margin-top20px;

  border1px solid gray;

  border-radius5px;

}

 

@media only screen and (max-width600px) {

  .input-field,

  img {

    width100%;

  }

}

 

#main_div {

  margin5% auto;

  width70%;

  border1px solid grey;

  border-radius1%;

  background-imageurl('https://wallpaperaccess.com/full/809214.jpg');

  background-sizecover;

  background-repeatno-repeat;

  displayflex;

  flex-directioncolumn;

  align-itemscenter;

  justify-contentcenter;

}

.input-field:focus {

  outlinenone;

  border-color#2e7bf6;

  box-shadow0 0 20px #4d90fe;

}

 

Step 6: Serve the Application

To run the application, open your terminal or command prompt and run the following command:

ng serve


Open your browser and navigate to http://localhost:4200/. You should see the Base64 to Image converter application. Try entering a Base64 string of an image and clicking the Convert button. You should see the image displayed. You can also download the image by clicking the Download button.

Conclusion:

 In this guide, we showed you how to convert Base64 to Image using Angular. This can be useful for displaying images stored in a database as Base64 strings. With the steps outlined in this guide, you should be able to implement this functionality in your own Angular projects.


Stackblitz Exmplehttps://stackblitz.com/edit/angular-ivy-keudnf?embed=1&file=src/app/app.component.ts 

Comments