Libraries
UnxPhoto
A photo uploader you can drop into any UNX app with one script tag. People pick, drop or take a picture; it is checked, cropped if you want, shrunk in the browser, uploaded with a progress bar, and you get its address back. No dependencies, about 19 KB.
Try it
Signed-in visitors can upload a picture to their own UNX Files. Nothing changes on your account unless you press the button under the result.
Checking your session…
Install
<script src="https://developer.unx.ng/libs/unx-photo.js"></script>
<!-- or download it and host it with your app: https://developer.unx.ng/libs/unx-photo.js -->
<!-- TypeScript types: https://developer.unx.ng/libs/unx-photo.d.ts -->It adds a global called UnxPhoto with three functions: field, attach and upload. Its styles load by themselves.
Quick start
Upgrade a text field that holds a picture address. The default settings send to UNX Files on the same site.
<!-- A normal text field that holds a picture address -->
<label>Logo <input id="logo" name="logo" value=""></label>
<script>
// Adds a drop zone and preview above the field. When an upload finishes, the field holds the new address
// and gets an "input" and "change" event, so your form code does not change.
UnxPhoto.field(document.getElementById("logo"), {
aspect: 1, // square crop with a move-and-zoom step
shape: "circle",
camera: true,
query: { purpose: "avatar", visibility: "public" }, // UNX Files options
});
</script>UNX Files accepts JPEG, PNG, WebP and GIF pictures up to 5 MB. With purpose: "avatar" the file is always public and its address works on any site. Set a profile picture with POST /v1/auth/update-user { image: url }.
Use it with another API
OCI School Connect uses it for school logos and hero pictures, sending to its own upload route with a bearer token.
// Any API that takes the picture as the request body and answers { url }
UnxPhoto.field(document.querySelector("[name=logo]"), {
endpoint: "https://api.example.com/v1/admin/uploads",
token: () => session.token, // sent as Authorization: Bearer …
credentials: "omit",
aspect: 16 / 9,
shape: "wide",
maxSize: 1600,
// result: (json) => json.data.link, // if the address is somewhere else in the answer
});Options
Every option is optional. Pass them to field, attach or upload.
| Option | Default | What it does |
|---|---|---|
endpoint | /v1/files/uploads | Where to send the picture. The default is UNX Files on the same site, using the signed-in session cookie. |
mode | "raw" | "raw": the picture is the request body and its name goes in ?name=. "multipart": a FormData field called file. |
query | {} | Extra query values, for UNX Files { purpose: "avatar", visibility: "public" }. |
token | none | A bearer token, or a function that returns one. Sent as Authorization: Bearer …. Use it with credentials: "omit" for other APIs. |
headers | {} | Extra request headers. |
credentials | "include" | "include", "same-origin" or "omit". |
result | json => json.url | A function that picks the picture's address out of the response. |
accept | JPEG, PNG, WebP, GIF | Allowed types. Anything else is refused before it is sent. |
maxBytes | 5 MB | Size limit after shrinking. The library lowers the quality, then the size, until it fits. |
maxSize | 1600 | Longest side in pixels after shrinking. |
quality | 0.86 | JPEG quality, 0.5 to 1. |
aspect | none | Width divided by height to crop to: 1 for square, 16 / 9 for wide. Leave out to keep the picture as it is. |
crop | true | With aspect set, show a move-and-zoom cropper before uploading. false crops the centre automatically. |
shape | "square" | How the preview looks: "circle", "square" or "wide". |
camera | false | Add a Take photo button that opens the camera on phones. |
label, hint | none | The drop zone's accessible name and the small text under it. |
strings | {} | Replace any message, for another language: { choose, change, camera, remove, drop, notImage, tooBig, failed, uploading, done, crop, zoom, save, cancel, drag }. |
value | none | attach() only: a picture address to show at the start. |
Callbacks and events
UnxPhoto.attach(document.getElementById("avatar"), {
onStart: (file) => console.log("uploading", file.name),
onProgress: (percent) => console.log(percent + "%"),
onUploaded: (result) => save(result.url),
onError: (error) => console.warn(error.code, error.message), // not_image, too_big, network, http_413 …
onRemove: () => save(""),
});
// The same moments as DOM events (they bubble):
el.addEventListener("unxphoto:uploaded", (e) => console.log(e.detail.url));
el.addEventListener("unxphoto:error", (e) => console.warn(e.detail.message));Upload without the widget
// No widget: process one File and upload it
const result = await UnxPhoto.upload(file, {
endpoint: "/v1/files/uploads",
query: { purpose: "avatar", visibility: "public" },
maxSize: 800,
});
console.log(result.url);Pass an aspect to crop; with crop: false the centre is used and no dialog appears.
Styling
/* Colours and corners come from CSS variables; the classes are all prefixed .unxp- */
.unxp { --unxp-accent: #0a9f4f; --unxp-radius: 16px; }
.unxp-drop { min-height: 180px; }The widget inherits your page's font and text colour, and uses --accent from the page when --unxp-accent is not set.
Good to know
- Pictures are checked in the browser (type, then size after shrinking) and again by the server. Never rely on the browser alone.
- A PNG stays a PNG, so logos keep their transparency. Other pictures become JPEG. Animated GIFs lose their animation when resized.
- The picture is shrunk before it leaves the phone, so a large phone photo usually uploads many times smaller.
- The library never writes the address into the page as HTML, only as an image source and an input value.
- It does not store anything in the browser. Tokens you pass stay in your code.