Compress images in React: React Image File Resize

Search for a command to run...

No comments yet. Be the first to comment.
Easily compress images on the client with React(javascript)
I recently got interested in compressing images on the client-side for a couple of reasons: Reduce my cloudinary bandwidth and storage usage Users were complaining about long waits while uploading images So I ventured to look for a library that wil...
Recently I started building tools that will help me automate my app deployment process easier and cheaper, to do that I knew CLIs are definitely going to be involved one way or another. So I started building two CLIs: Simp CLI: It's like Makefile ju...

Let's jump into it... fast. You got here because you want to setup Dokku and the other ways don't work... let's goooo I will setup for Dokku only Point your domain to the VPS of choice Spin up new VPS server here: Digital Ocean Linode Buy a dom...

So what is Dokku, another bit, another TL;DR; Let's jump into it. You might know Heroku, (my opinion) the greatest platform to ever make it easy to deploy server and client-side applications so fast and easy. Well Heroku is a business and they have t...

I recently got interested in compressing images on the client-side for a couple of reasons: Reduce my cloudinary bandwidth and storage usage Users were complaining about long waits while uploading images So I ventured to look for a library that wil...

Okay for this one I highly recommend, the speeds is just amazing. I loved it. I am gonna do a comparison real quick, between browser-image-compression and react-image-file-resizer.
browser-image-compression | react-image-file-resizer |
| Really fast | Compressed the same took a while |
| Quality option on the config | Only offers size limit |
Return base64 or Blob | Returns Blob only do the conversion yourself |
| No need to handle Promise | Must handle Promise |
| Specify compress format (png, webp, jpeg) | The compress format provided is the one returned |
Below is an image I was able to compress with the following config
width -> 480px
height -> 480px
file format -> Set to JPEG
quality -> 50
rotation -> 0
(Click here) Click on the link its a huge image and I just want the load time for this blog to be faster.

Let's jump into how you can plug it into your React app
Install the package
yarn add react-image-file-resizer
Create your react component
import React from "react";
// ...
class App extends React {
// ...
render() {
return (
<div className="App">
// ...
<h3>React Image File Resizer</h3>
<br />
<input
type="file"
id="file"
accept="image/*"
onChange={this.onFileResize}
/>
</div>
);
}
}
Start compressing
...
import Compress from "react-image-file-resizer";
...
onFileResize = e => {
const file = e.target.files[0];
Resizer.imageFileResizer(
file, // the file from input
480, // width
480, // height
"JPEG", // compress format WEBP, JPEG, PNG
70, // quality
0, // rotation
(uri) => {
console.log(uri);
// You upload logic goes here
},
"base64" // blob or base64 default base64
);
}
Again I highly recommend this library especially because it supports compressing to WEBP(file of this format are really fast).
Note: Set quality to 100 if you want to compress to PNG/WEBP(Lossless) else set the compressed format to JPEG(Lossy)
We will cover understanding Lossy and Lossless compression.