mohamed7sameer / backpack-image-traits
用于处理 Laravel 辅助特性图像上传字段的背包 (包括重复字段中的字段)
1.0.0
2023-09-17 20:12 UTC
Requires
Requires (Dev)
README
用于处理 Laravel 辅助特性图像上传字段的背包 (包括在重复字段中)
此包提供了处理背包内置图像字段的辅助特性。使用这些特性,您只需添加一行代码即可处理图像字段。
安装
依赖关系
此包需要
- PHP 7.2+
- intervention-image:^2.5
- backpack/crud:6.*
安装
通过 Composer
composer require mohamed7sameer/backpack-image-traits
使用方法
此包中包含的两个特性允许您仅用一行代码处理背包 Laravel 的 'image' 类型的字段。如果需要调整,还有更多的代码行可供选择(见下文选项)。
图像字段
对于单个顶级图像字段
- 将 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 formats 除了 data-url,自 Intervention Image 2.5.1 以来
- jpg
- png
- gif
- tif
- bmp
- ico
- psd
- webp
使用可调用的高级示例
transformations 可调用用于使用 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 可调用允许您使用生成的文件名作为输入变量来覆盖返回函数。
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 许可证下发布,因此您可以在任何背包 & Laravel 项目的顶部安装它。有关更多信息,请参阅 许可文件。
但是,请注意,您确实需要安装背包,因此您还需要遵守其 YUMMY 许可证。这意味着在生产中,您需要背包许可证代码。您可以在 backpackforlaravel.com 上免费获得一个非商业用途的许可证(或商业用途的付费许可证)。