Photo input — the picker that offers the camera and the photo library
prompts/pkg/llms/photo-input.md on every deploy, so it
can't drift from what the platform actually does. Read it from your terminal any
time with npx vibes-diy skills.
A photo comes from <input type="file" accept="image/*"> with no capture attribute. On a
phone that one input opens the system sheet with Take Photo, Photo Library and Choose File; on a
desktop it opens the file picker. A capture attribute removes the library and opens the camera
alone, so the photo someone already took is out of reach. Hide the input and open it from a
Button in the app's own words ("Add a photo", "Snap the plant"), give it multiple when the app
takes several, and accept paste and drag-and-drop too, which is how a desktop visitor hands over a
picture.
lib/photos.js — shrink before storing
js// lib/photos.js — plain module, no React.
// Re-encoding through a canvas downscales the picture, applies the camera's rotation and drops
// the EXIF block, the phone's GPS location included, before the photo is stored or sent.
// `type` forces one output format (Instagram takes JPEG only); PNG otherwise stays PNG for its transparency.
// Resolves null for a picture this browser cannot draw: the original, location and all, is never stored.
export async function shrinkPhoto(file, { maxEdge = 1600, quality = 0.85, type } = {}) {
const source = await decode(file);
if (!source) return null;
const w = source.naturalWidth ?? source.width;
const h = source.naturalHeight ?? source.height;
const scale = Math.min(1, maxEdge / Math.max(w, h));
const canvas = document.createElement("canvas");
canvas.width = Math.round(w * scale);
canvas.height = Math.round(h * scale);
canvas.getContext("2d").drawImage(source, 0, 0, canvas.width, canvas.height);
source.close?.();
const out = type ?? (file.type === "image/png" ? "image/png" : "image/jpeg");
const blob = await new Promise((resolve) => canvas.toBlob(resolve, out, quality));
const name = file.name.replace(/\.[^.]*$/, "") + (out === "image/png" ? ".png" : ".jpg");
return new File([blob], name, { type: out, lastModified: Date.now() });
}
// createImageBitmap first; an <img> decode where it refuses (older Safari); null when both do.
async function decode(file) {
try {
return await createImageBitmap(file, { imageOrientation: "from-image" });
} catch {
const url = URL.createObjectURL(file);
const img = new Image();
img.src = url;
try {
await img.decode();
return img;
} catch {
return null;
} finally {
URL.revokeObjectURL(url);
}
}
}
PhotoPicker — pick, paste or drop, then preview with remove and replace
jsximport { Button, Icon, IconButton, Row, Text } from "@vibes.diy/ui";
import { shrinkPhoto } from "./lib/photos.js";
// Reports the chosen Files through onChange; previews them at once.
function PhotoPicker({ multiple = false, label = "Add a photo", onChange }) {
const inputRef = React.useRef(null);
const [photos, setPhotos] = React.useState([]); // [{ file, url }]
const [over, setOver] = React.useState(false);
const [problem, setProblem] = React.useState("");
React.useEffect(() => onChange?.(photos.map((p) => p.file)), [photos]);
// Object URLs hold memory until revoked.
const urls = React.useRef([]);
React.useEffect(() => () => urls.current.forEach((u) => URL.revokeObjectURL(u)), []);
const add = React.useCallback(async (files) => {
const images = [...files].filter((f) => f.type.startsWith("image/"));
if (images.length === 0) return;
const shrunk = await Promise.all(images.map((f) => shrinkPhoto(f)));
const ready = shrunk.filter(Boolean);
setProblem(ready.length < images.length ? "That picture didn't open here. Try a JPEG or PNG." : "");
if (ready.length === 0) return;
const fresh = ready.map((file) => ({ file, url: URL.createObjectURL(file) }));
urls.current.push(...fresh.map((p) => p.url));
setPhotos((now) => (multiple ? [...now, ...fresh] : fresh.slice(0, 1)));
}, [multiple]);
// Paste a screenshot or a copied image anywhere on the page.
React.useEffect(() => {
const onPaste = (e) => add(e.clipboardData?.files ?? []);
window.addEventListener("paste", onPaste);
return () => window.removeEventListener("paste", onPaste);
}, [add]);
const remove = (url) => setPhotos((now) => now.filter((p) => p.url !== url));
return (
<div
onDragOver={(e) => { e.preventDefault(); setOver(true); }}
onDragLeave={() => setOver(false)}
onDrop={(e) => { e.preventDefault(); setOver(false); add(e.dataTransfer.files); }}
className={`flex flex-col gap-2 rounded-md border-2 border-dashed p-3 ${over ? "border-primary bg-muted" : "border-border"}`}>
<Row gap={2} align="center">
<Button type="button" variant="quiet" onClick={() => inputRef.current?.click()}>
{photos.length > 0 && !multiple ? "Replace photo" : label}
</Button>
<Text muted size="sm">or drop or paste one here</Text>
</Row>
{problem && <Text size="sm">{problem}</Text>}
<input ref={inputRef} type="file" accept="image/*" multiple={multiple} hidden
onChange={(e) => { add(e.target.files); e.target.value = ""; }} />
{photos.length > 0 && (
<Row gap={2} wrap>
{photos.map((p) => (
<div key={p.url} className="relative">
<img src={p.url} alt="Your photo" className="h-24 w-24 rounded-md object-cover" />
<span className="absolute right-1 top-1">
<IconButton size="sm" icon={<Icon name="close" />} label="Remove photo" onClick={() => remove(p.url)} />
</span>
</div>
))}
</Row>
)}
</div>
);
}
Clearing e.target.value after each pick lets the person choose the same photo again. Save a
picked File as _files: { photo: file } on the record it belongs to.
When the camera is the whole point
A scanner or a photo booth may open the camera straight away: one input with
capture="environment" (the back camera) or capture="user" (the front), behind the main button.
Keep the library one tap away as a second, quieter button over a second input without capture:
jsx<Button onClick={() => cameraRef.current?.click()}>Take a photo</Button>
<Button variant="quiet" onClick={() => libraryRef.current?.click()}>Choose from library</Button>
<input ref={cameraRef} type="file" accept="image/*" capture="environment" hidden onChange={(e) => add(e.target.files)} />
<input ref={libraryRef} type="file" accept="image/*" hidden onChange={(e) => add(e.target.files)} />