Logo

dev-resources.site

for different kinds of informations.

Client-side image compression with Supabase Storage

Published at
12/12/2022
Categories
supabase
webdev
javascript
compression
Author
mikeesto
Author
8 person written this
mikeesto
open
Client-side image compression with Supabase Storage

One of the projects I have been working on allows users to upload and share images. Super novel, I know. We are using Supabase for the backend, and the images are stored in Supabase Storage - an S3 compatible object store. I’ve used Supabase a few times now and found it overall to be a neat product (#notsponsored). However, this was my first time using the storage offering and we ran into an issue from the outset. Users were uploading photos from their phones with very large resolutions and consequently huge file sizes. This wasn’t good because we have limits on storage and bandwidth, plus the gallery view of the app was taking forever to load.

Unfortunately, Supabase does not currently support any kind of image optimization. It’s been commonly requested, and quite likely on their roadmap, but at least for now there wasn’t a setting I could quickly toggle to solve the problem. I’ve run into a similar issue before using Amazon S3, and in that project we used a lambda function to do image optimisation when an image was uploaded to the bucket. But this didn’t seem particularly straightforward with Supabase, so I went searching for a different solution.

After researching a couple of different options, what I landed on was a package called compressorjs written by Chen Fengyuan. This library performs compression and resizing of images in the browser. I was a bit skeptical at first, but it's a cool project and has good browser support. How Compressorjs works is that it uses the HTML5 canvas element to read the original image data and perform lossy transformations to compress and resize the image. There's a whole bunch of transformation options. For my project, what I have found is simply changing the maximum width of the image to 600px and slightly reducing the quality has greatly reduced the file sizes.

Here is how you might go about using compressorjs with Supabase:

import Compressor from "compressorjs";

document.getElementById("file").addEventListener("change", (e) => {
  const image = e.target.files[0];

  if (!image) {
    return;
  }

  new Compressor(image, {
    quality: 0.9,
    maxWidth: 600,

    success(compressedImage) {
      // compressedImage contains the Blob
      // Upload the compressed image to Supabase storage
      supabase.storage
        .from("images")
        .upload(`public/filename`, compressedImage);
    },
    error(err) {
      console.log(err.message);
    },
  });
});
Enter fullscreen mode Exit fullscreen mode

Cover photo thanks to Tamanna Rumee

compression Article's
30 articles in total
Favicon
AVIF File Format: The Evolution in Web Image Compression
Favicon
Lightweight, Transparent, Animated: Get All of Them by WebP Format
Favicon
Flutter Image Compression: Ensuring High Quality
Favicon
Compression Is About Knowing Your Data
Favicon
How to use compression in Node.js server for better bandwidth ?
Favicon
Dall.E Image Gen, And Size Comparison Of Image Formats
Favicon
When life gives you lemons ...
Favicon
Image Compression in JavaScript/TypeScript
Favicon
How to Use IMGCentury For Bulk & Unlimited Image Compressions?
Favicon
Created simple phone number compression functions.
Favicon
Exploring the LZ77 Compression Algorithm
Favicon
Ultimate 3D Compression: echo3D Introduces Compression Tools for 3D Content to Accelerate 3D Development
Favicon
Reduce Network Usage - With This 1 Simple Trick!
Favicon
How Finding the Right Compression Level Can Speed Up your Website
Favicon
Client-side image compression with Firebase and Compressor.js
Favicon
Client-side image compression with Supabase Storage
Favicon
COS Cost Optimization Solution
Favicon
Is there any way to compress the data while using mongo persistence with NEventStore?
Favicon
Search safe number compression algorithm(feat. Python)
Favicon
Golang HTTP Handler With Gzip
Favicon
How to use AVIF today!
Favicon
Large documents in redis: is it worth compressing them (Part 1)
Favicon
Open or create TAR files in C# with Aspose.ZIP
Favicon
Supercharge your API with Compression
Favicon
Django 3.2 - News on compressed fixtures and fixtures compression
Favicon
How I made my own file compressor using Node.js
Favicon
Compressed GraalVM Native Images: the best startup for Java apps comes in tiny packages
Favicon
What is lossy and lossless compression
Favicon
did you ever ask yourself how file compression works ? the maths behind greedy algos
Favicon
Huffman coding from scratch with Elixir

Featured ones: