The Image component extends the File component to provide a specialized image upload interface with preview, aspect ratio controls, and responsive sizing.
Basic Usage
<x-forms::image
name="featured_image"
width="500"
height="500"
/>
With Model Binding
<x-forms::form :model="$article">
<x-forms::image
name="featured_image"
width="800"
height="600"
/>
</x-forms::form>
When bound to a model that implements Spatie’s HasMedia interface, the component automatically retrieves and displays the image from the media library.
Image Preview Styles
{{-- Default preview --}}
<x-forms::image
name="avatar"
width="400"
height="400"
/>
{{-- Cover preview (fills container) --}}
<x-forms::image
name="banner"
width="1200"
height="400"
:cover="true"
/>
{{-- Circular avatar --}}
<x-forms::image
name="avatar"
width="200"
height="200"
:circle="true"
/>
{{-- Full width preview --}}
<x-forms::image
name="banner"
width="1920"
height="400"
:fullwidth="true"
/>
Aspect Ratio
{{-- Maintain aspect ratio (default) --}}
<x-forms::image
name="photo"
width="800"
height="600"
:maintain-aspect-ratio="true"
/>
{{-- Fixed aspect ratio --}}
<x-forms::image
name="thumbnail"
width="400"
height="400"
:aspect-ratio="1.5"
/>
{{-- Allow any aspect ratio --}}
<x-forms::image
name="photo"
width="800"
height="600"
:maintain-aspect-ratio="false"
/>
{{-- Custom collection --}}
<x-forms::image
name="avatar"
collection="profile_photos"
width="200"
height="200"
/>
{{-- Display specific conversion --}}
<x-forms::image
name="thumbnail"
collection="featured_image"
conversion="thumb"
width="150"
height="150"
/>
Model Setup
Your model should implement the HasMedia interface:
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
class Article extends Model implements HasMedia
{
use InteractsWithMedia;
public function registerMediaCollections(): void
{
$this->addMediaCollection('featured_image')
->singleFile()
->registerMediaConversions(function (Media $media) {
$this->addMediaConversion('thumb')
->width(150)
->height(150);
});
}
}
Custom Icons
{{-- Custom image placeholder icon --}}
<x-forms::image
name="photo"
width="400"
height="400"
icon="fa-camera"
/>
{{-- Custom upload icon --}}
<x-forms::image
name="photo"
width="400"
height="400"
upload-icon="fa-cloud-upload"
/>
Image Hints
The component automatically generates hints based on image dimensions:
<x-forms::image
name="banner"
width="1920"
height="400"
:show-hint="true"
/>
{{-- Displays: "Recommended 1920px x 400px. Only .jpeg, .png, .gif and .svg files of max 5MB allowed." --}}
File Type Control
{{-- All image types (default) --}}
<x-forms::image
name="photo"
width="400"
height="400"
/>
{{-- Only specific formats --}}
<x-forms::image
name="photo"
width="400"
height="400"
:mimetypes="['image/jpeg', 'image/png']"
/>
{{-- Custom extensions --}}
<x-forms::image
name="photo"
width="400"
height="400"
:extensions="['jpg', 'png']"
/>
Attributes
The name attribute for the image input
Label text for the image input. Auto-generated from name if not provided
Recommended image width in pixels. Used for display hints
Recommended image height in pixels. Used for display hints
Use cover mode for preview (fills container, may crop image)
Make preview take full width of container
Maintain the aspect ratio defined by width/height
Display preview as a circle. Automatically sets aspect ratio to 1:1
Custom aspect ratio (height/width). Overrides width/height ratio
Icon class for empty image placeholder. Framework default: fa-image (Bootstrap 5) or zmdi-image (Material Admin)
Icon class for the upload button. Framework default: fa-arrow-to-top (Bootstrap 5) or zmdi-upload (Material Admin)
type
string|array
default:"image"
File type category. Defaults to image for all image formats
Allowed image mime types. Default: image/jpeg, image/png, image/gif, image/tiff, image/svg+xml, etc.
Allowed file extensions. Automatically determined from mime types if not specified
Maximum file size in kilobytes. Uses system default for images if not specified
Spatie Media Library collection name. Defaults to the input name
Spatie Media Library conversion name to display. Example: thumb
Additional CSS classes for the file input wrapper
Model instance to bind to. Automatically retrieves image from media library if model implements HasMedia
Default image URL if no value is bound
Show automatic hint with recommended dimensions and file requirements
Mark the field as required
Ignore model accessors when retrieving value. Only retrieve from media library
Display label and input inline (horizontal layout)
Custom help text. Overrides automatic hints
Examples
Avatar Upload (Circular)
<x-forms::image
name="avatar"
label="Profile Photo"
width="200"
height="200"
:circle="true"
collection="avatars"
required
/>
Banner Image (Cover Mode)
<x-forms::image
name="banner"
label="Page Banner"
width="1920"
height="400"
:cover="true"
:fullwidth="true"
help="Upload a high-resolution banner image."
/>
Thumbnail with Conversion
<x-forms::form :model="$article">
<x-forms::image
name="thumbnail"
label="Article Thumbnail"
width="300"
height="200"
collection="featured_image"
conversion="thumb"
/>
</x-forms::form>
Square Product Image
<x-forms::image
name="product_image"
label="Product Photo"
width="600"
height="600"
:aspect-ratio="1"
:max-size="2048"
:extensions="['jpg', 'png']"
/>
<x-forms::image
name="og_image"
label="Social Media Image"
width="1200"
height="630"
help="Recommended size for Facebook and Twitter sharing."
/>
Aspect Ratio Calculation
The component automatically calculates aspect ratio from width and height:
// For width=800, height=600
aspectRatio = 600 / 800 = 0.75 (or 75%)
// For circle mode
aspectRatio = 1 (forced to 1:1)
// Custom aspect ratio overrides width/height
aspect-ratio="1.5" // Results in 150% aspect ratio
Preview Behavior
- New Image: Shows placeholder icon with “Click to select an image” text
- Existing Image: Displays the image preview with “Change” button
- After Selection: Shows selected image preview with “Remove” button
- Cover Mode: Image fills container maintaining aspect ratio (may crop)
- Circle Mode: Displays as circular preview (perfect for avatars)