5 Less-Known Features of Spatie Media Library

Spatie Laravel MediaLibrary is one of the most popular Laravel packages, with almost million downloads. But have you read all of its documentation, with all the features? Let's dive in.

In general, I notice that developers rarely read FULL documentation of the packages. And I assure you, there are usually a lot of "hidden gems" - rarely used parameters, additional features. Sometimes they are not even in the documentation, it's useful to also analyze the code of the package.

Anyway, let's see what Media Library has to offer.

Notice: the examples are taken from their documentation for Version 7 of the package. It's important, because over the years the package evolved, including some breaking changes between versions.


Feature 1. Add Media from URL

Media Library is mostly used to upload files and add them to collections. But did you know you can take the image directly from the internet by providing some URL?

$url = 'http://medialibrary.spatie.be/assets/images/mountain.jpg';
$newsItem
   ->addMediaFromUrl($url)
   ->toMediaCollection();

Feature 2. Human Readable Size

If you want to show file size somewhere in the list, you don't need to perform any manipulation with size in bytes. There's a property for that, called human_readable_size.

$mediaItems = $newsItem->getMedia();

// Returns the size in bytes
$mediaItems[0]->size;

// Returns the size in a human readable format (eg. 1,5 MB)
$mediaItems[0]->human_readable_size; 

Feature 3. Allow Certain Files into Collection

You can add some settings for every collection separately, one of them is restricting collection to allow only some type of file.

use Spatie\MediaLibrary\File;
...
public function registerMediaCollections()
{
    $this
        ->addMediaCollection('only-jpegs-please')
        ->acceptsFile(function (File $file) {
            return $file->mimeType === 'image/jpeg';
        });
}

Then, if you try to add non-jpeg file to collection, the action will throw a specific exception of class Spatie \ MediaLibrary \ Exceptions \ FileCannotBeAdded \ FileUnacceptableForCollection, which you can catch and inform your user.


Feature 4. Regenerating All (or Some) Images

If you change your media conversions, like sizes of thumbnails, and you want to regenerate all old images, there's artisan command:

php artisan medialibrary:regenerate

Of course, keep in mind, it may take a really long time for larger amount of images.

There are more parameters - you can renegerate only images by specific model, you can specify their IDs, regenerate only certain media conversions or only missing files. Read more here.


Feature 5. Downloading Multiple Files

What if you want to allow users to download a few files at once? Sure, let's archive them into zip and send it for download. Guess what, Media Library will help here!

class DownloadMediaController
{
   public function download(YourModel $yourModel)
   {
        // Let's get some media.
        $downloads = $yourModel->getMedia('downloads');

        // Download the files associated with the media in a streamed way.
        // No prob if your files are very large.
        return MediaStream::create('my-files.zip')->addMedia($downloads);
   }
}

Finally, not as separate features, but I advise you to look at two more things:

Anything I've missed? Add in the comments!

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1054 lessons, total 46 h 42 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials