MediaLibrary can be installed via composer:
composer require "spatie/laravel-medialibrary:^8.0.0"
The package will automatically register a service provider.
You need to publish and run the migration:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
php artisan migrate
If you use Artisan's migrate:rollback
or migrate:refresh
commands you should edit the migration to include a down method:
public function down()
{
Schema::dropIfExists('media');
}
Publishing the config file is optional:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"
This is the default content of the config file:
return [
'disk_name' => env('MEDIA_DISK', 'public'),
'max_file_size' => 1024 * 1024 * 10,
'queue_name' => '',
'queue_conversions_by_default' => env('QUEUE_CONVERSIONS_BY_DEFAULT', true),
'media_model' => Spatie\MediaLibrary\MediaCollections\Models\Media::class,
'remote' => [
'extra_headers' => [
'CacheControl' => 'max-age=604800',
],
],
'responsive_images' => [
'width_calculator' => Spatie\MediaLibrary\ResponsiveImages\WidthCalculator\FileSizeOptimizedWidthCalculator::class,
'use_tiny_placeholders' => true,
'tiny_placeholder_generator' => Spatie\MediaLibrary\ResponsiveImages\TinyPlaceholderGenerator\Blurred::class,
],
'default_loading_attribute_value' => null,
'conversion_file_namer' => \Spatie\MediaLibrary\Conversions\DefaultConversionFileNamer::class,
'path_generator' => Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator::class,
'url_generator' => Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator::class,
'version_urls' => false,
'image_optimizers' => [
Spatie\ImageOptimizer\Optimizers\Jpegoptim::class => [
'--strip-all',
'--all-progressive',
],
Spatie\ImageOptimizer\Optimizers\Pngquant::class => [
'--force',
],
Spatie\ImageOptimizer\Optimizers\Optipng::class => [
'-i0',
'-o2',
'-quiet',
],
Spatie\ImageOptimizer\Optimizers\Svgo::class => [
'--disable=cleanupIDs',
],
Spatie\ImageOptimizer\Optimizers\Gifsicle::class => [
'-b',
'-O3',
],
],
'image_generators' => [
Spatie\MediaLibrary\Conversions\ImageGenerators\Image::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Webp::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Pdf::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Svg::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Video::class,
],
'image_driver' => 'gd',
'ffmpeg_path' => env('FFMPEG_PATH', '/usr/bin/ffmpeg'),
'ffprobe_path' => env('FFPROBE_PATH', '/usr/bin/ffprobe'),
'temporary_directory_path' => null,
'jobs' => [
'perform_conversions' => \Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob::class,
'generate_responsive_images' => \Spatie\MediaLibrary\ResponsiveImages\Jobs\GenerateResponsiveImagesJob::class,
],
];
By default, the media library will store its files on Laravel's public
disk. If you want a dedicated disk you should add a disk to config/filesystems.php
. This would be a typical configuration:
...
'disks' => [
...
'media' => [
'driver' => 'local',
'root' => public_path('media'),
],
...
Don't forget to ignore the directory of your configured disk so the files won't end up in your git repo.
If you are planning on working with image manipulations it's recommended to configure a queue on your server and specify it in the config file.
Want to use S3? Then follow Laravel's instructions on how to add the S3 Flysystem driver.
##Optimization tools
Media library will use these tools to optimize converted images if they are present on your system:
Here's how to install all the optimizers on Ubuntu:
sudo apt install jpegoptim optipng pngquant gifsicle
npm install -g svgo
And here's how to install the binaries on MacOS (using Homebrew):
brew install jpegoptim
brew install optipng
brew install pngquant
brew install svgo
brew install gifsicle