okipa / laravel-medialibrary-extension
Extra features for https://github.com/spatie/laravel-medialibrary package.
Requires
- php: ^7.2
- ext-fileinfo: *
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- spatie/laravel-medialibrary: ^7.0.0
Requires (Dev)
- nunomaduro/larastan: ^0.3.16
- orchestra/testbench: 3.8.*
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2019-08-27 16:41:59 UTC
README
This package provide extra features for the spatie/laravel-medialibrary package.
Find the base package documentation here : https://docs.spatie.be/laravel-medialibrary/v7.
⚠️PACKAGE ABANDONNED => REPLACED BY https://github.com/Okipa/laravel-medialibrary-ext ⚠️
Installation
You can install the package via composer :
composer require okipa/laravel-medialibrary-extension:^7.0.0
The extension package will automatically install the spatie/laravel-medialibrary if it has not already been installed on your project.
This package follows the spatie/laravel-medialibrary
versioning scheme.
Extra features
- Collection mime types constraint setup
- Collection validation constraints rules generation
- Collection validation constraints legend generation
- Useful extra public methods
Collection mime types constraint setup
Addition of the acceptsMimeTypes(array $mimeTypes): MediaCollection
which can be used with a media collection.
Once declared, the mime types constraints will be used to trigger the FileUnacceptableForCollection
exception if not respected, and also used to generate validation constraints and legends (see bellow).
// example public function registerMediaCollections() { $this->addMediaCollection('images')->acceptsFile(function (File $file) { return $file->size <= 30000; })->acceptsMimeTypes(['image/jpeg', 'image/png']); }
Collection validation constraints rules generation
Addition of the validationConstraints(string $collectionName): string
method, which can be used with a model using the HasMediaTrait
.
// in your user storing form request for example public function rules() { return [ 'avatar' => (new User)->validationConstraints('avatar'), // your other validation rules ]; }
Rendering example : dimensions:min_width=60,min_height=20|mimetypes:image/jpeg,image/png
.
Collection validation constraints legend generation
Addition of the constraintsLegend(string $collectionName): string
method, which can be used with a model using the HasMediaTrait
.
// in your HTML form <label for="avatar">Choose a profile picture :</label> <input type=" id="avatar" name="avatar" value="{{ $avatarFileName }}"> <small>{{ (new User)->constraintsLegend('avatar') }}</small>
Rendering example : Min. width : 150 px / Min. height : 70 px. Accepted MIME Type(s) : image/jpeg, image/png.
Useful extra public methods
The following methods can also be used separately with a model using the HasMediaTrait
:
dimensionValidationConstraints(string $collectionName): string
: Get a collection dimension validation constraints string from its name.mimeTypesValidationConstraints(string $collectionName): string
: Get a collection mime types constraints validation string from its name.dimensionsLegend($collectionName): string
: Get a collection dimensions constraints legend string from its name.mimeTypesLegend($collectionName): string
: Get a collection mime types constraints legend string from its name.collectionMaxSizes(string $collectionName): array
: Get registered collection max width and max height from its name.getCollection(string $collectionName): ?MediaCollection
: Get a media collection object from its name.getConversions(string $collectionName): array
: Get declared conversions from a media collection name.mayContainsImages(MediaCollection $collection): bool
: Check if the given media collection contains images from its declared accepted mime types. It is considered that a collection without declared accepted mime types may contains images.
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.