| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A tool for compressing images. It picks the quality level for you and keeps the smallest format that still looks fine.
Available as a CLI tool and a JS library (browser + Node).
npm install lessbytesBrowser, no build step:
<script src="https://unpkg.com/lessbytes"></script>
<script>
lessbytes.compress(file).then(r => console.log(`${(r.ratio * 100).toFixed(1)}% smaller`));
</script>// ESM / bundlers
import { compress, compressBatch, computeSSIM, isFormatSupported } from 'lessbytes';
// CommonJS
const { compress } = require('lessbytes');
// Browser <script> tag — exposes a global `lessbytes`input can be a File, Blob, HTMLImageElement, URL string, ArrayBuffer, Uint8Array, or ImageData.
const result = await compress(file, {
format: 'webp', // 'webp' | 'auto' | 'jpeg' | 'png' | 'avif'
maxWidth: 1920, // resizes down if larger, never upscales
maxHeight: null,
targetSize: 100 * 1024, // optional byte limit, e.g. for upload restrictions
background: '#ffffff', // fill color used if flattening transparency
keepSmallest: true, // return original if compression makes it bigger
perceptualGamma: true // use linear sRGB luma calculations (default: true)
});
result.blob // compressed image
result.format // e.g. 'webp'
result.ratio // e.g. 0.84 (file is 84% smaller)
result.toFile('name.webp') // wraps result.blob as a FileExample, upload handler:
input.addEventListener('change', async () => {
const { blob, toFile } = await compress(input.files[0], { maxWidth: 1920 });
preview.src = URL.createObjectURL(blob);
formData.append('photo', toFile('photo.webp'));
});Compress multiple files, with a progress callback.
const results = await compressBatch(files, {
concurrency: 3,
maxWidth: 1600,
onProgress: (done, total, last) => {
console.log(`${done}/${total} done`);
}
});Returns true/false for whether the current browser can encode that format (mainly useful for checking AVIF support before showing it as an option).
npm install -g lessbytes
lessbytes <files...|dir> [options]Requires sharp. If it's missing, the CLI prints the install command needed.
Note: npx lessbytes is not supported — install it globally first.
lessbytes photo.jpg # writes photo.min.webp next to it
lessbytes *.png -o out/ # compress a batch into a folder
lessbytes ./assets -r -o build/img # compress a folder, including subfolders
lessbytes ./src/img -w # watch folder and auto-compress new assetslessbytes hero.png --format webp --quality 80 # fixed quality, no auto-search
lessbytes banner.jpg --max-size 100kb # keep file under a size limit
lessbytes huge.jpg --max-width 1920 # resize down, keep aspect ratio
lessbytes icon.png --keep-larger # save even if result is bigger
lessbytes ./photos --dry-run # simulate compression size reportRun with no arguments for a guided prompt:
lessbytes Image file or folder › photo.jpg
Output format
1 Auto (default)
2 WebP
3 AVIF
4 JPEG
5 PNG
› 1
Compression mode
1 Smart — auto quality (default)
2 Target file size
3 Fixed quality
› 1
Max width in px (blank = no limit) › 1920
Output path (blank = beside source) › build/img
| Flag | Description | Default |
|---|---|---|
| -o, --output <path> | Output file or folder | next to source, *.min.* |
| -f, --format <fmt> | auto, jpeg, webp, png, avif | webp |
| -q, --quality <1-100> | Fixed quality, skips auto mode | — |
| --max-size <size> | Size limit, e.g. 100kb, 1.5mb | — |
| --max-width <px> | Max width, keeps aspect ratio | — |
| --max-height <px> | Max height, keeps aspect ratio | — |
| -r, --recursive | Include subfolders | off |
| -w, --watch | Watch directory for changes | off |
| --dry-run | Simulate size reduction without saving | off |
| --suffix <str> | Suffix for output filename | .min |
| --keep-larger | Save even if the result is bigger | off |
| -s, --silent | Only print errors | off |
| -i, --interactive | Force interactive prompt | off |
| --logo | Print logo and exit | — |
| -h, --help | Show help | — |
| -v, --version | Show version | — |
It first adjusts quality to try to hit the limit. If that's not enough, it also shrinks the image dimensions and tries again. If the target still can't be reached, it saves the smallest version possible and marks it ⚠ over target.
lessbytes v1.2.3 compressing 1 image ✓ photo.jpg 235.8 KB → 36.9 KB -84% webp q93 Done. 235.8 KB → 36.9 KB (84.4% smaller)
If resized, it shows the scale (e.g. 60% scale). If compression didn't help, it shows kept original.
| Code | Meaning |
|---|---|
| 0 | Ran successfully |
| 1 | Bad input, no images found, sharp missing, or everything failed |
Partial failures (some images succeed, some don't) still exit 0 — check individual rows for errors.
| Back | FazBrowse Home | New Git URL |