A form says "image must be exactly 1200 × 630 pixels". A marketplace wants 800 × 800. Your phone shoots pictures 4000 pixels wide and several megabytes heavy, and the usual fix — uploading the photo to a random resizer website — means handing a personal picture to a stranger's server. There is a calmer way: resize the image in your own browser, where the file never leaves your device.

We built a small tool that does exactly that, and this post is about how it works under the hood: the one canvas call that does the resizing, the three-way sync between width, height and percent that keeps the picture's shape, and the checks that keep the tool honest about what resizing can and cannot do. It runs entirely in the browser, like our earlier exact-KB image compressor, job-photo resizer and format converter — this one adds exact-size resizing, which turns out to have its own small traps.

The live tool is here: Image Resizer. The full guide version of this topic lives on our blog: Image Resizer: Resize Images Online Free by Pixels or Percent — No Upload.

Step 1: Load the file without uploading it

The file input accepts any image, and decoding is delegated to the browser — createImageBitmap first, with a plain <img> element as fallback, exactly as in the format converter:

function loadBitmap(file) { if (window.createImageBitmap) { return createImageBitmap(file).catch(function () { return loadViaImg(file); }); } return loadViaImg(file); } 

A non-image file is refused in plain language before anything else happens — "That file is not an image. Please choose a JPG, PNG, WebP or other picture file." — and the moment a picture loads, the tool shows its real dimensions and file size and fills all three size fields from them: width and height from the image itself, percent at 100. Nothing is uploaded anywhere; the decoded image exists only as a bitmap in memory.

Step 2: Three inputs, one shape — the aspect-ratio lock

This is the part of the tool that does the most quiet work. There are three ways to say how big the result should be — width in pixels, height in pixels, or a percentage — and they must never disagree with each other. Each input has a sync function, and the aspect-ratio lock decides what the other fields do:

function syncFromWidth() { if (!origW) return; var w = parseInt(wIn.value, 10); if (!w || w < 1) return; w = clampSide(w); if (lockChk.checked) hIn.value = clampSide(w * origH / origW); pctIn.value = Math.max(1, Math.round(w / origW * 100)); } function syncFromPercent() { if (!origW) return; var p = parseInt(pctIn.value, 10); if (!p || p < 1) return; if (p > 1000) p = 1000; wIn.value = clampSide(origW * p / 100); hIn.value = clampSide(origH * p / 100); } 

Type a width with the lock on and the height follows from the original ratio, so a 4000 × 3000 photo typed to 800 wide becomes 800 × 600, not 800 × 3000. Type a percentage and both sides scale from the original. The percent field always shows what the current width means relative to the original, so the three numbers tell one consistent story at all times. Unlock the lock only when you truly need a different shape — changing one side alone stretches faces and squashes logos, and it shows.

Two small guards sit underneath. Every side is clamped between 1 and 10,000 pixels (clampSide), which covers phone photos and website images while stopping a typo like 80000 from asking the browser for an absurd canvas. And percent is capped at 1000%, so "make it bigger" stays inside something the browser can actually draw.

Step 3: One canvas, one draw — the browser does the resampling

The resize itself is a canvas created at the target size, with the source image drawn into it:

var c = document.createElement("canvas"); c.width = w; c.height = h; var ctx = c.getContext("2d"); if (type === "image/jpeg") { ctx.fillStyle = "#ffffff"; ctx.fillRect(0, 0, w, h); } ctx.drawImage(sourceImg, 0, 0, w, h); 

The four-argument drawImage is the whole resize: the browser's built-in scaler resamples the picture into the new dimensions. No resampling code of our own, no library — the scaling quality is whatever the browser ships, which for a downscale is the smooth result people expect. The white fill before the draw is the same trick as in the format converter: JPG has no alpha channel, so a PNG with transparent areas would come out with black where the transparency was. Painting the canvas white first keeps logos and screenshots clean.

Encoding reuses the converter's approach — canvas.toBlob with a toDataURL fallback — and the result is checked before it is shown, because toBlob is allowed to silently give you something else:

encode(c, type, q).then(function (blob) { if (!blob || blob.type !== type) { setStatus("Sorry — your browser could not create that file. Please try again or choose the other format."); return; } // ... }); 

The quality slider is wired the same honest way as before: it defaults to 90%, and it is disabled and dimmed when PNG is selected — PNG is lossless, so a quality setting would be a placebo. A control that does nothing should not look like it does something. One difference from the converter: this tool saves as JPG or PNG only. A WebP picture can be loaded as the input, but the output choice is JPG for photos and PNG when transparency or exact pixels matter.

Step 4: Show the result before the download

Before the download button appears, the tool shows the original dimensions next to the resized ones, and the original file size next to the new one — and it says out loud which of the three cases you are in:

if (w === origW && h === origH) { noteEl.textContent = "Done — same dimensions as the original (100%). Only the format or quality changed."; } else if (w > origW || h > origH) { noteEl.textContent = "Done — enlarged to " + w + " × " + h + " px. Remember: enlarging stretches the existing pixels, so the picture will look a little softer, not sharper."; } else { noteEl.textContent = "✅ Done — resized to " + w + " × " + h + " px, " + fmtSize(blob.size) + ". Your original file was not changed."; } 

The middle branch is the important one. A resizer can stretch the pixels that exist, but it cannot invent detail that was never captured — an enlarged photo is a larger, still-soft photo. Saying so on the result itself, instead of letting the user discover it on a banner or a print, is the difference between a tool that works and a tool that misleads. The download filename carries the new size too — holiday-photo-800x600.jpg — so the resized copy never overwrites the original, and the previous result's object URL is revoked when a new resize runs.

Edge cases, honestly

  • Enlarging never adds sharpness. Going bigger is allowed — up to 1000% — but the result is softer, never sharper. For a banner or print, always start from the largest original you have. Shrinking is the safe direction: it keeps a picture looking clean and cuts the file size hard.
  • Animated pictures become still. The tool resizes the first frame of an animated GIF, not the animation.
  • EXIF orientation is not handled explicitly. Decoding is delegated to the browser, and the re-encode drops the original metadata. That is a real limitation to test against your target browsers.
  • Very large files can be slow on an older phone. The 10,000-pixel cap keeps the canvas sane, but a huge original still has to be decoded and resampled on the device — that is the price of never uploading anything.
  • Percent is relative to the original, always. Type 50% and you get half the width and half the height — a quarter of the file area. It never compounds: typing 50% twice does not give you 25% of the original, because every sync recomputes from the loaded original, not from the last result.

Privacy is a side effect of the architecture

There is no upload endpoint to trust or distrust: the code contains no network calls at all. The file is decoded locally, the preview and the download link are both blob object URLs, and the previous result's URL is revoked when a new one is generated. "Your image never leaves your device" is printed on the page — and for once it's not a policy statement, it's a description of the code path. That matters here: the pictures people resize are often personal photos, ID scans and logos, the things nobody should have to upload to a stranger's server just to change a size.

The interesting part of this tool isn't the resize — a canvas and one drawImage call do that. It's the three places it refuses to mislead: the lock that keeps width, height and percent telling one story, the note that says out loud when enlarging made the picture softer, and the size comparison that shows the before and after before you download. A resizer that tells the truth about its own output is the whole point.

Try it live: Image Resizer. The sibling tools this builds on are the exact-KB image compressor, the job-photo resizer and the image format converter.