Image thumbnails
What it's for
A gallery, a product list or a profile page shows many images small. Sending each one full size is slow on a phone.
PocketBase can make thumbnails of a file field on request (?thumb=100x100); a module lets you make your own when the
file is uploaded: a fixed size, a PNG whatever came in, kept in its own field you can query and send anywhere. The
module uses the image crate, built with PNG and JPEG only.
The route
A photos collection with two file fields, image (what the user uploads) and thumb. An API route takes the
upload (a multipart form with an image field) and saves the photo with its thumbnail:
// pb_pages/photos.ts: an uploaded photo is saved with a 256 px PNG thumbnail in its thumb field
export async function post(ctx: any) {
if (!ctx.auth) throw ctx.httpError(401, "Sign in to upload")
const [file] = ctx.req.files("image")
if (!file) throw ctx.httpError(400, "No image")
const reader = file.reader.open()
let bytes: Uint8Array
try {
bytes = new Uint8Array(toBytes(reader))
} finally {
reader.close()
}
const thumb = await $wasm.run.async<{ width: number; height: number; png: string }>("thumbnail", bytes)
const photo = new Record(ctx.app.findCollectionByNameOrId("photos"))
photo.set("image", file)
photo.set("thumb", $filesystem.fileFromBytes(Array.from(Buffer.from(thumb.png, "base64")), "thumb.png"))
ctx.app.save(photo)
return ctx.res.json(201, { id: photo.id })
}
ctx.req.files gives the files uploaded with this request, ready for a record field. A Uint8Array goes to the module
as raw bytes. The module answers with JSON, {width, height, png}, the PNG as base64; $filesystem.fileFromBytes
takes the bytes as a plain array, hence Array.from.
The module
src/main.rs reads the image, scales it to fit, and writes a PNG:
let size = match std::env::args().nth(1).as_deref() {
Some("run") => 256,
Some("small") => 64,
// ...
};
let img = image::load_from_memory(&input)
.unwrap_or_else(|e| fail("bad_image", &format!("not a PNG or JPEG image: {e}")));
let thumb = if img.width() > size || img.height() > size { img.thumbnail(size, size) } else { img };
run fits the image in 256×256 and small in 64×64, kept in proportion ($wasm.call.async("thumbnail", "small", bytes) for the small one). It also takes the image as base64 text, which is how its manifest's examples pass one.
Build
cd ext/wasm/example/thumbnail
cargo build --release --target wasm32-wasip1 # rustup target add wasm32-wasip1 first
cp target/wasm32-wasip1/release/thumbnail.wasm pb_hooks/thumbnail.pb.wasm
Cost
448 KB. The first call compiles it (about 110 ms); later calls load it from the cache in about 4.5 ms. Decoding is most of the run: a 1280×853 JPEG takes about 230 ms, a 1920×1280 one about 460 ms, a 2560×1706 one about 780 ms (2026-11-18, a laptop).
Limits
- The decoded image has to fit in the call's memory, 32 MB by default (
MaxCallMemoryMB): 3 bytes a pixel, so up to about 2560×1706 with the defaults. A 4000×2666 photo fails withcrashedand a message naming the limit; raiseMaxCallMemoryMB(and the timeout) for big camera photos. - The upload is at most 4 MB (
MaxPayload). - PNG and JPEG only. WebP, GIF and others are one Cargo feature each on the
imagecrate, and make the module bigger.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.