Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

How I added AI background removal to a Laravel site without uploading a single photo

I run Prestofiles, a small set of free image and PDF tools (compress an image to 50KB, make a passport size photo, merge PDFs, and so on). The site is plain Laravel + Bootstrap + vanilla JS, and one rule from day one was

I run Prestofiles, a small set of free image and PDF tools (compress an image to 50KB, make a passport size photo, merge PDFs, and so on). The site is plain Laravel + Bootstrap + vanilla JS, and one rule from day one was: files never leave the visitor's device. Every tool runs in the browser.

That rule got interesting when I wanted to add a background remover and a "change photo background to white or blue" option for passport photos. Most background removers send your photo to a server with a GPU. I wanted the same result on a cheap shared host, with zero uploads. Here is what worked, and what didn't.

1. Picking a model you can actually ship

I tested several open models in the browser with real photos (people, products, dark hair on a dark background, arms with gaps):

  • MediaPipe selfie segmentation: tiny and very fast, but edges are soft and it only knows people.
  • u2netp / MODNet: small, but failed badly on some photos (a full-body photo came out almost empty).
  • IS-Net general use (Apache-2.0): clearly the best edges and works on objects too, but the fp32 file is about 170 MB.

I went with IS-Net and made it smaller.

2. Shrinking the model without wrecking quality

Full int8 quantization (weights and activations) destroyed the masks: IoU against the fp32 output dropped to about 0.65. What worked was weight-only int8 with DequantizeLinear nodes, so the math still runs in float:

  • file size: ~170 MB → ~45 MB
  • IoU vs the original: 0.999

Two more graph fixes made it browser friendly:

  • The exported graph had fixed-size Resize nodes, so only 1024×1024 input worked. Replacing the constant sizes with Shape() slices made the input size dynamic.
  • WebGPU in onnxruntime-web refused MaxPool with ceil_mode=1. Setting ceil_mode=0 gave identical output for the sizes I use, and WebGPU started working.

I also cut the 12 unused side outputs, which saved memory.

3. Running it without freezing the page

Everything heavy runs in a Web Worker:

  • onnxruntime-web with WebGPU when available, falling back to WASM
  • 1024 px input on desktop GPUs, 768 px everywhere else (640 and 512 lost detail on hair and thin objects in my tests)
  • the model is cached with the Cache Storage API, so it downloads once

The main thread only draws the preview. The longest main-thread task while processing is about 0.1 s.

4. The part nobody talks about: post-processing

The raw mask was good, but the results still looked "AI-ish". Four small steps in the worker fixed most of it:

  1. Guided filter – snaps the mask to the real edges of the photo, no stair steps.
  2. Edge colour decontamination – edge pixels still carry the old background colour (a light halo on a blue background). Estimating the true foreground colour from nearby solid pixels removes the halo.
  3. Solidify – the model was sometimes only 60% sure in the middle of a face or a plain shirt, so the new background showed through and faces looked washed out. Filling the interior to fully opaque fixed it.
  4. Dehaze – faint, half-included areas (a shadow, a water streak) that don't touch the solid subject are removed.

5. Phones

Phones were the hard part. A 48 MP camera photo plus a 45 MB model can crash a mobile tab ("Aw, Snap!"). What helped:

  • shrink big photos once to 2000 px before anything else
  • refine the edges once and reuse it for both preview and download
  • turn off onnxruntime's memory pattern (enableMemPattern: false) – about 190 MB less peak memory, same speed
  • if the tab crashed during processing, remember it (a flag in localStorage) and use the light MediaPipe model on that device next time

Result

The tool is live here: change photo background, and the same engine powers the white/blue background option in the passport photo maker. No signup, no upload.

If you have tried running segmentation models in the browser, I'd love to hear which model and tricks worked for you.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.