A library for resizing and editing images.
Demo: https://2yh02.github.io/img-toolkit
- Features
- Installation
- Usage
- Functions
- Options
- Defaults and Input Validation
- Error Handling Policy
- Quality Behavior
- Quality Comparison
- Vite Setup for WASM
- License
- Author
- β‘ Fast & Efficient: Powered by Rust and WebAssembly.
- πΈ Supports JPEG, PNG, WebP formats.
- π Resizing with automatic aspect-ratio handling.
- ποΈ Adjust brightness easily.
- π Multiple resampling filters (Nearest, Triangle, CatmullRom, Gaussian, Lanczos3).
To install the library, you can use npm or yarn:
npm install img-toolkit
yarn add img-toolkitimport {
processImage,
resize,
convertFormat,
adjustBrightness,
} from "img-toolkit";
const resized = await resize(file, { width: 800, resampling: 4 });
const brighter = await adjustBrightness(file, { brightness: 0.6 });
const converted = await convertFormat(file, { format: "webp", quality: 0.8 });
const processed = await processImage(file, {
width: 800,
height: 600,
quality: 0.8,
format: "jpg",
brightness: 0.5,
resampling: 2,
});import { resizeImage } from "img-toolkit";
const file = // your image file
const output = await resizeImage(file, {
width: 800,
height: 600,
quality: 0.8,
format: "jpg",
brightness: 0.5,
resampling: 2,
});resizeImage(file, options) is still supported in 2.x and will be removed in 3.0.0.
processImage(file: File, options: ProcessImageOptions): Promise<File>
resize(file: File, options: ResizeOnlyOptions): Promise<File>
convertFormat(file: File, options: ConvertFormatOptions): Promise<File>
adjustBrightness(file: File, options: BrightnessOptions): Promise<File>| Option | Type | Description |
|---|---|---|
width |
number | (Optional) Target width in pixels. If omitted, width is auto-adjusted. |
height |
number | (Optional) Target height in pixels. If omitted, height is auto-adjusted. |
quality |
number | (Optional) 0.0 to 1.0. Effective for JPEG and WebP output. Defaults to 0.7. Non-finite values (e.g. NaN) are sanitized to the default. |
format |
string | Output format ("jpg", "png", "webp"). |
brightness |
number | (Optional) 0.0 to 1.0. Defaults to 0.5. |
resampling |
number | (Optional) 0 to 10. Defaults to 4. |
| Option | Type | Description |
|---|---|---|
width |
number | (Optional) Target width in pixels. |
height |
number | (Optional) Target height in pixels. |
resampling |
number | (Optional) 0 to 10. Defaults to 4. |
quality |
number | (Optional) 0.0 to 1.0. Effective if source is JPEG. Defaults to 0.7. Non-finite values are sanitized to the default. |
| Option | Type | Description |
|---|---|---|
format |
string | Output format ("jpg", "png", "webp"). |
quality |
number | (Optional) 0.0 to 1.0. Effective for JPEG and WebP output. Defaults to 0.7. Non-finite values are sanitized to the default. |
| Option | Type | Description |
|---|---|---|
brightness |
number | 0.0 to 1.0. Defaults to 0.5. Non-finite values are sanitized to the default. |
quality |
number | (Optional) 0.0 to 1.0. Effective if source is JPEG. |
qualitydefaults to0.7when omitted.brightnessdefaults to0.5when omitted.resamplingdefaults to4when omitted.qualityandbrightnessare clamped to valid ranges (0.0..1.0), and non-finite values such asNaN/Infinityare sanitized to safe defaults.resamplingis clamped to0..10, and non-finite values are sanitized to default.
- User-facing errors are intentionally generic and safe for client contexts.
- Internal low-level encoder/decoder details are logged internally and are not returned directly to API consumers.
- Typical user-facing messages include:
Invalid optionsUnsupported formatImage processing failed
jpgoutput:qualityis applied.pngoutput:qualityis not applied (lossless PNG encoding).webpoutput:qualityis applied through the browser's native lossy WebP encoder.
Below is a simple visual/file-size comparison using the same source image.
| Variant | Size |
|---|---|
| Original | 747 KB |
| JavaScript Canvas API output | 49.3 KB |
Rust/WASM output (img-toolkit) |
41.3 KB |
To use img-toolkit with Vite, you must disable pre-optimization for the package to prevent WebAssembly loading issues:
import { defineConfig } from "vite";
export default defineConfig({
optimizeDeps: {
exclude: ["img-toolkit"],
},
});Without this setting, Vite may attempt to pre-bundle img-toolkit and break WASM module resolution. This ensures that WebAssembly is loaded correctly at runtime via dynamic import().
This project is licensed under the MIT License. See the LICENSE file for details.
Created by 2YH02.


