Handling HEIC Images in Angular: A Comprehensive Tutorial

HEIC to JPG

HEIC to JPG

In this article, we will learn how to handle the HEIC image formats in an angular application, To begin with, you should have basic knowledge of angular.

Table of Contents:

  • Overview of HEIC Image Format
  • Setting up the Angular Application
  • Installing Necessary Dependencies
  • Creating a Custom Directive for Image Conversion
  • Handling File Upload and Image Preview
  • Implementing Image Conversion and Download
  • Displaying HEIC Images in Angular Components
  • Adding Support for Multiple Images
  • Integrating with a Backend Service
  • Using Alternative Libraries for Image Conversion
  • Browser Support and Compatibility Considerations
  • Testing and Debugging
  • Performance Optimization
  • Conclusion

Overview of HEIC Image Format:

The High-Efficiency Image Container (HEIC) is an image format based on the High-Efficiency Video Codec (HEVC). It was introduced by Apple in iOS 11 and macOS High Sierra as a more efficient alternative to the widely used JPEG format. HEIC images offer better compression and higher image quality than JPEG images, resulting in smaller file sizes without sacrificing image quality. However, not all web browsers and operating systems support the HEIC format, making it necessary to convert HEIC images to more widely supported formats like JPEG or PNG in web applications.

Here is the git repository link to the application developed in this tutorial https://github.com/psmohan/handling-heic-angular/tree/master

This tutorial will guide you through handling HEIC images in an Angular application, including uploading, previewing, converting, and displaying HEIC images. We will also cover various aspects like error handling, progress indication, and performance optimization.

Setting up the Angular Application:

First, create a new Angular application using the Angular CLI:

This command will create a new Angular project in the “heic-handling” directory.

Installing Necessary Dependencies:

For this tutorial, we’ll use the heic2any library to convert HEIC images to JPEG or PNG format. Install the library and its required dependencies using npm:

Creating a Custom Directive for Image Conversion:

To simplify handling HEIC images in Angular components, we’ll create a custom directive called appHeicToJpeg. This directive will convert an input HEIC file to a JPEG or PNG image and update the src attribute of an <img> element.

Create a new file called heic-to-jpeg.directive.ts in the src/app directory and add the following code:

This code defines a new directive with an input property heicFile. When the heicFile property is set, the convertHeicToJpeg method is called, which converts the HEIC file to a JPEG image using the heic2any library. The resulting JPEG image is then set as the src attribute of the <img> element.

Next, add the HeicToJpegDirective to the declarations array in the src/app/app.module.ts file:

Handling File Upload and Image Preview:

Now, let’s create a simple file input element in the src/app/app.component.html file to allow users to upload HEIC images:

Next, update the src/app/app.component.ts file to handle the selected file:

To display a preview of the uploaded HEIC image, add an <img> element to the src/app/app.component.html file and use the appHeicToJpeg directive:

Implementing Image Conversion and Download:

To allow users to download the converted JPEG image, add a download button to the src/app/app.component.html file:

Update the src/app/app.component.ts file to implement the downloadConvertedImage method:

Output:

Downloading the converted HEIC image

Downloading the converted HEIC image

Displaying HEIC Images in Angular Components:

Now that we’ve implemented the core functionality, you can easily display HEIC images in your Angular components by using the appHeicToJpeg directive. For example, if you have an array of HEIC images

You can display these images in your component’s template by iterating over the array using the *ngFor directive and applying the appHeicToJpeg directive to each <img> element:

Here’s what the code does:

  1. The *ngFor directive iterates over the heicImages array and creates a new <div> element for each image in the array.
  2. The {{ image.name }} expression displays the name of the image file.
  3. The <img> element uses the [appHeicToJpeg]=”image” binding to apply the appHeicToJpeg directive to each image.
  4. This directive takes care of converting the HEIC image to a JPEG image and updating the src attribute of the <img> element.

Adding Support for Multiple Images:

To extend our application to support multiple HEIC images, modify the file input element in the src/app/app.component.html file to accept multiple files:

Update the src/app/app.component.ts file to handle multiple selected files:

When clicked on the download button below the image, that file will be passed to the downloadConvertedImage().

Now, update the src/app/app.component.html file to display and convert multiple images:

Converting multiple HEIC images at once

Converting multiple HEIC images at once

Integrating with a Backend Service:

In a real-world scenario, you may need to send the converted images to a backend service for further processing or storage. To achieve this, you can create a service to handle HTTP requests and upload images.

First, generate a new service using the Angular CLI:

Next, update the src/app/image-upload.service.ts file to implement the uploadImage method:

Make sure to replace https://your-api-url.com/upload with the actual API URL.

Now, update the src/app/app.module.ts file to import the HttpClientModule:

modify the src/app/app.component.ts file to use the ImageUploadService and upload the converted images:

Finally, modify the src/app/app.component.html file to show the upload to server button:

Upload to backend web server

Upload to backend web server

Backend server

If you don’t have a backend server to test this, you can always use Beeceptor to mock a backend server to upload a file. Here are the detailed steps:

First, go to https://beeceptor.com/ and sign up for a free account. Once you have signed up, log in to your account.

After logging in, click on the “Create Endpoint” button after entering a sample endpoint name.

Click on the Mocking rules option shown below

Click on the Mocking rules option shown below

Page results: Looks Awesome!

Page results: Looks Awesome!

Now, enter the URL endpoint for the file upload API that you want to mock. For example, if your actual API endpoint for file upload is https://api.example.com/upload, then you can enter “/upload” in the “Endpoint” field, and save the rule.

Configuring the endpoint

Configuring the endpointWhen you click on the upload to server button you should be able to see the following results in the Beeceptor dashboard.

/upload request recived by backend web server.

/upload request recived by backend web server.

Request received by backend web server

Request received by backend web server

Above are the images showing the request received in backend webserver.

Using Alternative Libraries for Image Conversion:

While heic2any is a convenient library for converting HEIC images, there are other libraries available, such as libheif-js. You can experiment with different libraries to see which one best suits your needs.

Browser Support and Compatibility Considerations:

HEIC image support is not universal across all browsers and operating systems. As a result, always test your application in various browsers to ensure compatibility.

Testing and Debugging:

Use the Angular CLI to run unit and end-to-end tests to ensure your application works as expected:

You can go through the official angular documentation to learn more about testing and debugging in angular https://angular.io/guide/test-debugging

Performance Optimization:

Large HEIC files may take some time to process, and conversion performance can be a concern. To optimize performance, consider using web workers to offload the conversion process to a separate thread.

Conclusion

In this tutorial, we’ve covered how to handle HEIC images in Angular applications, from setting up the environment to uploading, previewing, and converting images. We also touched on error handling, progress indication, and integration with backend services. With this knowledge, you should now be able to build robust Angular applications that can handle HEIC images.

References

https://angular.io/

https://www.npmjs.com/package/heic2any

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

15.10.2020

A comprehensive insight of the Angular Structure

Whenever you start learning a new technology, learning about its structure is one of the most important things. Let's understand the Angular ...

2.03.2020

How to Create a Personal Blogging Website: Front-End (Angular) #2

In this article, we are going to create the client-side part of the personal blogging website using ...

How I Got Started with Angular and TypeScript

I share my experience on how to learn the most basic parts of Angular and ...

1 comment

Egor kozachok November 22, 2023 at 3:50 am
0

This is very useful advice

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy