PHP code example of eleven59 / backpack-image-traits

1. Go to this page and download the library: Download eleven59/backpack-image-traits library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eleven59 / backpack-image-traits example snippets


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);
    }
}

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)
    ]);
}

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);
        },
    ]);
}

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);
        },
    ]);
}