Mainstay GitHub
Content

Every size written once, at upload

A site needs each image at a few sizes: a wide one for an article's head, a small one for a card. There are two moments to make them. One is the first time a size is asked for, with a cache in front, which needs PHP on the request path wherever images are served. The other is when the file arrives. Mainstay makes them then, writes them to disk, and serves them as plain files any web server or CDN can hand out.

Sizes are declared on the field

app/Content/Article.php
#[Image(sizes: ['cover' => [1600, 900], 'card' => [640]])]
public ?Media $cover;

A width and a height crop the image to fill that shape around its focal point; a width alone scales it. Neither enlarges an image smaller than the size. A field has to name at least one size, since nothing could print an image with none, and the property is nullable because null is no image chosen; required: true is what makes every write need one.

In a view, {{ $article->cover?->picture('card') }} prints a <picture>: the AVIF as a source, the WebP as the <img>, with the alt text and the real width and height on it, loading lazily. An image that is missing prints nothing.

What happens at upload

  • The file is recognised by its bytes, not its name: JPEG, PNG, GIF and WebP. SVG is not accepted, since an SVG can carry script.

  • It is held once, by the hash of its bytes. Uploading the same file again is refused, naming the image already in the library.

  • The original is kept on a private disk and never served. A camera's file carries EXIF data, its GPS position included, and the copies are written without it.

  • Every size declared by an image field of any registered type or block is written in AVIF and in WebP, to a public disk.

The images are made with spatie/image on GD, chosen over Imagick on purpose: one engine everywhere means one set of tests proves what is written. Each copy is written in AVIF and WebP where GD writes them, and in the original's own format where it writes no WebP.

Names that cannot go stale

A copy's file name is derived from everything it is made from: the hash of the original, the size, and for a size that crops, the focal point as percentages. A cropping size is named hash-WxH-fx-fy and a width-only size hash-W. Moving the focal point writes new files under new names for the cropping sizes only. So a name that exists is a file that is right, and caches and CDNs never need invalidating.

When a declaration changes

Adding a size is a declaration and a backfill. php artisan mainstay:media:reprocess queues the copies the fields now declare and that are not written yet, so a large library is processed by a queue worker rather than in a request or a deploy. Originals are kept for good, because discarding them would make a new crop or a new size impossible.

Alt text and a focal point

An image in the library has alt text in every content language and a focal point, set in the admin's media library. Empty alt text marks an image as decorative. Both are data on the image, not on the field using it, so an image used on ten pages is described once.

An image moved to the trash, or gone, reads as an image marked missing that keeps its id, the same way a relation to a deleted entry does. A page drawing it prints nothing in its place rather than failing, and saving the entry again keeps the reference.

The admin's own grid of the library uses a 640 pixel copy of every image, written at upload beside the sizes the fields declare.

Keep reading

All articles