mohamed7sameer/backpack-image-traits
Composer 安装命令:
composer require mohamed7sameer/backpack-image-traits
包简介
Backpack for Laravel helper traits for processing image upload fields (including within repeatable fields)
README 文档
README
Backpack for Laravel helper traits for processing image upload fields (including in repeatable fields)
This package provides helper traits for processing Backpack's built-in image fields. With these traits, you'll only have to add a function with one line of code to process image fields.
Installation
Dependencies
This package requires
- PHP 7.2+
- intervention-image:^2.5
- backpack/crud:6.*
Installation
Via Composer
composer require mohamed7sameer/backpack-image-traits
Usage
The two traits included in this package allow you to process Backpack for Laravel fields of the 'image' type with only a single line of code. More lines of code available if you need to tweak things (see options below).
Image fields
For single top-level image fields:
- Add HasImageFields Trait to Model
- Add setAttribute function for the image field
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); } }
Images within repeatable fields
If you have a repeatable field in your model that has one or more child image fields:
- Add HasImageFields and HasImagesInRepeatableFields Traits to Model
- Add setAttribute function for the repeatable field
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); } }
Options
Both traits support the same array of options to customize how the image upload is processed. For repeatable fields, the same options will be used for every image field in the repeatable field's child array.
All available options are displayed below with their default values.
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) ]); }
Supported formats: all intervention image formats except data-url, so as of Intervention Image 2.5.1:
- jpg
- png
- gif
- tif
- bmp
- ico
- psd
- webp
Advanced examples using callables
The transformations callable can be used to perform additional transformations using the Intervention Image object (see Intervention Image docs).
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); }, ]); }
The callback callable allows you to override the return function using the generated filename as an input variable.
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); }, ]); }
Change log
Breaking changes will be listed here. For other changes see commit log.
Credits
License
This project was released under the MIT license, so you can install it on top of any Backpack & Laravel project. Please see the license file for more information.
However, please note that you do need Backpack installed, so you need to also abide by its YUMMY License. That means in production you'll need a Backpack license code. You can get a free one for non-commercial use (or a paid one for commercial use) on backpackforlaravel.com.
mohamed7sameer/backpack-image-traits 适用场景与选型建议
mohamed7sameer/backpack-image-traits 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「backpack」 「Backpack for Laravel」 「Backpack Addon」 「BackpackImageTraits」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mohamed7sameer/backpack-image-traits 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mohamed7sameer/backpack-image-traits 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mohamed7sameer/backpack-image-traits 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Extended summernote fields for laravel backpack
Yii2 panel widget
A Laravel package to handle GitHub webhooks seamlessly by dispatching Laravel events for all GitHub events. Users can listen to and handle these events easily.
Use a translation namespace vor mutliple translation files.
The query builder for WordPress
Ready to use Documents Backpack CRUD with API route for eg. privacy statement, imprint, sbt or gtc
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-17