Image#
Image holds an uploaded image’s storage key, image in the
descriptor. It accepts raster formats only.
Image::make('photo')->maxBytes(2_000_000), // png, jpeg, webp, gif by default; 5 MB
Uploading, the checks on each file, serving and cleaning up unused
files are on Files and images. A rich-text field’s images upload through
its imageField() (RichText). A duplicated record
starts without the image’s key (Duplicating).
It takes the common modifiers too.
Cropping#
crop() lets the user crop a picked image before it uploads. It takes
the list of aspect ratios the cropper offers, in order: a ratio (width /
height) locks the box to it, and null crops freely. With no argument
the crop is free.
Image::make('photo')->crop(), // free
Image::make('banner')->crop([16 / 9]), // locked to 16:9
Image::make('avatar')->crop([1]), // square
Image::make('picture')->crop([1, 4 / 3, null]), // square, 4:3 or free
An empty list, a ratio that is not a positive number, an entry that is
neither a number nor null, and an entry listed twice (1 and 1.0
are the same) throw InvalidArgumentException. The descriptor
publishes the list in the field’s props as
crop: {aspectRatios: list<float|null>}, in the given order, absent
when crop() is not called.
When the user picks a file the browser reports as png, jpeg or webp, the
form opens a dialog over the image with a crop box. When the field offers two or more ratios, a
row of buttons above the image names them in their order, a ratio as a
simple fraction such as 16:9 (or a short decimal when none is close)
and null as Free; the first is chosen when the dialog opens, and
choosing another starts the box over. The box starts as the largest
centred box at the chosen ratio, or the whole image when the crop is
free. Dragging it moves
it, dragging a corner resizes it, and the arrow keys move it when it has
focus (Shift moves further); mouse and touch both work, and the box
stays inside the image. Apply draws the box at the image’s full
resolution in the picked file’s format (jpeg and webp at quality 0.92)
and uploads it under the picked file’s name, through the same upload as
any other file. The cropped file is encoded anew and can be larger than
the picked one, so the size limit is checked before the dialog closes:
when the cropped file is over it, the dialog stays open and says
“The cropped image is larger than X MB. Crop a smaller area or cancel.”
Cancel, or Escape, uploads nothing and leaves the field as it was;
a click outside the dialog does not close it.
A gif or avif is uploaded as picked, without the cropper: drawing a gif
would drop its animation, and browsers cannot all encode avif.
Images added to a rich-text field through its imageField() are not
cropped either.
The crop happens in the browser and is a convenience of the form, not validation: the server accepts an upload of any dimensions, and a client of the REST API uploads whatever it sends.