Inspiration

PhotoCraft is a Rust photo editor developed in the open on GitHub. Its JPEG Options dialog let people choose a Quality from 0 to 12 and a format, but gave no hint of the resulting file size, so getting a photo under a size limit meant saving, checking and saving again. Stargazer7780 opened issue #7843 wanting both manual quality control and a rough size figure. Adjustable quality had arrived with the dialog in #7883; the size preview was still listed as missing in the project's docs.

What it does

With storytold/photocraft#9952, merged by davidmann4 on 12 November 2026, the dialog shows the size of the file it's about to write, in dim text left of OK and Cancel ("152.3K", "1.5M"), with "Estimated file size" on hover. It follows the Quality slider and the format choice as you change them. Documents over 40 megapixels on desktop, or 4 megapixels in the web build, show a dash.

How I built it

The readout doesn't estimate. It encodes the document to JPEG bytes in memory through the same photocraft_io::export call and settings the save uses, then counts them:

let bytes = photocraft_io::export(doc, "size.jpg", &settings.io_options()).ok()?.bytes;

That way the colour profile, metadata and even a reopened file's exact original quality all count. On desktop the encode runs on a worker thread, following the pattern Export As already uses for its exact size, and asks egui (the UI library) to redraw when finished. The web build has no threads, so it encodes within the frame. A cache keyed on document, revision, quality and format means unchanged settings never encode twice, and a single encode slot stops work piling up. Everything sits in a new size_readout.rs, with the dialog gaining one line before it's drawn and one in its button row.

Challenges I ran into

Keeping the slider smooth while doing real work on every change: the cache, the one encode rule and the pixel limits handle that.

Formatting: rounding has to happen before choosing K or M, or a file just under a megabyte reads "1024.0K" instead of "1.0M".

Staying mergeable: the PR was open from 28 October to 12 November while main kept editing the same doc status lines, 21 translation catalogs and the contributors file. The branch took in main 20 times, 11 merges by me and 9 by davidmann4. When the catalogs moved to storytold/craft-i18n, my 21 rows went with the deleted files, so the tooltip is English until that repo gets the row.

Accomplishments that I'm proud of

A size readout that equals the bytes the save writes, which the tests check directly, along with a test that opens the real dialog through kittest and reads the label. davidmann4 called it "a really nice addition to JPEG Options".

What I learned

CodeRabbit's three minor review notes all held up: the docs overstated where the encode runs, one cached size wasn't enough, and an old size could pass for a new one. Keeping the last 8 sizes and marking a pending one with "…" fixed the last two, and a docs edit fixed the first. I also learned where merge conflicts come from on a fast moving repo: apart from one doc comment, every conflict on this PR was in a shared list.

What's next

A translation row for "Estimated file size" in storytold/craft-i18n, so the tooltip can show in every language the app supports.

The full story, with all the code, is on my blog: https://ashokramsi.hashnode.dev/photocraft-jpeg-file-size

Built With

Share this project:

Updates