Avatar upload

A profile photo or a logo: the current avatar, Change and Remove, and a crop dialog that makes whatever was chosen a square of a known size.

Avatar upload

Needs Alpine and the package's script. See getting started. Without them it is a plain file input that posts the original photo, and a Remove checkbox.

Use it

<form method="post" action="{{ route('profile.photo') }}" enctype="multipart/form-data">
    @csrf
    <x-ds::avatar-upload name="photo" :src="$user->photo_url" :alt="$user->name"
                         :maxSize="2 * 1024 * 1024" :error="$errors->first('photo')" />
    <x-ds::button type="submit">Save</x-ds::button>
</form>
{{-- posts photo (a 512 × 512 JPEG), or photo_remove=1 --}}
$request->validate([
    'photo' => ['nullable', 'image', 'mimes:jpeg,png,webp', 'max:2048'],
]);

if ($request->boolean('photo_remove')) {
    $user->deletePhoto();
} elseif ($request->hasFile('photo')) {
    $user->updatePhoto($request->file('photo')); // already cropped, with the script
}

It does not upload anything. The crop goes into the real file input, so your form posts it, or Livewire uploads it:

<x-ds::avatar-upload name="photo" wire:model="photo" :alt="$name"
                     :src="$photo?->temporaryUrl() ?? $user->photo_url" />

Without the script the reader's original file posts instead, so crop on the server too (or check the dimensions).

A company or a project takes a square:

<x-ds::avatar-upload name="logo" label="Logo" shape="square" :src="$team->logo_url"
                     :alt="$team->name" outputType="image/png" :outputSize="256" />

Props

PropTypeDefaultWhat it does
namestringrequiredThe file field's name.
labelstringPhotoNames the control, beside the avatar.
srcstring—The current photo's URL.
altstring—Who or what is pictured: the photo's alt text, and the initials without one.
shapecircle | squarecircleThe crop's mask and the avatar's shape.
acceptstringimage/jpeg,image/png,image/webpAs on a file input; checked in the browser.
maxSizeint—Bytes.
outputSizeint512The crop's width and height in pixels.
outputTypestringimage/jpegimage/jpeg, image/png or image/webp.
qualityfloat0.90 to 1, for JPEG and WebP.
rotatebooltrueA Rotate button in the crop dialog.
removablebooltrueA Remove button while there is a photo.
removeNamestring{name}_removeWhat Remove posts as 1.
uploadingboolfalseYour upload, in progress: an overlay on the avatar.
progressint—With uploading: 0 to 100, or none for a spinner.
sizelg | xlxl48 or 64px.
help / error / disabledAs on every field.

Events

crop fires from the component after Apply, with { blob, width, height }, for a host that wants the image itself (to show it elsewhere, say). The file input dispatches input and change as it would for a chosen file.

<x-ds::avatar-upload name="photo" x-on:crop="preview = URL.createObjectURL($event.detail.blob)" />

Keyboard

In the crop area: the arrow keys move the photo (Shift: further), + and − zoom. The zoom slider is a native slider. Nothing needs a drag.

Accessibility

The component is a group named by its label. The crop dialog is ds::modal: it traps focus and gives it back to Change. The crop area is a focusable group with its instructions as its description. Errors are said in a polite live region.

Don't

  • Don't trust the browser's checks. Validate type, size and dimensions on the server; without the script nothing was cropped or checked.
  • Don't upload from crop and post the form as well.
  • Don't use it for documents or several files — that is file upload.

More in specs/avatar-upload.md.


From the package repo: edit this page on GitHub.

Was this page helpful?