Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Image Variants

A preset names a size and format an image serves at; the variant renders on its first request.

use laterite_media::{Format, Preset};

registry.add(Preset::new("card").width(600).height(400).crop());
registry.add(Preset::new("hero").width(1600).format(Format::WebP).quality(85));
<img src="/storage/media/{{ hash }}/card/{{ name }}" width="600" height="400">
BuilderDefaultMeaning
Preset::new(name)The name in the URL.
.width(px), .height(px)unboundedThe box.
.fit()yesInside the box, keeping the aspect ratio; a smaller source stays its size.
.crop()Filling the box, cropped from the centre.
.exact()The box, stretched.
`.format(Format::AutoJpegPng
.quality(1..=100)82JPEG quality.

Override a built-in preset

Built inBoxUsed by
thumb320 × 320, fitThe library grid.
preview1200 × 1200, fitThe file page.

A registered preset with a built-in name replaces it. An undeclared name is a 404. A file that is not an image serves as it is under any preset.

Keep within the limits

LimitValue
Side12 000 pixels.
Pixels50 million.

An image past either limit serves as it is. A variant renders upright, whatever orientation the camera recorded; width and height are recorded on upload.

Render from code

let prepared = laterite_media::image::prepare(bytes);
let rendered = laterite_media::image::render(&bytes, &preset, prepared.orientation)?;
let made = variants.ensure(&db, disk.as_ref(), &record, &preset).await?;

Crop an image

POST /admin/media/<id>/crop   x=100&y=50&w=200&h=100&mode=new
FieldMeaning
x, y, w, hThe area kept, in the image’s own pixels, clamped to it.
modenew (the default): a file named <stem>-crop.<ext> in the same folder. replace: this file changes in place and its variants are dropped.

The format is kept (PNG, JPEG, WebP; anything else becomes PNG). The file page offers the dialog on an image to a user with media.manage.

let out = laterite_media::image::crop(&bytes, Rect { x, y, width, height }, orientation)?;
laterite_media::record::replace_blob(&db, id, &hash, size, &content_type, width, height).await?;