DEV Community

sharefun2023
sharefun2023

Posted on

I dropped five files into seven browser image tools. Each one decoded exactly one.

Every browser image tool has the same drop zone. Drag a folder of icons onto it and the UI does something visible — a spinner, a new filename, a "Done" state. What it does not do is convert five files.

I build a set of client-side image tools (no upload, everything in canvas), so I wanted to know exactly where the "batch" expectation dies. Not from reading the code — from handing real files to the live pages and counting what the browser actually decodes.

The setup

Seven pages of my own toolset, each opened in a fresh tab (these tools keep a single workspace, so a reused tab carries the previous file over and quietly poisons the result):

  • the home page and /svg-to-png (SVG → PNG)
  • /compress-png, /compress-jpg
  • /jpg-to-webp
  • /resize-image, /image-crop

HeadlessChrome 149 via Playwright. Fixtures: five hand-written SVGs from 200×100 up to 680×340, 185–186 bytes each, and matching PNG/JPEG ladders. I instrumented URL.createObjectURL, HTMLCanvasElement.prototype.toBlob and anchor clicks before the page's own scripts ran, so every decode and every download is counted rather than inferred from the status text.

Reading 1: the file input cannot hold more than one file

Every one of the seven pages renders <input type="file"> without the multiple attribute:

page multiple accept
/ false .svg,image/svg+xml
/svg-to-png false .svg,image/svg+xml
/compress-png false image/png,.png
/compress-jpg false image/jpeg,.jpg,.jpeg
/jpg-to-webp false image/jpeg,.jpg,.jpeg
/resize-image false image/*
/image-crop false image/*

That single missing attribute decides the whole story. A non-multiple file input truncates input.files to one entry before any of your code runs. Playwright says it out loud at the boundary — Non-multiple file input can only accept single file — and that error is the finding, not a bug in the harness.

Reading 2: drop five files, one gets decoded

A drop event is not filtered by the input's multiple flag, so this is the honest test of what the drop handler does. I built a real DragEvent with five File objects and fired it at the drop zone:

  • input.files.length afterwards: 1 (browser enforced)
  • URL.createObjectURL calls before clicking convert: 0
  • after clicking convert: 2, both for the first file
  • canvas.toBlob calls: 1
  • canvas size: 200 × 100 — the dimensions of fixture #1
  • output: 2,071 bytes of image/png, named after the source file

Same pattern on the PNG side: /compress-png produced a 400 × 200 canvas and a 5,507 byte PNG from a five-file drop.

So four of the five files were never opened. No object URL, no img.src = blob:, no canvas allocation, no blob. They were dropped, listed for a moment in a status line, and discarded.

Reading 3: at least it says so

On all three pages tested, a warning line appears verbatim:

You dropped 5 files — this tool converts one at a time, so it is using the first one only.

That sentence is the most useful thing in this whole exercise, and it is rarer than you'd think. The failure mode without it is silent: you drag ten icons, get one PNG, and assume nine uploads failed somewhere. If you are writing one of these tools, print the count you ignored — it costs one line.

One caveat worth stating plainly: forcing input.files to five entries via a DataTransfer (what an extension or an automated script can do) converts one file and clears the warning, because the change listener resets the note. Same situation, quieter failure.

Why it happens

Two lines, both ordinary:

const file = input.files[0];          // change handler
const f = e.dataTransfer.files[0];    // drop handler
Enter fullscreen mode Exit fullscreen mode

files[0] is not laziness, it is a consequence of the input shape: with no multiple, there is nothing at index 1 to read. To do real batches you need multiple on the input, a for (const file of files) loop, one output per input, and some way to hand back more than one download — a zip, or showDirectoryPicker(). That is a different product, not a flag.

What it actually costs

A single small-icon conversion in this harness took 40–71 ms end to end (click → downloadable blob). Twenty icons is roughly one second of compute — and twenty drag-and-drop round trips. The cost of "one at a time" is not the CPU, it is you sitting there repeating a gesture.

Past a couple of dozen files, stop using a browser tab:

# vector source, exact pixel width
rsvg-convert -w 512 icon.svg -o icon-512.png

# Inkscape, whole folder
for f in *.svg; do inkscape "$f" --export-type=png --export-width=512; done

# raster: resize + convert in one pass
mogrify -resize 512x -path out/ *.png
Enter fullscreen mode Exit fullscreen mode

Those are one process per file with no UI in the loop, which is the actual feature the drop zone was pretending to offer.

Reproduce it in ten lines

Open any image tool, paste this in the console, then drop several files:

let n = 0;
const real = URL.createObjectURL.bind(URL);
URL.createObjectURL = b => { n++; console.log('object URL for', b.size, 'bytes'); return real(b); };
document.querySelector('input[type=file]').addEventListener('change', e => {
  console.log('files in input:', e.target.files.length);
  setTimeout(() => console.log('object URLs created:', n), 2000);
});
Enter fullscreen mode Exit fullscreen mode

If it prints 1 and then 2, the tool decoded exactly one of the files you gave it — no matter how many the drop zone appeared to accept.

The one-file path is measured in more detail on the SVG to PNG converter, and the "quality slider does nothing" result from earlier in this series came out of the same harness on the PNG compressor. Everything here runs locally in the tab; no file leaves the machine, which is also why the count above is observable at all.

Top comments (0)