PHPackages                             mohamed7sameer/backpack-image-traits - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mohamed7sameer/backpack-image-traits

ActiveLibrary

mohamed7sameer/backpack-image-traits
====================================

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

1.0.0(2y ago)091MITPHP

Since Sep 17Pushed 2y ago1 watchersCompare

[ Source](https://github.com/mohamed7sameer/backpack-image-traits)[ Packagist](https://packagist.org/packages/mohamed7sameer/backpack-image-traits)[ Docs](https://github.com/mohamed7sameer/backpack-image-traits)[ RSS](/packages/mohamed7sameer-backpack-image-traits/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (1)

BackpackImageTraits
===================

[](#backpackimagetraits)

[![Latest Version on Packagist](https://camo.githubusercontent.com/fb2b3ba786e3111ad08fcf9b5d5bd320b5b046517a071bf1a0b0844a8534a53c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f68616d65643773616d6565722f6261636b7061636b2d696d6167652d7472616974732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mohamed7sameer/backpack-image-traits)[![Total Downloads](https://camo.githubusercontent.com/be86c702e1da04e06552cf6fe5a920f48f771b3a6582f84a3713322a14e8787b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f68616d65643773616d6565722f6261636b7061636b2d696d6167652d7472616974732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mohamed7sameer/backpack-image-traits)

**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
------------

[](#installation)

### Dependencies

[](#dependencies)

This package requires

- PHP 7.2+
- [intervention-image:^2.5](https://github.com/Intervention/image)
- [backpack/crud:6.\*](https://github.com/Laravel-Backpack/CRUD)

### Installation

[](#installation-1)

Via Composer

```
composer require mohamed7sameer/backpack-image-traits
```

Usage
-----

[](#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

[](#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

[](#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

[](#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](http://image.intervention.io/api/encode) except data-url, so as of Intervention Image 2.5.1:

- jpg
- png
- gif
- tif
- bmp
- ico
- psd
- webp

### Advanced examples using callables

[](#advanced-examples-using-callables)

The **transformations** callable can be used to perform additional transformations using the Intervention Image object (see [Intervention Image docs](http://image.intervention.io/)).

```
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
----------

[](#change-log)

Breaking changes will be listed here. For other changes see commit log.

Credits
-------

[](#credits)

- [Mohamed Samir @ mohammed-samir.com](https://github.com/mohamed7sameer)
- Built with [Laravel Backpack addon skeleton](https://github.com/Laravel-Backpack/addon-skeleton)

License
-------

[](#license)

This project was released under the MIT license, so you can install it on top of any Backpack &amp; Laravel project. Please see the [license file](license.md) for more information.

However, please note that you do need Backpack installed, so you need to also abide by its [YUMMY License](https://github.com/Laravel-Backpack/CRUD/blob/master/LICENSE.md). 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](https://backpackforlaravel.com).

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

964d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4fef2179c1f40e9fb23cbc8e790e27e68a3467c6e5e10c11f18df57f1fcdb4c2?d=identicon)[mohamed7sameer](/maintainers/mohamed7sameer)

---

Top Contributors

[![mohamed7sameer](https://avatars.githubusercontent.com/u/39682366?v=4)](https://github.com/mohamed7sameer "mohamed7sameer (3 commits)")

---

Tags

laravelbackpackBackpack for LaravelBackpack AddonBackpackImageTraits

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mohamed7sameer-backpack-image-traits/health.svg)

```
[![Health](https://phpackages.com/badges/mohamed7sameer-backpack-image-traits/health.svg)](https://phpackages.com/packages/mohamed7sameer-backpack-image-traits)
```

###  Alternatives

[backpack/theme-tabler

UI for Backpack v6 that uses Tabler and Bootstrap v5.

35536.5k](/packages/backpack-theme-tabler)[backpack/activity-log

Activity Log for Backpack

3487.5k1](/packages/backpack-activity-log)[backpack/translation-manager

Translation Manager for Backpack

5118.9k1](/packages/backpack-translation-manager)[backpack/medialibrary-uploaders

Helper functions to save files with spatie media library

1373.3k](/packages/backpack-medialibrary-uploaders)[figlabhq/crud-resource-for-backpack

Build CRUD panels using fluent field definitions.

122.1k](/packages/figlabhq-crud-resource-for-backpack)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
