npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mbs-dev/react-image-compressor

v1.0.5

Published

react image compressor

Downloads

17

Readme

@mbs-dev/react-image-compressor

A package for compressing images efficiently and effectively in React applications. Keep your image quality, save your storage and make your website faster.

Installation

To add this package to your project, execute the following command in your terminal:

npm i @mbs-dev/react-image-compressor

Usage

First, import the necessary functions from the package in your React component:

import {compressAndAppendImage, imageCompressor} from '@mbs-dev/react-image-compressor';

Method -1 | using compressAndAppendImage function


const [imageFile, setimageFile] = useState<File>()
const formData = new FormData();
const imagefileFieldName = 'photo'
const quality = 0.8

//-- Using formik
await compressAndAppendImage(formik.values.image as unknown as File, formData,imagefileFieldName,quality)

//-- Using useState
await compressAndAppendImage(imageFile, formData,imagefileFieldName,quality)

Method -2 | using imageCompressor function



const [imageFile, setImageFile] = useState<File>();
const formData = new FormData();
const quality = 0.2

//-- Using Formik
const compressedImage = await imageCompressor(formik.values.image as unknown as File, quality);
const fieldName = formik.values.image as unknown as File;
const imagefileFieldName = fieldName.name.replace(' ', '-').slice(0, fieldName.name.lastIndexOf('.')) + '.webp';
formData.append('photo', compressedImage as File, imagefileFieldName);


//-- Using useState
const compressedImage = await imageCompressor(imageFile, quality);
const fieldName = imageFile as File;
const imagefileFieldName = fieldName.name.replace(' ', '-').slice(0, fieldName.name.lastIndexOf('.')) + '.webp';
formData.append('photo', compressedImage as File, imagefileFieldName);

Notes

The imagefileFieldName parameter is used to specify a custom file name for the compressed image. When images are compressed, the default behavior is to return the compressed image with a generic name, often as 'blob'. By setting imagefileFieldName, you can define a meaningful name for the output file, making it easier to identify, organize, and reference the compressed images in your application or storage system.


The quality parameter in image compression functions is used to specify the desired quality level of the compressed image. This parameter accepts values in the range from 0 to 1, where 0 represents the lowest possible quality (and thus the highest compression) and 1 represents the highest possible quality (and thus the lowest compression). Values between 0 and 1 allow you to balance between image quality and file size according to your needs.


When compressing images, particularly for products where detail and color accuracy are crucial—like cosmetics—it's essential to strike a balance between reducing file size and maintaining high-quality visuals. By setting the compression quality to around 95% to 99%, or equivalently, a quality parameter value of 0.2 , you can significantly reduce the image file size while ensuring the compressed images maintain quality that is virtually indistinguishable from the original images.

Some experimental results


//-- Using quality : 0.2
10Mb  -->  ~355Kb
15Mb  -->  ~368Kb
20Mb  -->  ~353Kb


//-- Using quality : 0.8
10Mb  -->  ~858Kb
15Mb  -->  ~939Kb
20Mb  -->  ~925Kb

Image size after compression can change depending on image content or other factors.