I Built an Image Resizer That Hits Exact Pixel Sizes (Not 'Close Enough')
Every platform states its image size in pixels, and every platform states a different one. An Instagram post wants 1080 x 1080. A YouTube thumbnail wants 1280 x 720. An X header wants 1500 x 500. Upload the wrong numbers
Every platform states its image size in pixels, and every platform states a different one. An Instagram post wants 1080 x 1080. A YouTube thumbnail wants 1280 x 720. An X header wants 1500 x 500. Upload the wrong numbers and the platform does not complain - it just crops your photo itself, usually through somebody's forehead.
So I built a resizer that runs 100% in the browser: drop in a photo, tap a preset or type the exact size, and the downloaded file is exactly those pixels. It accepts JPG, PNG, WebP and GIF up to 25 MB. No server, no uploads, nothing leaves the page.
Here is what I learned building it.
Four modes, one idea: never make the user do ratio math
The tool offers Exact size, Width only, Height only, and Percentage. Three of the four exist for one reason: aspect-ratio arithmetic is where people give up. With the aspect lock on, typing a width fills in the matching height automatically:
// Width only, aspect lock on: the height is not a guess, it is the same ratio
function dimsForWidth(origW, origH, targetW) {
return { w: targetW, h: Math.round(targetW * origH / origW) };
}
A phone photo at 4032 x 3024 with the width set to 1200 comes out at 1200 x 900 - 1,080,000 pixels instead of 12,192,768, which is 91.1% fewer pixels with the same proportions and the same photo. Percentage mode is the same trick in one step: 50% of 4032 x 3024 is 2016 x 1512, and because both sides halve, the pixel count drops to exactly a quarter.
The interesting decision is what happens when the shapes disagree
Your photo is 4:3. An Instagram story is 9:16. Those shapes cannot both win, and every resizer quietly picks a winner for you. I made it an explicit choice instead, and the whole thing is one line of difference in the draw call:
if (fit === 'cover') {
// Fill and crop: scale until the frame is covered, trim the overflow
var s = Math.max(tw / origW, th / origH);
} else {
// Fit within: scale until the whole photo fits, letterbox the rest
var sc = Math.min(tw / origW, th / origH);
}
Math.max versus Math.min. That is the entire difference between the two honest options. The third option, Stretch, skips the scale factor and draws straight to the target - included because sometimes the shape change is tiny, and labelled plainly because faces get wider.
Here is what those lines do to a real photo. Take the 4032 x 3024 shot and target a story at 1080 x 1920:
| Fit | What actually happens |
|---|---|
| Fill and crop | Scales to 2560 x 1920, then trims the sides - 57.8% of the width is cut away |
| Fit within | Scales to 1080 x 810 and centres it on the 1080 x 1920 canvas - 1,110 px of empty space, nothing cropped |
| Stretch | Draws 4:3 into 9:16 - everyone in the photo gets noticeably taller |
Neither honest option is "correct". A document scan must never lose an edge (Fit within); a story post will be cropped by the platform anyway, so you may as well choose the crop yourself (Fill and crop). The tool's job is to show the trade-off, not hide it.
The presets are just the modes with the numbers filled in
| Preset | Pixels | Shape |
|---|---|---|
| Instagram post | 1080 x 1080 | Square |
| Instagram story | 1080 x 1920 | Tall, 9 by 16 |
| YouTube thumbnail | 1280 x 720 | Wide, 16 by 9 |
| X header | 1500 x 500 | Very wide, 3 by 1 |
| Pinterest pin | 1000 x 1500 | Tall, 2 by 3 |
| LinkedIn post | 1200 x 627 | Wide, about 1.91 by 1 |
A preset sets the numbers, not the composition - a square crop of a wide landscape still cuts off the sides, which is exactly what the fit choice above is for.
The file-size cap is borrowed - with one deliberate difference
The resizer also takes an optional max file size in KB, and for that I reused the engine from my exact-size compressor: a binary search over JPEG quality between 0.05 and 0.92, seven rounds, keeping the best result that fits. The pixels never change during the search.
But the fallback is deliberately different. In the compressor, when quality alone cannot reach the target, the tool shrinks the dimensions and tries again - the file size is the promise there, so pixels are negotiable. In the resizer the promise is reversed: the pixels are the whole point. So when the search cannot fit your KB limit at your exact dimensions, the tool does not shrink anything. It hands you the smallest file it achieved at exactly the pixels you asked for, and says the limit was not met. Silently breaking the one number you typed in would be the worst possible failure mode.
Two format gotchas carried over from the compressor, because they are still true:
1. PNG cannot be squeezed. PNG is lossless, so there is no quality to search - with a KB cap set, JPEG or WebP are the formats that can actually honour it. That is also why the tool defaults to "Keep original": its promise is pixels, not bytes, and re-encoding a PNG to JPEG just to satisfy a cap you did not ask about would be a nasty surprise.
2. JPEG fills transparency with white. A transparent PNG exported as JPEG gets a white background painted in before the draw, because JPEG has nowhere to put an alpha channel - and white beats the black-background surprise a naive canvas export produces.
One more honesty detail: upscaling is allowed. Set a width above the original and the tool will do it - but the new pixels are interpolated from the old ones, so the result is softer, never sharper, and the tool warns you instead of pretending otherwise.
Try it / break it
The tool is here: Image Resizer - everything runs client-side, so view-source is the full documentation.
If you find a target shape where Fill and crop trims something other than the edges, or a KB cap the search reports as met when the downloaded file is over it, I want to hear about it - the fit math and the borrowed search are exactly the kind of code that fails quietly.
This is part of a series on building small, single-purpose web tools that run entirely in the browser. Next up: reading one exact pixel out of a photo - a colour picker with an 8x loupe, and what a photo's dominant colours are really made of.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.