When using other files like videos or PDFs you'll often want to generate a still image or thumbnail. Medialibrary can do this for you.
As a start you'll need to have
Imagick installed on your system.
Imagick is used to convert
When using the command line (for example in this demo) make sure the
Imagick extension is also enabled there by running the following command:
php -me | grep 'imagick'
imagick is printed to the console you're good.
To generate still frames from video files you'll need the
FFmpeg framework on your system. Please refer to their official website for instructions. You can check whether
FFmpeg is installed by running the following command:
Remember to add the
php-ffmpeg package as a dependency to your project as well:
composer require php-ffmpeg/php-ffmpeg
We've already done this for you in the demo project.
As usual we've prepared an
Article model which for you that implements the
HasMediaConversions interface and has a
registerMediaConversions method. The
Article features some downloadable files for which we want to generate thumbnails.
$this->addMediaConversion('thumbnail') ->width(360) ->height(230) ->extractVideoFrameAtSecond(5) // Grab the still frame from the 5th second in the video ->performOnCollections('downloads');
You'll notice that the conversions look very similar to image conversions. The only difference being that we've specified a time when the still image should be generated from the video files using
article:addmedia command to add some
demofiles/hamlet.pdf) files to the
php artisan article:add-media demofiles/coolvideo.webm
If we check the storage disk now we'll find the media file and the specified conversion as a
JPG image. It's that simple.