Getting started
Library
Components
Integrations
Media Library Item-driver
The MediaLibraryItemDriver allows you to combine the best of Spatie Media Library with the flexibility of re-using media items across models. Furthermore, storing media items is as easy as storing just a single ID.
Installation
Publish migrations
First, publish the migrations of the Media Library package:
php artisan vendor:publish --tag="filament-media-library-migrations"
Install Spatie Media Library package
Next, you need to install the Spatie Media Library package and follow the instructions in the Spatie Media Library documentation:
composer require spatie/laravel-medialibrary
Next, publish and run the package migrations:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations"
Review disk
The Spatie Media Library stores all media on a disk. By default, this is the public disk. It is recommended to verify the disk before storing any media:
MEDIA_DISK=s3
That's it! You now have a fully functional Media Library running Spatie Medialibrary as a driver.
Conversions
As the Media Library Item Driver is built on top of Spatie Medialibrary, it fully supports all of Spatie Medialibrary's features, including image conversions.
By default, all images in the Media Library are displayed using Glide (if you have the gd or imageick extension enabled). This is very convenient, as Glide can generate all images from the Media Library into a perfect size on-the-fly, without the need to create and store multiple versions of the same image. However, in some cases you might want to create and store multiple versions of the same image, for example when you need to perform more complex image manipulations or prefer to store it, you can use Spatie Medialibrary's conversions for that.
Enabling conversions
Conversions are opt-in. To enable support, call the driver ->conversions() method:
$driver->conversions()
Working with media conversions
By default, the plugin registers four media conversions:
| Conversion | Name in Spatie | Default size | Queued | Enabled |
|---|---|---|---|---|
| Responsive | `responsive` | Spatie responsive images | Yes | Yes |
| Medium | `800` | 800px wide | Yes | Yes |
| Small | `400` | 400px wide | Yes | Yes |
| Thumb | `thumb` | 600 × 600 (cropped) | No | Yes |
By default, the thumb conversion runs synchronously on upload so a preview is always immediately available; the other conversions are queued.
In case your media conversions are not being generated (or only the thumb conversion), please ensure you have correctly set up a queue. The thumb conversion will be generated directly when the user uploads an image (to always have one conversion available), whereas the other conversions will be generated in the background on the queue in order to not unnecessarily slow down the upload so much. See also here.
Modifying media conversions
You can modify these conversions using the following methods:
use Spatie\MediaLibrary\Conversions\Conversion;
$driver
->conversionResponsive(enabled: true, modifyUsing: function (Conversion $conversion) {
// Apply any modifications you want to the conversion, or omit to use defaults...
return $conversion->keepOriginalImageFormat();
})
->conversionMedium(enabled: true, width: 800)
->conversionSmall(enabled: true, width: 400)
->conversionThumb(enabled: true, width: 600, height: 600)
Modifying every conversion at once
If you want to apply the same modification to every built-in conversion (responsive, small, medium, thumb), you can use ->modifyConversionsUsing(). The callback receives each conversion in turn and is applied on top of the per-conversion modifyUsing: callbacks:
use Spatie\MediaLibrary\Conversions\Conversion;
$driver
->modifyConversionsUsing(function (Conversion $conversion) {
$conversion->keepOriginalImageFormat();
});
This is convenient when you have a setting that applies across the board – like keeping the original image format, queueing all conversions on the same queue, or applying a watermark.
Customizing the Spatie media collection
By default, every MediaLibraryItem registers a Spatie media collection named media. If you need to customize that collection per item – for example to apply single file collection behaviour or to register accept-callbacks – use ->mediaCollection():
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
$driver
->mediaCollection(function (MediaCollection $mediaCollection) {
$mediaCollection->singleFile();
});
The callback receives the Spatie MediaCollection instance and is invoked when registering the collection on the MediaLibraryItem.
Registering custom media conversions
If you want to register additional media conversions, you can do so using the ->registerConversions() method:
$driver
->registerConversions(function (MediaLibraryItem $mediaLibraryItem, Media $media = null) {
$mediaLibraryItem
->addMediaConversion('test');
// ..
})
You are free to register as many conversions as you like. However, be aware that each new conversion takes up more disk space.
Custom models
If you want to use custom Eloquent models for the Media Library item or folder, configure them on the MediaLibraryItemDriver inside your modifyDriverUsing callback:
use RalphJSmit\Filament\MediaLibrary\Drivers\MediaLibraryItemDriver;
$driver
->mediaLibraryItemModel(\App\Models\MediaLibraryItem::class)
->mediaLibraryFolderModel(\App\Models\MediaLibraryFolder::class);