In the previous tutorial, we discussed the file upload from a Angular Client to a Spring Boot back-end server. Most uploaded files on web are images. It’s always wise to compress and minify before sending them to the server. In this tutorial we will see how to Angular compress image before upload to Spring Boot back-end.

Install NPM libraries


As for the previous tutorial, we will use the same ng2-file-upload for file uploading. So install it :


For image compression, we will use image-compressor.js. Install it using npm :

Once the library is installed, you need to add script in your application. If you are using Angular-CLI (which I highly recommend), add the script relative path to your angular.json like below :

Libraries are now installed, let’s now see the Angular part.


ImgCompressor directive

To compress the image, we will extend the ng2-file-upload directive and add our compression there. Below the implementation :

  • img-compressor.directive.ts

Don’t forget to add the directive in the declarations of your @NgModule.

Component template

The template is a basic input using the  appImgCompressor directive and an uploader.

  • app.component.html


  • app.component.ts
That’s it. Your images will now be reduced by 50%.

The tutorial source code is available on Github. Download the source