skip to content

Should a real-estate listing portal's web uploader resize and re-encode an agent's 12-megapixel phone photos before uploading them on a weak uplink, and what does that cost a low-end phone?

level: seniorimportance: should knowfreq 33%

answer

  1. bytes on the uplink against work on the phone
  2. decoded pixels: width times height times four
  3. one photo at a time, off the main thread
  4. metadata does not survive a re-encode
  5. rotation must be baked into the pixels

basics

~20 s

Usually yes: a resized, re-encoded photo is a fraction of the original's bytes and leaves its location metadata behind. The cost is decode memory and CPU on the phone, so process one photo at a time in a worker.

solid answer

~50 s

On a weak uplink the bytes dominate: 80 photos of about 4 MB are 320 MB, roughly 21 minutes at 2 Mbit/s, while the same photos at a 2048-pixel longest side are closer to 40 MB, under three minutes. Re-encoding also writes a fresh file, so the camera metadata, location included, is left behind. The cost lands on the phone: decoding a 12-megapixel photo produces about 48 MB of pixels, so the page processes one photo at a time, in a worker with `createImageBitmap` and `OffscreenCanvas` so scrolling and progress stay smooth; the worker saves the main thread, not memory. Bake each photo's rotation into the pixels, never upscale, and ask the agent before sending an undecodable original as is. Hashing has the same cost profile: the digest call needs the whole input in memory, fine for a resized photo, not for the 3 GB video.

code

javascript · 19 lines
javascript
// resize-worker.js: one photo at a time, off the main thread
self.onmessage = async (event) => {
  const { id, file, maxEdge, quality } = event.data;
  let bitmap;
  try {
    bitmap = await createImageBitmap(file); // default imageOrientation "from-image" bakes rotation into pixels
    const scale = Math.min(1, maxEdge / Math.max(bitmap.width, bitmap.height)); // never upscale
    const width = Math.round(bitmap.width * scale);
    const height = Math.round(bitmap.height * scale);
    const canvas = new OffscreenCanvas(width, height);
    canvas.getContext("2d").drawImage(bitmap, 0, 0, width, height);
    const blob = await canvas.convertToBlob({ type: "image/jpeg", quality }); // a fresh file, no camera metadata
    self.postMessage({ id, ok: true, blob });
  } catch (error) {
    self.postMessage({ id, ok: false, reason: String(error) }); // the page asks the agent what to do
  } finally {
    bitmap?.close(); // free the decoded pixels before the next photo
  }
};

go deeper

for a junior

Recall why resizing helps on a weak uplink, that decoded pixels cost width times height times four bytes, and that a worker keeps heavy work off the main thread.

for a middle

Explain the decode, resize and encode steps with createImageBitmap and OffscreenCanvas in a worker, why that saves responsiveness but not memory, and why rotation must be baked in.

for a senior

Show the low-end phone judgment: one photo at a time, bitmaps closed, results streamed into the queue, a fallback for undecodable formats, and no design that hashes a multi-gigabyte file in the page.

for a principal

Weigh image quality against upload time and privacy: set a target size and quality with the listing's owners, decide when originals are allowed, and make the server strip metadata on any path that skips re-encoding.

## What resizing buys this product An agent standing in a property shoots 80 photos on a phone camera, each around 12 megapixels and about 4 MB, and uploads them over a weak mobile uplink. The listing will show them at most a couple of thousand pixels wide. Resizing in the browser before upload changes the numbers that matter: - **Bytes on the uplink.** 80 originals are about 320 MB; at 2 Mbit/s (about 0.25 MB per second) that is roughly 21 minutes. Resized to a 2048-pixel longest side and re-encoded at a sensible quality, the same set is closer to 40 MB, under three minutes. - **Privacy.** A phone photo carries metadata: often the coordinates where it was taken, the time and the device. Re-encoding from decoded pixels writes a new file that does not carry the original's metadata block, so the location is not uploaded at all. Verify that on real output rather than assume it. - **What it does not replace.** The server still produces its own display sizes and formats; that is image variants, a separate subject. The client resize exists to save the agent's uplink and keep metadata off the wire. ## What it costs a low-end phone | Step | Cost | Mitigation | |---|---|---| | Decode a 12 MP photo | about 12 million pixels times 4 bytes, roughly 48 MB, plus CPU time | one photo at a time; release the bitmap before the next | | Draw at the smaller size | CPU | in a worker, on an `OffscreenCanvas` | | Encode the new file | CPU, noticeable per photo on a slow phone | in a worker, so the page stays responsive | | Hold results | each encoded file until it is sent | hand each result straight to the upload queue | The dangerous mistake is parallelism: decoding eight photos at once on a phone with little memory can cost several hundred megabytes and get the tab killed, which also kills every upload in flight. ## Keep heavy work off the main thread `createImageBitmap` and `OffscreenCanvas` are both available in workers, so the decode, resize and encode can run there. That keeps scrolling, the progress bars and the agent's taps responsive while photos process. It is important to be precise about what the worker buys: - it moves CPU time off the main thread; - it does **not** reduce memory, since a decoded photo is the same size wherever it lives; - it adds a small cost for passing files and results between threads. How many workers to run and what they cost in general is a question about workers themselves; for this product one worker processing one photo at a time is the safe default on a phone. If the API asks for a content hash of each upload, the same reasoning applies. The browser's `SubtleCrypto.digest` does not accept streaming input: the whole input must be read into memory first. For a 600 KB resized photo in a worker that is trivial; for the 3 GB walkthrough video on a phone it is not an option, so the design must not depend on hashing large files in the page. ## Correctness traps 1. **Orientation.** Phones often store a photo sideways with a metadata flag saying how to rotate it. Because re-encoding drops that metadata, the rotation must be applied to the pixels. `createImageBitmap` applies the photo's orientation metadata by default (`imageOrientation: "from-image"`). 2. **Never upscale.** A photo already below the target size is not enlarged. 3. **Accept the odd bigger file.** Re-encoding an already small, well-compressed photo can make it slightly bigger; that is the price of dropping its metadata, so tune quality rather than fall back to the original. 4. **Formats the browser cannot decode.** Some camera formats do not decode in every browser. Fall back to asking the agent before sending the original unchanged, since that path keeps its metadata, and then the server must strip it before publishing. 5. **Quality floor.** Listing photos are zoomed and sometimes printed; agree on the target size and quality with the people who own the listing page. ## The decision - On a phone or any weak uplink: resize and re-encode by default, one photo at a time, in a worker. - On an office laptop on broadband: still strip metadata; offer an "original quality" option if the portal wants full resolution for print. - Always: show per-photo "preparing" before "uploading", so a slow phone's processing time is visible instead of looking like a stalled upload.

  • Why not let the server resize the photos after upload instead?
    The server will make its display sizes anyway, but it cannot give back the agent's uplink: the 4 MB original has already crossed the weak link by then, along with its location metadata. Client-side resizing is about bytes and privacy before sending; server-side resizing is about the variants the listing page serves. They do different jobs.
  • On a low-memory phone the tab crashes after about fifteen photos. What do you check?
    That decodes run one at a time, not in parallel; that each bitmap is closed after use; that encoded results go straight to the upload queue instead of piling up in memory; and that preview object URLs are revoked as tiles leave. The worker does not save memory, so the leak is usually in what the page keeps, not in where it runs.

  • Moving the resize into a worker also reduces its memory use
  • Decoding all the photos in parallel speeds the batch up safely
  • A re-encoded photo keeps its orientation metadata, so rotation takes care of itself
  • Hash the 3 GB video with the digest call in a worker before upload
  • Resizing on the server after upload saves the agent the same upload time