Lenses and transforms
Resize and convert retained images and previews through the CDN's rendered and preview resources, with reusable named lenses or one-off transforms, without creating new asset records. View and change your project's lenses in Portal › Lenses.
Choose a transform
| Need | Use |
|---|---|
| A reusable image size or preset, such as a card | A named lens. Change its definition in one place and every URL that names it follows. |
| A one-off size or format | A direct transform selector in the URL. |
Lenses and transforms apply to rendered when it serves an image, and to preview, the still image of an image, video, audio, document, presentation or website. On rendered PDFs, video and audio they return 400 transform/invalid, and /original always serves the uploaded file as it is. Images without an explicit format are served in the best format each browser accepts.
Named lenses
Use a named lens by appending /@:lensIdentifier to a rendered or preview URL, such as a card lens you define in code. The @ prefix makes the selector unambiguously a named lens.
/assets/:projectId/:asset/rendered/@card
/assets/:projectId/:asset/preview/@cardWith the built-in thumbnail lens:
const response = await client.cdn.serve({
assetId,
lens: "thumbnail",
});
For a private asset, append the same selector to its signed asset URL, such as https://cdn.calmlens.com/v1/assets/{projectId}/sig_…/rendered/@thumbnail. Lenses and transforms aren't part of the signature, so one signed asset URL serves every lens.
Built-in lenses
Every project has these lenses. Each scales the image down to a fixed width, keeps its aspect ratio, never enlarges it and sets no format, so the CDN serves the best format each browser accepts.
| Lens | Transform |
|---|---|
thumbnail | fit=scale-down,width=128 |
sm | fit=scale-down,width=640 |
md | fit=scale-down,width=768 |
lg | fit=scale-down,width=1024 |
xl | fit=scale-down,width=1280 |
2xl | fit=scale-down,width=1536 |
These identifiers are reserved: the built-in lens always takes precedence, and ensuring or creating a project lens with one of them returns 409 lens/identifier-reserved. Give your own lenses other identifiers, such as card or avatar. To change a built-in's output for one URL, add options after it, such as @thumbnail,format=png.
Direct transforms
For one-off derived assets, append transform options after /rendered or /preview as one path segment of comma-separated key=value pairs. The CDN applies the options together in one pass, in a fixed order whatever order you write them in: it trims, then rotates and flips, then resizes and crops, then adjusts colour and blur, then adds the border, and converts the format last. A / inside the transform returns 400 transform/invalid.
/assets/:projectId/:asset/rendered/blur=12,fit=cover,width=1024Pick a lens or your own options, and make the asset private to see its signed URL. The demo shows the URL and the resulting image size, from a 1920 × 1280 source.
Lens
The SDKs accept either the options string or an options object in the same shape as a lens's transform, such as { format: "png", width: 1024 }. An options object is converted to the selector for you, with keys sorted and values encoded, so nested options such as border and gravity can be used without writing the selector by hand.
const response = await client.cdn.serve({
assetId,
transform: { format: "png", width: 1024 },
// or the selector string: transform: "format=png,width=1024"
});
Combine a lens with extra options by setting both lens and transform in the SDK. In a URL, put the options after the lens in the same segment: /rendered/@card,width=64. Options in the URL replace the lens's options of the same name, and the result applies in one pass. This also works for signed URLs; changing either part of a signed URL invalidates its signature.
Direct transforms are not saved as lenses. Use a custom lens when a transform should have a stable project-owned name and cache invalidation on definition changes.
Image transform options
Options are comma-separated key=value pairs. Write nested options with a dot, such as border.width=2,border.color=ff0000, and colours as hex without #. A key on its own means true.
| Option | Accepted values |
|---|---|
format | jpg, png, webp, avif, or gif. Leave it out to serve the best format each browser accepts. |
width, height | Integers from 1 through 10000, after dpr. With a size and no fit, the image scales down and is never enlarged. |
dpr | Pixel density from 1 through 3, such as 2 for high-density screens. Multiplies width, height and the border width. |
fit | scale-down, contain, pad, squeeze, cover, or crop |
quality | Integer from 1 through 100, or low, medium or high. Lossy formats default to high. |
anim | false serves the first frame of an animated GIF or WebP as a still image. |
rotate | 0, 90, 180, or 270 |
flip | h, v, or hv |
redact | redact.text or redact.personalInformation blurs the text or personal information OCR found when the image was uploaded, before the other options apply. See Text regions and redaction. |
| Other adjustments | background, blur, brightness, contrast, gamma, saturation, sharpen, gravity, border, segment, and trim |
redact.faces blurs the faces that face detection found when the image was uploaded. See Blurring faces.
Rendered images are served without EXIF, XMP or IPTC metadata.
Malformed selectors return 400 transform/invalid. Unsupported options return 400 transform-options/invalid.
Custom lenses
Define lenses in code when a derived asset needs a stable URL and predictable cache invalidation. Create or update a project lens by its identifier with the SDK's client.ensureLens() method:
const normalLens = await client.ensureLens({
identifier: "picmagic-normal",
name: "PicMagic normal",
type: "image",
active: true,
transform: { fit: "scale-down", width: 1024, format: "png" },
});
const response = await client.cdn.serve({
assetId,
resource: "rendered",
lens: normalLens.identifier,
});
Ensuring a lens creates it if the project has no lens with that identifier, changes it to the definition you send if it differs, and returns it unchanged otherwise, so it is safe to run on every deploy. Lens identifiers contain lowercase letters, numbers, and hyphens and are at most 50 characters, and can't be one of the built-in lenses.
When a lens's definition changes, the CDN doesn't serve files made with the earlier definition. Each CDN location keeps a lens definition for up to two minutes, so after you update, deactivate or delete a lens, other locations can serve the previous definition for up to about two minutes. SDK lens URLs use the required @ prefix automatically.
To ensure multiple lenses in one request, use client.ensureLenses():
const [normalLens, uhdLens] = await client.ensureLenses({
lenses: [normalDefinition, uhdDefinition],
});
PUT / ensures one definition or several in one request, up to 100 per request; the SDKs split larger batches into consecutive requests for you.
To manage lenses one at a time, by ID:
| Task | Request |
|---|---|
| List the project's lenses, newest first | GET / |
| Create a lens whose identifier is new to the project | POST / |
| Get a lens | GET / |
| Change a lens's name, description, type, transform or whether the CDN serves it | PATCH / |
| Delete a lens | DELETE / |
Creating a lens with an identifier the project already uses returns 409 lens/identifier-taken, and a lens ID that doesn't belong to the project returns 404 lens/not-found. The built-in lenses aren't listed and can't be changed. The Lenses reference documents the lens object and the request schemas.
Variants and billing
Each requested variant is stored and reused, and billed once as an image transform on its first billable request; Pricing explains what makes a variant. Changing the settings creates a new variant, while reordering option keys or using a lens with the same settings reuses the existing one. Generated variants remain stored until the asset's content is removed or the asset is deleted, and they follow the source asset's current access and moderation rules.