
Product
Reachability for Ruby Now in Beta
Reachability analysis for Ruby is now in beta, helping teams identify which vulnerabilities are truly exploitable in their applications.
react-compress-images
Advanced tools
Tiny, framework-friendly utilities to compress images in the browser using `browser-image-compression`, with handy helpers for Base64 conversion.
Tiny, framework-friendly utilities to compress images in the browser using browser-image-compression, with handy helpers for Base64 conversion.
File/File[] or Base64 string(s)npm install react-compress-images
# or
yarn add react-compress-images
Runtime: Browser environments (uses
File,FileReader, andfetch).
These are declared in peerDependencies for compatibility with React apps:
react (v18 or v19)react-dom (v18 or v19)Note: The utilities themselves are framework-agnostic and can be used without React.
Compress multiple images and get File[] back (default):
import { compressImages } from 'react-compress-images';
const inputFiles: File[] = Array.from(fileInput.files ?? []);
const compressedFiles = await compressImages({
type: 'multiple',
images: inputFiles,
});
Return Base64 strings instead:
const base64Images = await compressImages({
type: 'multiple',
images: inputFiles,
returnAsBase64: true,
});
Compress a single image:
const compressedFile = await compressImages({
type: 'single',
image: file,
});
const base64 = await compressImages({
type: 'single',
image: file,
returnAsBase64: true,
});
Overloads provide precise return types based on type and returnAsBase64:
// Single image
function compressImages(options: {
type: 'single';
image: File;
options?: import('browser-image-compression').Options;
returnAsBase64: true;
}): Promise<string>;
function compressImages(options: {
type: 'single';
image: File;
options?: import('browser-image-compression').Options;
returnAsBase64?: false | undefined;
}): Promise<File>;
// Multiple images
function compressImages(options: {
type: 'multiple';
images: File[];
options?: import('browser-image-compression').Options;
returnAsBase64: true;
}): Promise<string[]>;
function compressImages(options: {
type: 'multiple';
images: File[];
options?: import('browser-image-compression').Options;
returnAsBase64?: false | undefined;
}): Promise<File[]>;
browser-image-compression.
{ maxSizeMB: 0.2, maxWidthOrHeight: 1920, useWebWorker: true }true, returns Base64 string(s) instead of File/File[].Behavior and edge cases:
type: 'multiple' with an empty images array, resolves to an empty array.type: 'multiple', if compression fails, the function currently resolves to an empty array (matching the requested return type).type: 'single', errors bubble up (reject), so wrap in try/catch as needed.Converts a File/Blob to a Base64 data URL.
import { getBase64 } from 'react-compress-images';
const dataUrl = await getBase64(file);
Converts a Base64 data URL to a File. Safer for large/unicode data than manual atob parsing; uses fetch(dataUrl).blob() under the hood.
import { convertB64ToFile } from 'react-compress-images';
const file = await convertB64ToFile(
dataUrl, // must start with "data:"
'my-image', // optional filename without extension
'image/png' // optional fallback MIME if the data URL lacks one
);
Notes:
${filename}.${ext} inferred from the MIME (or fallbackType).dataUrl does not start with data:.import { useState } from 'react';
import { compressImages } from 'react-compress-images';
export function ImageCompressor() {
const [previews, setPreviews] = useState<string[]>([]);
async function onChange(e: React.ChangeEvent<HTMLInputElement>) {
const list = e.target.files;
if (!list || list.length === 0) return;
const files = Array.from(list);
const base64 = await compressImages({
type: 'multiple',
images: files,
returnAsBase64: true,
});
setPreviews(base64);
}
return (
<div>
<input type="file" multiple accept="image/*" onChange={onChange} />
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, 160px)', gap: 12, marginTop: 12 }}>
{previews.map((src, i) => (
<img key={i} src={src} alt={`preview-${i}`} style={{ width: 160, height: 160, objectFit: 'cover' }} />
))}
</div>
</div>
);
}
import { compressImages } from 'react-compress-images';
const input = document.querySelector('input[type="file"]');
input?.addEventListener('change', async (e) => {
const files = Array.from((e.target as HTMLInputElement).files ?? []);
const result = await compressImages({ type: 'multiple', images: files });
console.log(result);
});
maxSizeMB, maxWidthOrHeight, and initialQuality for your use case. See the
browser-image-compression docs for all options.File, you can read the File back as ArrayBuffer or Blob after compression.import type { Options } from 'browser-image-compression';
import type { CompressImagesOptions } from 'react-compress-images';
File, FileReader, and fetch for data URLs.ISC © Ahmed Elsehrawy
FAQs
Tiny, framework-friendly utilities to compress images in the browser using `browser-image-compression`, with handy helpers for Base64 conversion.
We found that react-compress-images demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Reachability analysis for Ruby is now in beta, helping teams identify which vulnerabilities are truly exploitable in their applications.

Research
/Security News
Malicious npm packages use Adspect cloaking and fake CAPTCHAs to fingerprint visitors and redirect victims to crypto-themed scam sites.

Security News
Recent coverage mislabels the latest TEA protocol spam as a worm. Here’s what’s actually happening.