Okay, let's break down how to configure Spatie's laravel-medialibrary to use the libvips engine for image conversions.
The key is that laravel-medialibrary uses the spatie/image package under the hood for image manipulations. spatie/image supports multiple drivers: GD, Imagick, and Vips. You just need to tell laravel-medialibrary (and thus spatie/image) to use the Vips driver.
Here are the steps:
1. System Requirements: Install libvips and the php-vips Extension
This is the most crucial prerequisite. libvips needs to be installed on your server, and the corresponding PHP extension must be compiled and enabled.
2. Configure Spatie Medialibrary
Now, tell laravel-medialibrary to use the Vips driver.
-
Publish Configuration (if you haven't already):
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"
This will create the config/media-library.php file.
-
Edit Configuration:
Open config/media-library.php and find the image_driver setting. Change its value from the default (gd or imagick) to vips:
<?php
return [
// ... other settings
/*
* The driver that will be used for image manipulations.
* Determines which underlying library is used by `spatie/image` to perform manipulation.
* Supported: "gd", "imagick", "vips".
*/
'image_driver' => 'vips', // <--- Change this line
// ... other settings
];
3. Define Your Conversions (No Change Needed Here)
The way you define your media conversions in your Eloquent models remains exactly the same. Medialibrary will now simply use the Vips driver behind the scenes when executing these conversion definitions.
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
class YourModel extends Model implements HasMedia
{
use InteractsWithMedia;
public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('thumb')
->width(368)
->height(232)
->sharpen(10)
->performOnCollections('images'); // Example collection
$this->addMediaConversion('large')
->width(1024)
->performOnCollections('images');
// Add more conversions as needed
// Medialibrary will now use libvips for these manipulations
}
}
4. Test It!
Upload a new image to a model that uses InteractsWithMedia and has conversions defined. Check the generated files in your storage directory. Medialibrary should now be using libvips for resizing, cropping, format changes, etc., specified in your registerMediaConversions method.
Benefits of Using Vips:
- Performance: Vips is generally significantly faster than GD and Imagick, especially for large images.
- Memory Efficiency: Vips uses less memory as it can stream image processing operations rather than loading the entire image into RAM.
Potential Issues & Troubleshooting:
- Extension Not Loaded: The most common issue is the
php-vips extension not being installed or enabled correctly. Double-check php -m and phpinfo().
- Libvips Missing: Ensure the base
libvips library is installed on the system.
- Permissions: Ensure your web server/PHP process has the necessary permissions to execute Vips operations and write files.
- Unsupported Formats: While Vips supports many formats, ensure the specific formats you need (e.g., WebP, AVIF) are enabled in your
libvips build. You might need to install additional libraries like libwebp-dev, libheif-dev before installing libvips or the PHP extension.
- Logs: Check your Laravel logs (
storage/logs/laravel.log) and PHP error logs for any specific error messages.
By following these steps, you can leverage the speed and efficiency of libvips for your image conversions within your Laravel application using Spatie Medialibrary.