Pre-built code blocks

Image upload and resize

Client-side image resize with size and type guards

For when the answer to “People upload photos or images” is yes.

Rebuilt in ~11/20 projects, and it is the direct cause of store-kit's quota finding: a phone photo is 4-8MB, base64 inflates it ~33%, localStorage caps near 5MB.

Built by hand again in 11 of 20 audited projects before this existed. Each rebuild was another chance to make the mistake below.

A large picture frame falling into a funnel and emerging below as a small tidy frame, while an even larger frame bounces back off a closed gate above the funnel

Skip the rebuild

You do not write this one. It arrives in your project as ordinary source you can read, change and keep, with its audit record and its caveats beside it.

  1. Download the Playground. It is free and runs on your own machine.
  2. Start a new app and tick “People upload photos or images”.
  3. This block is written in, along with anything else you ticked.

Download the Playground See the other blocks

The mistake it removes

The naive version decodes whatever it is handed. A 100MB image is fully rasterized into memory before anyone checks it — that is a tab crash on a phone.

What is already handled

Each of these was checked by running the code, not by reading it.

What the audit found

Named rather than summarized. The reasoning behind each one ships inside the block, so it travels with the code instead of living on a page you have to trust.

What you still have to do

A copied file cannot own your secrets, your host or your legal obligations. This is the part the block deliberately does not claim.

What lands in your project

vanilla

  • vanilla/img-util.js

resizeImage(file, { maxDim, format, quality, maxInputBytes, maxOutputBytes, maxPixels }) -> { dataUrl, width, height, bytes, format }. Also exports dataUrlBytes(dataUrl). Framework-free — works as-is in React.

What you supply

Licensed MIT. It is a starting point, not a finished product.

Get it

Download the Playground See the other blocks

Nothing here is locked. The files are yours, in your folder, under a permissive license.

Questions

Is Image upload and resize audited?
It was audited on 2026-09-29, and the verdict was not a clean pass.
What do I still have to do myself?
Independent server-side validation of anything you upload. file.type is client-supplied, so the MIME allowlist here is a UX guard and not a security boundary — re-check the bytes on the server before you store or serve them.
How do I get this code?
Download the Playground, start a new app, and tick “People upload photos or images”. The block is written into your project as ordinary source you can read and edit.

All pre-built code blocks · Learn to build from zero · The coding guide