定制 eleven59/backpack-image-traits 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eleven59/backpack-image-traits

Composer 安装命令:

composer require eleven59/backpack-image-traits

包简介

Backpack for Laravel helper traits for processing image upload fields (including within repeatable fields)

README 文档

README

Latest Version on Packagist Total Downloads

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

Installation

Via Composer

composer require eleven59/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:

  1. Add HasImageFields Trait to Model
  2. 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:

  1. Add HasImageFields and HasImagesInRepeatableFields Traits to Model
  2. 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.

eleven59/backpack-image-traits 适用场景与选型建议

eleven59/backpack-image-traits 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「backpack」 「Backpack for Laravel」 「Backpack Addon」 「BackpackImageTraits」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 eleven59/backpack-image-traits 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 eleven59/backpack-image-traits 我们能提供哪些服务?
定制开发 / 二次开发

基于 eleven59/backpack-image-traits 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 84
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 6
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-02