brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: capture="environment" on a file input opens the phone's back camera

Web-Development
~/tips/tip-input-capture-environment-camera

accept="image/*" capture="environment">

An upload button on a phone goes straight to the camera instead of the file picker.

For a form where people photograph a receipt or a meter reading, the usual file picker is one step too many. The capture attribute asks a phone to open the camera directly.

<label for="receipt">Photo of the receipt</label>
<input type="file" id="receipt" name="receipt"
       accept="image/*" capture="environment">

accept="image/*" says the input wants an image, and capture says it should be a new one taken with the camera. environment asks for the camera facing away from the user, user for the selfie camera. With accept="video/*" the phone records video instead.

The result is an ordinary file in the input, so the form and your server code do not change.

The caveats are about who gets it. MDN lists support for Chrome on Android and Safari on iOS, not for desktop browsers, which ignore the attribute and show the normal file dialog. That fallback is fine. On a phone, though, capture can take away the choice to pick an existing photo from the gallery, depending on the browser. If people might already have the picture, I’d leave capture off. Without it, phones usually offer both the camera and the library in their menu.

Further reading: MDN: capture attribute

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link