A local image-resize exercise should make its input and output separate. The page below reads one user-selected PNG or JPEG, draws a smaller image on a canvas and offers a new PNG download. It has no automatic folder scan and does not write back to the selected file.
This is a single-image teaching tool. It does not claim batch processing, archival fidelity, universal metadata removal or professional colour management. Those are different requirements with additional implementation and test work.
Choose explicit limits
The selected file must be no larger than five MiB and have a PNG or JPEG type. The maximum output dimension must be an integer from one to 4096. After decoding, the page rejects an image larger than sixteen million pixels.
The decoded-pixel check happens after decoding, so it is not a complete defense against memory pressure from every unusual or hostile image. Use small ordinary sample files in this exercise. A production upload or batch-processing system needs a more robust resource strategy.
Keep the aspect-ratio calculation visible
The scale factor is the smaller of one and the requested maximum divided by the larger input dimension. That prevents the page from enlarging a small source merely to reach the chosen limit. Width and height are then rounded to integer canvas dimensions, with a minimum of one pixel.
Rounding matters for tiny images: a one-pixel minimum cannot preserve every ratio exactly. The result is appropriate for demonstrating the calculation, not a claim that every thumbnail transformation preserves all original visual information.
Decode, draw and encode as separate steps
The browser decodes the selected file into an ImageBitmap, draws it onto the canvas and closes the bitmap when finished. The canvas is encoded into a PNG Blob, and an object URL connects that Blob to the download link.
A previous download URL is revoked before another attempt so the page does not keep presenting an old output as a new result. Errors hide the download link and show a rejected message. The original file remains a selected input, not an output destination.
Inspect the measured example
| Test input | Observed outcome |
|---|---|
| Generated four-by-two PNG | Decoded successfully in the isolated browser |
| Maximum dimension two | Two-by-one canvas and a PNG download link |
| Original-file hash before and after | Unchanged |
| Maximum dimension zero | Rejected; download link hidden |
The test ran in Chrome 158.0.8127.37. It verifies that small fixture and those interactions; it does not establish JPEG orientation, every colour profile or every browser’s decoding behavior. A broader image tool should add fixtures for the formats and properties it promises to support.
Understand the output change
Even a JPEG input produces a PNG output in this version. PNG file size can be larger than the original JPEG, so a smaller pixel dimension is not a guaranteed smaller download. The page does not choose a quality setting or compare alternative encodings.
Inspect the downloaded image before using it. Keep the original separately if it matters. A changed filename and a successful preview are not evidence that the output retains metadata or an exact appearance in every viewer.
The fixture was generated solely for this test; no personal photograph was needed to establish its dimensions.
Connect the experiment to a real project
Use the static-page lab to understand local serving and links. Use the storage experiment before adding saved preferences; this resize page does not persist the selected image automatically.
If you keep a small JSON manifest of test fixtures, inspect its syntax with the JSON tool and record the expected dimensions there. Do not place private image descriptions or original files into a public example bundle merely to make a test look realistic.
Complete lab files
image-resize/index.html
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Local image resize lab</title>
<label>PNG or JPEG <input id="file" type="file" accept="image/png,image/jpeg">
</label>
<label>Maximum dimension <input id="limit" type="number" min="1" max="4096" value="800">
</label>
<button id="resize">Create preview</button>
<p id="status" role="status">
</p>
<canvas id="preview">
</canvas>
<a id="download" hidden>Download new PNG</a>
<script>
let downloadURL = null;
const file = document.querySelector('#file'), limit = document.querySelector('#limit'), canvas = document.querySelector('#preview'), status = document.querySelector('#status'), link = document.querySelector('#download');
document.querySelector('#resize').onclick = async () => { link.hidden = true; if (downloadURL) {
URL.revokeObjectURL(downloadURL);
downloadURL = null;
} try {
const f = file.files[0], max = Number(limit.value);
if (!f || !['image/png', 'image/jpeg'].includes(f.type) || f.size > 5 * 1024 * 1024)
throw Error('Choose PNG/JPEG up to 5 MiB');
if (!Number.isInteger(max) || max < 1 || max > 4096)
throw Error('Maximum must be 1-4096');
const bitmap = await createImageBitmap(f);
try {
if (bitmap.width * bitmap.height > 16000000)
throw Error('Decoded image exceeds 16 million pixels');
const ratio = Math.min(1, max / Math.max(bitmap.width, bitmap.height));
canvas.width = Math.max(1, Math.round(bitmap.width * ratio));
canvas.height = Math.max(1, Math.round(bitmap.height * ratio));
canvas.getContext('2d').drawImage(bitmap, 0, 0, canvas.width, canvas.height);
}
finally {
bitmap.close();
}
const blob = await new Promise(resolve => canvas.toBlob(resolve, 'image/png'));
if (!blob)
throw Error('PNG encoding failed');
downloadURL = URL.createObjectURL(blob);
link.href = downloadURL;
link.download = 'resized.png';
link.hidden = false;
status.textContent = canvas.width + ' × ' + canvas.height;
}
catch (e) {
status.textContent = 'Rejected: ' + e.message;
} };
</script>
</html>
Run the example
python3 -m http.server 8766 --bind 127.0.0.1
# Open http://127.0.0.1:8766/image-resize/
Observed result
Fixture: 4 × 2 PNG
Maximum: 2
Preview: 2 × 1
Original bytes: unchanged
Maximum 0: rejected
The lab download contains these files and sample data. Use a disposable directory and fictional inputs. Tests described here were run locally; no hosted CI or production deployment is implied.