Media Library can be installed via Composer:
If you only use the base package issue this command:
composer require "spatie/laravel-medialibrary:^10.0.0"
If you have a license for Media Library Pro, you should install spatie/laravel-media-library-pro
instead. Please refer to our Media Library Pro installation instructions to continue.
##Preparing the database
You need to publish the migration to create the media
table:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
After that, you need to run migrations.
php artisan migrate
##Publishing the config file
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_connection_name' => '',
'queue_name' => '',
'queue_conversions_by_default' => env('QUEUE_CONVERSIONS_BY_DEFAULT', true),
'media_model' => Spatie\MediaLibrary\MediaCollections\Models\Media::class,
'temporary_upload_model' => Spatie\MediaLibraryPro\Models\TemporaryUpload::class,
'enable_temporary_uploads_session_affinity' => true,
'generate_thumbnails_for_temporary_uploads' => true,
'file_namer' => Spatie\MediaLibrary\Support\FileNamer\DefaultFileNamer::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 => [
'-m85',
'--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',
],
Spatie\ImageOptimizer\Optimizers\Cwebp::class => [
'-m 6',
'-pass 10',
'-mt',
'-q 90',
],
Spatie\ImageOptimizer\Optimizers\Avifenc::class => [
'-a cq-level=23',
'-j all',
'--min 0',
'--max 63',
'--minalpha 0',
'--maxalpha 63',
'-a end-usage=q',
'-a tune=ssim',
],
],
'image_generators' => [
Spatie\MediaLibrary\Conversions\ImageGenerators\Image::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Webp::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Avif::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Pdf::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Svg::class,
Spatie\MediaLibrary\Conversions\ImageGenerators\Video::class,
],
'temporary_directory_path' => null,
'image_driver' => env('IMAGE_DRIVER', 'gd'),
'ffmpeg_path' => env('FFMPEG_PATH', '/usr/bin/ffmpeg'),
'ffprobe_path' => env('FFPROBE_PATH', '/usr/bin/ffprobe'),
'jobs' => [
'perform_conversions' => Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob::class,
'generate_responsive_images' => Spatie\MediaLibrary\ResponsiveImages\Jobs\GenerateResponsiveImagesJob::class,
],
'media_downloader' => Spatie\MediaLibrary\Downloaders\DefaultDownloader::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,
],
'enable_vapor_uploads' => env('ENABLE_MEDIA_LIBRARY_VAPOR_UPLOADS', false),
'default_loading_attribute_value' => null,
'prefix' => env('MEDIA_PREFIX', ''),
];
##Adding a media disk
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'),
'url' => env('APP_URL').'/media',
],
...
Don't forget to .gitignore
the directory of your configured disk, so the files won't end up in your git repo.
To store all media on that disk by default, you should set the disk_name
config value in the media-library
config file to the name of the disk you added.
return [
'disk_name' => 'media',
];
Want to use S3? Then follow Laravel's instructions on how to add the S3 Flysystem driver. If possible, we recommend using a remote filesystem like S3 instead of your local filesystem to prevent security issues.
##Setting up a queue
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.
##Setting up 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 libavif-bin
npm install -g svgo
If you don't want to install npm
on your Ubuntu server, you can use snap
which is installed by default:
sudo apt install jpegoptim optipng pngquant gifsicle libavif-bin
sudo snap install svgo
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
brew install libavif
##Installing Media Library Pro
Media Library Pro is an optional add-on package that offers Blade, Vue, and React components to upload files to your application. It integrates beautifully with the laravel-medialibrary.
You can buy a license for Media Library Pro on the product page at spatie.be.
To install Media Library Pro, you should follow these instructions.