kda/eloquent-medialibrary-item
Composer 安装命令:
composer require kda/eloquent-medialibrary-item
包简介
Associate models with spatie/media-library
README 文档
README
About
This is a package that has the ambition to fill the gap of spatie/laravel-medialibrary and allows to link the same media to multiple model
In addition It provides the concept of Media Flavor, that can centralize and reuse the media conversions
Installation
You can install the package via composer:
composer require kda/eloquent-medialibrary-item
You can publish and run the migrations with:
php artisan vendor:publish --provider="\KDA\Eloquent\MedialibraryItem" --tag="migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="\KDA\Eloquent\MedialibraryItem" --tag="config"
This is the contents of the published config file:
return [
];
Usage
Media Flavor
first, you need to create a media flavor in your project. Create a class that extends \KDA\Eloquent\MedialibraryItem\Flavor.
<?php
namespace App\Flavors;
use KDA\Eloquent\MedialibraryItem\Flavor;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Conversions\Conversion;
class Header extends Flavor{
//spatie collection name
public $collection = "header";
//define your variants
public $variants= [
'bw',
'sepia'
];
// define your breakpoints
public $breakpoints = [
'sm',
'md',
'xl',
];
//define your stack
public $stack = [
'dimensions',
'ratios',
'colors'
];
//set to false to only generate variants
//if true, you'll have a conversion with no variant
public $generateOneWithoutVariant = true;
public function colors(Media $media,Conversion $conversion, string $breakpoint,array |null $data,string | null $variant) :Conversion {
return match($variant){
"bw"=> $conversion->greyscale(),
"sepia"=> $conversion->sepia()->blur(20),
default=> $conversion
};
}
public function ratios(Media $media,Conversion $conversion, string $breakpoint,array |null $data, string | null $variant) :Conversion {
return match($breakpoint){
'xl'=> $conversion->focalCrop(1024,1024,50,50,2),
default=> $conversion
};
}
public function dimensions(Media $media,Conversion $conversion, string $breakpoint, array |null $data) :Conversion {
return match($breakpoint){
'md'=> $conversion->width(128)->height(128),
default=> $conversion
};
}
}
Model
enhance any model with the \KDA\Eloquent\MedialibraryItem\Models\Traits\Curates trait
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Post extends Model
{
use \KDA\Eloquent\MedialibraryItem\Models\Traits\Curates;
///
Then you can associate a media with the addMedia method on your model
$p->addMedia($media1)->usingFlavor('\App\Flavors\Header');
This will add the media and generate all the conversions for all breakpoints and variants
Then you can retrieve media like this
//reading media
$p->mediaLibraryItems->first()->getFirstMediaByFlavor('App\Flavors\Header')->getUrl('xl');
//reading variant
$p->mediaLibraryItems->first()->getFirstMediaByFlavor('App\Flavors\Header')->getUrl('bw-xl');
Hint
you can shorten your flavors by binding them in the Service container
public function boot()
{
$this->app->bind('avatar', \App\Flavors\Avatar::class);
$this->app->bind('header', \App\Flavors\Header::class);
}
....
$p->mediaLibraryItems->first()->getFirstMediaByFlavor('header')->getUrl('xl');
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
kda/eloquent-medialibrary-item 适用场景与选型建议
kda/eloquent-medialibrary-item 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 377 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kda/eloquent-medialibrary-item 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kda/eloquent-medialibrary-item 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 377
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-26