# Simple image compression

> We have reducing our image storage for iHateReading by 20%

_Published 2025-07-01_

_Author: Shrey Vijayvargiya_

Hello and welcome to the new blog

Today, we will be covering a small but important use case of image compression methods.

While writing this kind of blog for our audience, we prefer using our own custom Editor as shown in the image below

![](https://firebasestorage.googleapis.com/v0/b/ihatereading-4ba52.appspot.com/o/Logs%2FQXq1wZdVU3X7pGJJjdiB2JSavdk1%2Fimages%2FScreenshot%202025-07-01%20at%203.26.53%E2%80%AFPM.png?alt=media&amp;token=9516c01e-4bd4-476d-ab81-5c17aec42db4)

Now, one can make this kind of Editor on their using 

1. Editorjs
2. Tiptap Editor
3. Markdown Editor

However, each editor needs images and videos to manage and then store in the database. Storing images directly from the editor to the storage, like AWS or Firebase storage, will take a huge amount of space 

Compression is the best alternative apart from other big alternatives and I am only talking about client side image compression, compress and reduce the size of an image and store automatically the reduced size inside storage.

## Browser Image Compression

<a href="https://www.npmjs.com/package/browser-image-compression" target="_blank" rel="external">https://www.npmjs.com/package/browser-image-compression</a>

This single package helps to reduce the image size by far, almost more than 20% without compromising much on the quality side.

One can even use ffmpeg as well, <a href="https://www.npmjs.com/package/ffmpeg" target="_blank" rel="external">https://www.npmjs.com/package/ffmpeg</a>

One single method and the uploaded image can be compressed as shown below

```
async function handleImageUpload(event) {

  const imageFile = event.target.files[0];
  console.log('originalFile instanceof Blob', imageFile instanceof Blob); // true
  console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);

  const options = {
    maxSizeMB: 1,
    maxWidthOrHeight: 1920,
    useWebWorker: true,
  }
  try {
    const compressedFile = await imageCompression(imageFile, options);
    console.log('compressedFile instanceof Blob', compressedFile instanceof Blob); // true
    console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`); // smaller than maxSizeMB

    await uploadToServer(compressedFile); // write your own logic
  } catch (error) {
    console.log(error);
  }

}
```

How cool is that, once you get the compressed image file store is directly inside storage.

Just a few lines of code in React before uploading data in the backend database will certainly optimise the application by far.

Also prefer using image lazy loading on the client side while fetching and rendering the images, if using Next.js, then usethe  Image component from next/image or simply use the "lazy" prop for the img tag

&lt;img loading="lazy" src="" /&gt;

That's it for today, see you in the next one

iHateReading

## Summary

This blog post discusses client-side image compression for web applications using rich text editors like Editor.js, Tiptap, or Markdown Editor.  Storing uncompressed images consumes significant storage space; therefore, the author recommends using the `browser-image-compression` npm package (or alternatively, ffmpeg) to reduce image size before uploading to storage services like AWS or Firebase.  A code example demonstrates how to compress images using this package, achieving over 20% size reduction without significant quality loss. The post also suggests using image lazy loading (`loading="lazy"` attribute) for further optimization.

---

Canonical HTML: https://ihatereading.in/blogs/simple-image-compression
Markdown: https://ihatereading.in/blogs/simple-image-compression.md
