Components
Media Library
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.
Pros
- Image/PDF conversions built-in.
- Proven technology & scalability with over 28 million installs.
- Can handle non-unique file names easily.
- Easy response-image integration for use on (Blade-based) frontends.
- Link selected images to your models using a database ID and foreign key.
- More performant searching through large libraries.
Cons
- Storing a selection of multiple
MediaLibraryItem's can be done using a JSON-column with integer IDs or a separate pivot table, which is less intuitive.
Installation
Publish migrations
First, publish the migrations of the Media Library package:
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:
Next, publish and run the package 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:
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 ->conversions() method on the $driver:
Working with media conversions
By default, the plugin registers four media conversions. The media conversions are:
responsivesmall(known as400)medium(known as800)thumb
Media conversions not being generated
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:
Registering custom media conversions
If you want to register additional media conversions, you can do so using the ->registerConversions() method:
You are free to register as many conversions as you like. However, be aware that each new conversion takes up more disk space.