Handling media in Laravel with Laravel Simple Media
About two weeks ago, I published an open-source Laravel package on Github called Laravel Simple Media.
The package enables you to upload media files, optionally attached to an Eloquent model, through a simple to use API.
The package is built using the Laravel Flysystem. For image quality and resizing, Intervention Image is used.
Why a new package
There are already some packages out there that provide similair functionalities, like for example Spatie's popular Laravel Medialibrary. Spatie's package is certainly more fleshed out and feature rich compared to mine.
However, I needed something very simple which would also allow me not having to attach media to models. Sometimes you just quickly want to upload something that isn't necasserily linked to a blog or an article for example. At this moment, this appears to be not possible with Spatie's package.
Interacting with the API
The API is very straightforward. Below we will go through some basic examples, but for more detailed usage instruction, check out the readme on Github.
For uploading images, you would simply do this:
Media::uploadImage($file); // $file points to the file path
For media other then images, you would use:
Now these uploads would be unattached to other Eloquent models. But, say for example you have a
Blog model and you would like to attach an image. You can then do this:
To make that work, you need to add the
HasMedia trait to the model that you want to attach media to.
If you are working with a file submitted from a request, you can use these handy functions:
$blog->attachImageFromRequest('image'); // where image references the key of the file in the request $blog->attachFileFromRequest('file'); Media::uploadImageFromRequest('image'); Media::uploadFileFromRequest('file');
For all of the above methods, you are able to overwrite the default file data. Say for example, you would like to use a different disk then the one that is configured, you can do this:
Media::uploadImage($file, ['disk' => 'local']);
To retrieve media you can simply use:
$blog->media(); // Get all attach media
Retrieving a specific image size:
Another handy thing, is that you can group files. Say for example that you would want to group files as
documents, you can do that like this:
Media::uploadFile($file, ['group' => 'documents']); // upload with group Media::where('group', 'documents')->get(); // retrieving // When retrieving attached media by goup $article->mediaByGroup('documents')->get();
Multiple image sizes and other configuration options
You can set several configuration options, including the image driver that should be used. You can also define the image quality and image sizes that will automatically be generated when an image is uploaded.
'image_sizes' => [ 'thumbnail' => [ 'width' => 100, 'height' => 100, 'crop' => true // determine if the image needs to be cropped ], // etc. ]
Additionally, you can configure which File Storage disk should be used for your uploads in the configuration file. By default, it uses
This is actually the first Laravel package I'm making open source, so that is exciting. I feel the API is simple to use, which was one of the main goals. The package is still under development, as I'm sure there are things that can be improved upon. If you would like to contribute, feel free to do so by opening a pull request on Github.