-
Notifications
You must be signed in to change notification settings - Fork 15
HasMediaTrait with additional features (conversions, translatable...) #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shaoshiva
wants to merge
2
commits into
0.3
Choose a base branch
from
feature/HasMediaTrait
base: 0.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| <?php | ||
|
|
||
| namespace Novius\Backpack\CRUD\ModelTraits\SpatieMediaLibrary; | ||
|
|
||
| use Spatie\Image\Manipulations; | ||
| use Spatie\MediaLibrary\HasMedia\HasMediaTrait as HasMediaTraitNative; | ||
| use Spatie\MediaLibrary\Models\Media; | ||
|
|
||
| /** | ||
| * Overrides the native HasMediaTrait trait to add some additional features : | ||
| * - adds some methods to work with medias stored as attributes (for example with the UploadableImage trait) | ||
| * - adds some properties and methods to easily handle conversions (crop and fit max) | ||
| * | ||
| * @package Novius\Backpack\CRUD\ModelTraits\SpatieMediaLibrary | ||
| */ | ||
| trait HasMediaTrait | ||
| { | ||
| use HasMediaTraitNative; | ||
|
|
||
| /** | ||
| * The crop conversions | ||
| * | ||
| * @var array | ||
| */ | ||
| // protected $mediaConversionsCrop = [ | ||
| // 'picture' => [ // The attribute name | ||
| // 'full' => [1440, 810], // An example of conversion | ||
| // 'medium' => [1080, 608], // Another example of conversion | ||
| // 'small' => [768, 432], // A third example of conversion | ||
| // ] | ||
| // ]; | ||
|
|
||
| /** | ||
| * The fit max conversions | ||
| * | ||
| * @var array | ||
| */ | ||
| // protected $mediaConversionsFitMax = [ | ||
| // 'picture' => [ // The attribute name | ||
| // 'full' => [877, 900], // An example of conversion | ||
| // 'medium' => [670, 687], // Another example of conversion | ||
| // 'small' => [580, 594], // A third example of conversion | ||
| // ], | ||
| // ]; | ||
|
|
||
| /** | ||
| * Gets the attribute media URL with a fallback on the media stored on disk. | ||
| * | ||
| * @param string $attributeName | ||
| * @param string $conversionName | ||
| * @param bool $fallbackOnDisk Whether to fallback on the media stored on disk if the media collection doesn't exists | ||
| * @return \Illuminate\Contracts\Routing\UrlGenerator|null|string | ||
| */ | ||
| public function getAttributeMediaUrl(string $attributeName, string $conversionName = '', bool $fallbackOnDisk = true) | ||
| { | ||
| // Gets the URL from the media collection | ||
| $collectionName = $this->getAttributeMediaCollectionName($attributeName); | ||
| $url = $this->getFirstMediaUrl($collectionName, $conversionName); | ||
|
|
||
| if (empty($url) && $fallbackOnDisk) { | ||
| // Gets the URL from the media stored on disk as fallback | ||
| $url = $this->getAttributeMediaUrlFromDisk($attributeName); | ||
| } | ||
|
|
||
| return url($url); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the attribute media URL with crop conversion and with a fallback on the media stored on disk. | ||
| * | ||
| * @param string $attributeName | ||
| * @param string $cropConversionName | ||
| * @param bool $fallbackOnDisk | ||
| * @return \Illuminate\Contracts\Routing\UrlGenerator|null|string | ||
| */ | ||
| public function getAttributeMediaCropUrl(string $attributeName, string $cropConversionName, bool $fallbackOnDisk = true) | ||
| { | ||
| return $this->getAttributeMediaUrl($attributeName, 'crop-'.$cropConversionName, $fallbackOnDisk); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the attribute media URL with fit max conversion and with a fallback on the media stored on disk. | ||
| * | ||
| * @param string $attributeName | ||
| * @param string $fitMaxConversionName | ||
| * @param bool $fallbackOnDisk | ||
| * @return \Illuminate\Contracts\Routing\UrlGenerator|null|string | ||
| */ | ||
| public function getAttributeMediaFitMaxUrl(string $attributeName, string $fitMaxConversionName, bool $fallbackOnDisk = true) | ||
| { | ||
| return $this->getAttributeMediaUrl($attributeName, 'fitmax-'.$fitMaxConversionName, $fallbackOnDisk); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the attribute media URL from disk. | ||
| * | ||
| * @param string $attributeName | ||
| * @return \Illuminate\Contracts\Routing\UrlGenerator|string | ||
| */ | ||
| public function getAttributeMediaUrlFromDisk(string $attributeName) | ||
| { | ||
| return \Storage::disk('public')->url($this->getAttribute($attributeName)); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the localized collection name of the attribute media. | ||
| * | ||
| * @param string $mediaAttributeName | ||
| * @param string|null $locale | ||
| * @return string | ||
| */ | ||
| public function getAttributeMediaCollectionName(string $mediaAttributeName, string $locale = null) | ||
| { | ||
| $collectionName = $mediaAttributeName; | ||
|
|
||
| // Appends the locale if translatable | ||
| if ($this->isTranslatableAttributeMedia($mediaAttributeName)) { | ||
| $collectionName .= '-'.($locale ?? $this->getLocale()); | ||
| } | ||
|
|
||
| return $collectionName; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the given attribute media is translatable. | ||
| * | ||
| * @param string $attributeName | ||
| * @return bool | ||
| */ | ||
| public function isTranslatableAttributeMedia(string $attributeName) | ||
| { | ||
| return method_exists($this, 'isTranslatableAttribute') && $this->isTranslatableAttribute($attributeName); | ||
| } | ||
|
|
||
| /** | ||
| * Registers the crop conversions from the property "mediaConversionsCrop". | ||
| * | ||
| * @throws \Spatie\Image\Exceptions\InvalidManipulation | ||
| */ | ||
| public function registerMediaConversionsCrop() | ||
| { | ||
| if (!property_exists($this, 'mediaConversionsCrop')) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ($this->mediaConversionsCrop as $collectionName => $conversions) { | ||
| foreach ($conversions as $conversionName => $dimensions) { | ||
| $this->addMediaConversion('crop-'.$conversionName) | ||
| ->crop(Manipulations::CROP_CENTER, $dimensions[0], $dimensions[1]) | ||
| ->optimize() | ||
| ->performOnCollections($collectionName); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Registers the fit max conversions from the property "mediaConversionsFitMax". | ||
| * | ||
| * @throws \Spatie\Image\Exceptions\InvalidManipulation | ||
| */ | ||
| public function registerMediaConversionsFitMax() | ||
| { | ||
| if (!property_exists($this, 'mediaConversionsFitMax')) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ($this->mediaConversionsFitMax as $collectionName => $conversions) { | ||
| foreach ($conversions as $conversionName => $dimensions) { | ||
| $this->addMediaConversion('fitmax-'.$conversionName) | ||
| ->fit(Manipulations::FIT_MAX, $dimensions[0], $dimensions[1]) | ||
| ->optimize() | ||
| ->performOnCollections($collectionName); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Automatically registers the media conversions defined via properties. | ||
| * | ||
| * @param Media|null $media | ||
| * @throws \Spatie\Image\Exceptions\InvalidManipulation | ||
| */ | ||
| public function registerMediaConversions(Media $media = null) | ||
| { | ||
| $this->registerMediaConversionsCrop(); | ||
| $this->registerMediaConversionsFitMax(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We find already this information in the readme file. If it is intended as documentation I would put this within the trait phpdoc.