imgtowebp
v0.0.2
Published
Convert images to WebP in browser and Node.js.
Readme
imgtowebp
Convert images to WebP in both browsers (Canvas/OffscreenCanvas) and Node.js (via sharp), with optional downscaling and an optional “try to hit a target size” quality search.
What this is for
- Shrink images for the web: turn PNG/JPEG/etc into WebP, optionally downscale, and (optionally) try to keep output under a byte budget.
- Same API across environments: import
imageToWebp()from the browser entry or the Node entry.
Install
npm i imgtowebpQuick start
Browser
import { imageToWebp } from "imgtowebp"
const res = await imageToWebp(fileOrBlobOrUrl, {
maxWidth: 2048,
maxHeight: 2048,
targetBytes: 300_000,
maxQuality: 0.82,
minQuality: 0.45,
returnFile: true,
})
console.log(res.isWebp, res.width, res.height, res.quality, res.blob.size)- Input types (browser):
Blob | File | string- If
string, it is fetched as a URL.
- If
- Output: always includes
blob. IfreturnFile: trueand the environment hasFile, it also includesfile.
Node.js
import { imageToWebp } from "imgtowebp/node"
import { readFile } from "node:fs/promises"
const bytes = await readFile("input.png")
const res = await imageToWebp(bytes, { targetBytes: 250_000 })
const webpArrayBuffer = await res.blob.arrayBuffer()
console.log(res.isWebp, res.width, res.height, res.quality, webpArrayBuffer.byteLength)- Input types (node):
Buffer | Uint8Array | ArrayBuffer | string- If
stringstarts withhttp://orhttps://, it is fetched. - Otherwise it is treated as a local file path.
- If
- Node requirement: Node.js 18+ (uses global
fetch/Blob).
API
imageToWebp(input, options?)
Options
maxWidth/maxHeight: hard cap for output dimensions (keeps aspect ratio). Default2048.targetBytes: if set, tries to encode to (\le) this size by lowering quality (binary search within the range).maxQuality/minQuality: quality range, from0to1. Defaults0.82/0.45.returnFile: iftrue, also returnsfilewhenFileexists. Defaultfalse.fileName: output file name (only used when returning aFile).force: when WebP encoding isn’t supported in the browser, controls whether the original input can be returned instead of WebP. (In Node, WebP is always produced becausesharpencodes it.)
Result
blob: outputBlob(normallytype === "image/webp").file?: only present whenreturnFile: trueandFileexists.width/height: output dimensions.quality: selected quality in the0..1range.isWebp: whether the output is actually WebP.
Notes and limitations
- Browser WebP support varies: the browser build relies on Canvas/OffscreenCanvas WebP encoding support. When encoding isn’t available, the function can return the original input and set
isWebp: false. - Metadata: EXIF/IPTC metadata is not preserved.
- Not a perfect “max bytes” guarantee:
targetBytesis a best-effort search within the provided quality range.
Import paths
- Browser:
import { imageToWebp } from "imgtowebp" - Node:
import { imageToWebp } from "imgtowebp/node"
Build (contributors)
npm run build
npm run typecheck
npm testLicense
MIT. See LICENSE.
