PHP code example of lloricode / laravel-imageable

1. Go to this page and download the library: Download lloricode/laravel-imageable 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/ */

    

lloricode / laravel-imageable example snippets


// ...
use Illuminate\Database\Eloquent\Model;
use Lloricode\LaravelImageable\Models\Traits\ImageableTrait;

class MyModel extends Model
{
    use ImageableTrait;
    
    
    // ...
    




return [


    'migration' => [
        'table_name' => 'images',
    ],

    'cache' => [
        'enable' => true,
        'prefix' => 'imageable',
    ],
];

   use Lloricode\LaravelImageable\Models\Image;
        
    $folder = Image::PATH_FOLDER .'/';
    Storage::disk('local')->deleteDirectory($folder);
    Storage::disk('public')->deleteDirectory($folder);


        // files to be upload 
        $imageFiles = [
            //  instance of \Illuminate\Http\UploadedFile
            $imageFile1,
            $imageFile2,
        ];

        $myModel->uploads($imageFiles)// must array, 
            ->each([
                [
                    'size_name' => 'banner', // this will be use in calling image
                    'spatie' => function ($image) {

                        $image// abstract of spatie/image https://github.com/spatie/image
                        ->optimize()->width(100)-> // ....

                        return $image;
                    },
                ],
                [
                    'size_name' => 'thumbnail', // this will be use in calling image
                    'spatie' => function ($image) {

                        $image// abstract of spatie/image https://github.com/spatie/image
                        ->greyscale()-> // ....

                        return $image;
                    },
                ],
            ])->contentTypes([
                'image/png',
                'image/jpg',
                'image/jpeg',
            ])->disk('public')// any disk in config('filesystem) except cloud
            ->category('banner')// optional
            ->save(); // save mutiple 

    $images = $myModel->getImages($size_name = null, $category = null, $group = null); // a size_name use when uploading,
    <img href={{ $images->first()->source }} />

$myModel->deleteImages($size_name = null, $category = null, $group = null);

# via http delete
# using `source_delete` return from collection


        $myModel->uploads()
            ->each([
                [
                    'size_name' => 'from', // this will be use in calling image
                    'spatie' => function ($image) {

                        // ....

                        return $image;
                    },
                ],
                [
                    'size_name' => 'to', // this will be use in calling image
                    'spatie' => function ($image) {

                         // ....

                        return $image;
                    },
                ],
                [
                    'size_name' => 'other', // this will be use in calling image
                    'spatie' => function ($image) {

                         // ....

                        return $image;
                    },
                ],
            ])
            ->regenerate('from'); // will regenerate `to` and`other`