Compress images in React: Browser Image Compression Libary

Search for a command to run...

No comments yet. Be the first to comment.
Easily compress images on the client with React(javascript)
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-compressionreact-image-file-resizer Really fastCo...
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...

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-compressionreact-image-file-resizer Really fastCo...

I recently got interested in compressing images on the client-side for a couple of reasons:
So I ventured to look for a library that will help me solve this fast and easy, maybe in the future I might build my own from scratch.
So I came by a couple of libraries that will help in compressing images on the client-side.
Install it with your favorite package manager
npm install browser-image-compression --save
or
yarn add browser-image-compression
Create your react component
import React from "react";
class App extends React.Component {
onChange = async (e) => {
// Some onchange event calls
};
render() {
return (
<input name="file" id="file" onChange={this.onChange} type="files" />
);
}
}
Start compressing
...
import Compress from "browser-image-compression";
...
onChange = (e) => {
// Get the files selected from the input tag
// On select files always come in an array even
// if you choose one its the first index
// if you selected a couple then loop through the array
const file = e.target.files[0]
// Compression config
const options = {
// As the key specify the maximum size
// Leave blank for infinity
maxSizeMB: 1.5,
// Use webworker for faster compression with
// the help of threads
useWebWorker: true
}
// Initialize compression
// First argument is the file object from the input
// Second argument is the options object with the
// config
Compress(file, options)
.then(compressedBlob => {
// Compressed file is of Blob type
// You can drop off here if you want to work with a Blob file
console.log(compressedBlob)
// If you want to work with the File
// Let's convert it here, by adding a couple of attributes
compressedBlob.lastModifiedDate = new Date()
// Conver the blob to file
const convertedBlobFile = new File([compressedBlob], file.name, { type: file.type, lastModified: Date.now()})
// Here you are free to call any method you are gonna use to upload your file example uploadToCloudinaryUsingPreset(convertedBlobFile)
})
.catch(e => {
// Show the user a toast message or notification that something went wrong while compressing file
})
}
Next, we will cover using react-image-file-resizer library