eleven59 / backpack-image-traits
用于处理图像上传字段的Laravel辅助特性包(包括在可重复字段中)
1.0.3
2023-08-29 09:06 UTC
Requires
- backpack/crud: ^4.1.0|^5.0
- illuminate/support: ~7|~8|~9
- intervention/image: ^2.5
Requires (Dev)
- orchestra/testbench: ~5|~6
- phpunit/phpunit: ~9.0
README
用于处理图像上传字段的Laravel辅助特性包(包括在可重复字段中)
本包提供了处理Backpack内置图像字段的辅助特性。使用这些特性,您只需添加一行代码的函数即可处理图像字段。
安装
依赖项
本包需要
安装
通过Composer
composer require eleven59/backpack-image-traits
用法
本包包含的两个特性允许您使用单行代码处理Backpack for Laravel的图像类型字段。如需调整,请参考以下选项。
图像字段
对于单个顶级图像字段
- 将HasImageFields特性添加到模型中
- 为图像字段添加setAttribute函数
class Rogue extends Model { use HasImageFields; /** * i.e. $this->avatar is a CRUD image field */ public function setAvatarAttribute($value) { $this->attributes['avatar'] = $this->uploadImageData($value); } }
可重复字段中的图像
如果您的模型中有一个或多个子图像字段的可重复字段
- 将HasImageFields和HasImagesInRepeatableFields特性添加到模型中
- 为可重复字段添加setAttribute函数
class Mage extends Model { use HasImageFields, HasImagesInRepeatableFields; protected $casts = ['spells' => 'array']; // You should already have this /** * i.e. $this->spells is a CRUD field of the 'repeatable' type * each child entity has one or more fields of the 'image' type */ public function setSpellsAttribute($value) { $this->attributes['spells'] = $this->uploadRepeatableImageData($value); } }
选项
这两个特性支持相同的选项数组,以自定义图像上传的处理方式。对于可重复字段,相同的选项将被用于可重复字段子数组中的每个图像字段。
以下列出所有可用选项及其默认值。
public function setAvatarAttribute($value) { $this->attributes['avatar'] = $this->uploadImageData($value, [ 'disk' => 'public', // Storage disk as defined in config/filesystems.php 'delete_path' => null, // Path of old value; file will be deleted if specified (don't use for repeatable) 'directory' => $this->table, // Directory in storage disk to use; defaults to model's table name 'quality' => 65, // Intervention Image quality setting, default is 65 'format' => 'jpg', // Format to use for the generated image, default is jpg 'transformation' => null, // Accepts a callable to make additional transformations (see advanced examples) 'callback' => null, // Accepts a callable to override the return function (see advanced examples) ]); }
支持的格式:所有Intervention Image格式,除data-url外,自Intervention Image 2.5.1起
- jpg
- png
- gif
- tif
- bmp
- ico
- psd
- webp
使用callables的高级示例
transformations callable可以用于使用Intervention Image对象执行额外的转换(请参阅Intervention Image文档)。
public function setLogoAttribute($value) { $this->attributes['logo'] = $this->uploadImageData($value, [ 'format' => 'png', 'transformations' => function(Intervention\Image\Image $image) { // Remove all red and blue from the image $image->colorize(-100, 0, -100); }, ]); }
callback callable允许您使用生成的文件名作为输入变量来覆盖返回函数。
public function setSecretPhotoAttribute($value) { $this->attributes['secret_photo'] = $this->uploadImageData($value, [ 'disk' => 'local', 'directory' => 'secret_photos', 'callback' => function($filename) { // Return storage path instead of public url return Storage::disk('local')->path('secret_photos/'.$filename); }, ]); }
变更日志
重大更改将在此列出。有关其他更改,请参阅提交日志。
鸣谢
许可
本项目以MIT许可证发布,因此您可以在任何Backpack & Laravel项目上安装它。有关更多信息,请参阅许可文件。
但是,请注意,您需要安装Backpack,因此您还需要遵守其YUMMY许可证。这意味着在生产中,您需要Backpack许可证代码。您可以在backpackforlaravel.com上免费(用于非商业用途)或付费(用于商业用途)获取许可证。