Lapis Docs

Building a site

Images and media

In Lapis, an image is uploaded once and shown everywhere in the size the place needs. The template sets the size, and Lapis makes a variant of the image for it: in 1x and 2x, in WebP when the server can, with srcset, width, height and loading="lazy". Editors never resize an image by hand.

The place sets the size

The size attribute of an image block tells Lapis how large the image is on the page:

<x-block type="image" name="portrait" size="440x528" />          <!-- cropped around the focus point -->
<x-block type="image" name="cover" size="1200" />                <!-- width only, ratio kept -->
<x-block type="image" name="logo" size="x80" fit="contain" />
<x-block type="image" name="card" size="card" />                 <!-- a named size -->

With a width and a height, the image is cropped around its focus point. With only a width (1200) or only a height (x80), the ratio is kept. fit="contain" fits the whole image into the box instead of cropping it.

Named sizes and limits

When several places use the same size, give it a name in lapis.config.php:

// lapis.config.php
new LapisConfig(
    imageSizes: ['card' => new ImageSize(300, 200, fit: 'cover', quality: 80)],   // Lapis\Media\ImageSize
    imageMaxWidth: 2000,          // upper limit for images without a size
    uploadMaxDimension: 6000,     // larger originals are scaled down once on upload (0 = never)
    maxUploadMb: 16,              // max upload size in megabytes
    uploadExtensions: ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'svg', 'pdf'],
);

From a template

Outside a block, for example for an image from a data table, ask for a variant yourself:

$image = $cms->imageVariant($url, '800x450');   // ['src', 'srcset', 'width', 'height']

How variants work

The original file is always kept, and the page always gets a variant made for the place. When the original is smaller than the place, nothing is upscaled: the variant stays smaller, and no 2x is made.

Images without a size are encoded again too, so no EXIF or GPS data leaks from a phone photo. SVGs and the images under /assets stay unchanged.

Variants are files under storage/uploads/_variants, made the first time a page shows them. When the template's size or the image's focus point changes, a new variant is made. ./lapis media:prune removes the old ones.

Images go through Intervention Image, with GD by default. With IMAGE_DRIVER=Intervention\Image\Drivers\Imagick\Driver in .env, Imagick does the work (its memory is outside PHP's memory_limit). What Lapis writes never has metadata (a phone photo's GPS position is dropped).

Memory

GD decodes the whole image into memory, about 4.2 bytes per pixel. Before processing an image, Lapis checks that it fits in memory_limit. When it does not, the upload fails with a clear message, and pages show the original instead of a variant, because PHP's out-of-memory error cannot be caught.

A memory_limit of 128M is enough for photos of about 25 megapixels, 256M for about 57. ./lapis doctor prints the limit of the server.

Media library

The media library (toolbar → Media library) holds every uploaded file:

  • Finding files: search by name and alt text, and filter by type. Drop files on it to upload them.
  • Per file: the dimensions, the file size, the date, the alt text and a caption. When an image is picked for a block, Lapis offers its alt text.
  • Focus point: mark the most important part of an image, and every crop follows it.
  • Where it is used: the pages and blocks that show the file, drafts too, with links.
  • Replace: a new file in place of the old one, everywhere it is used.
  • Delete: when the file is in use, the library shows where first.

From the console, ./lapis media:stats shows the storage used, ./lapis media:prune [--orphans] removes stale variants, and ./lapis media:regenerate deletes every variant so that pages make them again.