Avatars
The avatar problem: arbitrary user photos in, consistent squares (or circles) out, with faces centered and files small.
The recipe
All three tabs build from a uuid, so each needs your CDN base — the prefixed host below is derived from a public key with prefixedCdnBase.
import { serializeCdnUrl } from '@uploadcare/cdn-url'
import { borderRadius, scaleCrop } from '@uploadcare/cdn-url/ops'
function avatarUrl(uuid: string, size: number): string {
return serializeCdnUrl({
cdnBase: 'https://1s4oyld5dc.ucarecd.net',
uuid,
operations: [
scaleCrop(size, size, { type: 'smart_faces_objects' }),
borderRadius('50p')
]
})
}
avatarUrl(uuid, 96)
// → …/-/scale_crop/96x96/smart_faces_objects/-/border_radius/50p/import { CdnUrl } from '@uploadcare/cdn-url/builder'
import { borderRadius, scaleCrop } from '@uploadcare/cdn-url/ops'
function avatarUrl(uuid: string, size: number): string {
return new CdnUrl({
cdnBase: 'https://1s4oyld5dc.ucarecd.net',
uuid,
operations: [
scaleCrop(size, size, { type: 'smart_faces_objects' }),
borderRadius('50p')
]
}).href
}import { cdn } from '@uploadcare/cdn-url/fluent'
import { prefixedCdnBase } from '@uploadcare/cdn-url/cdn-base'
const myCdn = cdn.base(prefixedCdnBase('demopublickey'))
function avatarUrl(uuid: string, size: number): string {
return myCdn
.file(uuid)
.scaleCrop(size, size, { type: 'smart_faces_objects' })
.borderRadius('50p').href
}import { modifiers, tinyBuild } from '@uploadcare/cdn-url/tiny'
const cdnBase = 'https://1s4oyld5dc.ucarecd.net'
function avatarUrl(uuid: string, size: number): string {
return tinyBuild({
cdnBase,
uuid,
modifiers: modifiers(
`scale_crop/${size}x${size}/smart_faces_objects`,
'border_radius/50p'
)
})
}What each piece does:
scaleCrop(size, size, { type })scales down and crops to an exact square. The smart types run content detection as a fallback chain:smart_faces_objectstries faces first, then salient objects, then sensible defaults.'smart'is the full detection chain, and narrower variants like'smart_faces'pick specific detectors.SCALE_CROP_TYPEShas the complete list. PlainscaleCrop(size, size, { align: 'center' })skips detection entirely.borderRadius('50p')does the circle crop on the CDN. Skip it if you round corners in CSS (cheaper to change later, and it works with transparent hover states).- Format negotiation (
format/auto) and adaptive quality apply automatically once the chain has a processing operation, so no extra ops are needed (Best practices).
Tighter face crops
scaleCrop smart types keep context around the face. For a tight head-and-shoulders crop, lead with an object-aware crop, then size it:
import { cropByTag, scaleCrop } from '@uploadcare/cdn-url/ops'
const ops = [
cropByTag('face', { ratio: '1:1' }), // square region around the detected face
scaleCrop(96, 96)
]Operations are a pipeline, so the scaleCrop here only resizes: the crop already chose the region.
Retina variants
Avatars are fixed-size, so density descriptors fit naturally (see Responsive images):
<img
src="{avatarUrl(uuid,"
96)}
srcset="{`${avatarUrl(uuid"
192)}
2x`}
width="96"
height="96"
alt="{name}"
/>Fallback for missing photos
scaleCrop smart types need a processable image. If the row might reference a deleted file, wrap rendering with an onerror fallback or pre-validate the uuid server-side. The URL itself is always well-formed, but the CDN returns 404 for missing files.