escapework / laramedias
A Laravel package that integrates Glide for easily manage medias on your project.
Installs: 1 861
Dependents: 0
Suggesters: 0
Security: 0
Stars: 18
Watchers: 5
Forks: 2
Open Issues: 0
Requires
- escapework/laravel-steroids: 0.7.*
- league/glide: ^2.2
Requires (Dev)
- doctrine/dbal: ~2.3
- mockery/mockery: 1.5.*
- orchestra/testbench: 7.4.*
- orchestra/testbench-browser-kit: 7.2.*
- phpunit/phpunit: ~9.0
README
A Laravel package that integrates [Glide](http://glide.thephpleague.com) and easy media management on your Laravel project.
<img src="{{ $product->present->picture(500, 300, 'crop') }}" alt="Easy media management">
Version Compatibility
Installation
Via Composer:
$ composer require escapework/laramedias:"0.7.*"
Configuration
And execute the following code:
$ php artisan vendor:publish --provider="EscapeWork\LaraMedias\Providers\MediasServiceProvider"
$ php artisan migrate
Configurations explained:
'disk' => null, // if you dont want to use filesystems.default disk config, change it here... // ...for saving on another disk 'max_size' => [ 'width' => 2000, // when creating medias, the images will be resized to this max_size... 'height' => 2000, // ...for reducing disk usage ], 'url' => 'medias', // if you want to change the laravel glide medias URL 'dir' => 'medias', // if you want to change the default directory where the medias are saved 'path' => 'general', // if you want to change the directory where the multipleMedias are saved (you will undestand this later)
Usage
This package allows you to easily use medias with your laravel models. There are two basic ways to use:
One model has multiple medias
Let's say you have a Product
model that need to have multiple medias. You have to do this:
- Import the following trait in your model;
use EscapeWork\LaraMedias\Traits\Medias; class Product extends Model { use Medias; }
Now, you can do this:
Upload and create multiple medias:
$product->uploadMedias($request->file('medias'));
Interate through your medias
The $product->medias
will be a default Laravel collection of EscapeWork\LaraMedias\Models\Media
models which you can use any of the collection methods available.
@foreach ($product->medias as $media) <?php /* all media models have an presenter class so you can easily show the image in different forms ->picture($width, $height, $fit) */ ?> <img src="{{ $media->present->picture(600, 300, 'crop') }}"> @endforeach
Each $media
object will be a LaraMedias\Models\Media
eloquent model, which will have a presenter for easily displaying images (see the above example).
The parameters in the example are the Glide width (w
), height (h
) and fit
. You can see a simple example here (http://glide.thephpleague.com/1.0/simple-example/).
If your model was deleted, all the medias will be deleted too.
Deleting medias
For delete your medias, just call the method removeMedias
.
$product->removeMedias([1, 2]); // just pass the IDs
For removing all medias, just call the removeMedias
method without any parameters.
$product->removeMedias();
Events
These events are dispatched when in the above use case:
One model has one media field
Let's say you have a Banner
model and want to upload a single image for him. With Laramedias
you can do this:
First, configure the config/medias.php
file:
'models' => [ 'banners' => [ 'model' => 'App\Models\Banner', 'fields' => ['banner'] // here you have to put the fields in your model which use medias ], ],
Second, use the EscapeWork\LaraMedias\Traits\Medias
trait in your Banner
model.
use EscapeWork\LaraMedias\Traits\Medias; class Banner extends Model { use Medias; }
Then, you can just use the uploadSingleMedia
method.
$banner = Banner::find(1); $banner->uploadSingleMedia($request->file('banner'), 'banner'); // the second parameter is the field name to be updated $banner->save();
After that, you can just use the media
helper method to show your banner.
<img src="{{ media_path($banner, 'banner', 1920, 400, 'crop') }}" alt="...">
I would also recomend in this case to make use of Presenters. You can use your custom setup or make use of this package that makes it very easy.
Then, you can setup like this:
use EscapeWork\LaravelSteroids\Presenter; class BannerPresenter extends Presenter { public function banner($w = 100, $h = 50, $fit = 'fit') { return media_path($this->model, 'banner', $w, $h, $fit); } }
And you like this:
<img src="{{ $banner->present->banner(1920, 500, 'crop') }}">
Contributing
Feel free to open any pull request/issue with your idea/bug/suggestion.
License
See the License file.