feugene/laravel-files

There is no license information available for the latest version (v1.0.0) of this package.

Laravel files model

v1.0.0 2019-01-05 17:39 UTC

This package is auto-updated.

Last update: 2024-10-24 04:50:45 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version

Build Status

Maintainability Test Coverage

Information

Add-on file model for Laravel models. Implements work with native files.

Install

  • composer require feugene/laravel-files

Examples

  • Add ServiceProvider into your app: config/app.php (section: providers)

        // ...
        Feugene\Files\ServiceProvider::class,

    or if Laravel >= 5.7 - use service discover.

  • Run php artisan migrate for add table for file

File upload

Only simple upload:

public function store()
{
    $list = app(UploadService::class)
        ->upload();

    return [
        'success' => $list->isNotEmpty(),
        'files'   => $list,
    ];
}

Upload with wrapped file to model via after actions:

public function store()
{
    $list = app(UploadService::class)
        ->setAfterAction(AfterModelAction::class)
        ->upload();

    return [
        'success' => $list->isNotEmpty(),
        'files'   => $list,
    ];
}

Upload with wrapped file to custom model and custom path:

public function store(int $sectionId)
{
    /** @var Section $section */
    $section = Section::findOrFail($sectionId);

    $this->authorize('uploadFile', $section);

    $upload = new Upload($section);
    $path = $upload->getUploadPath();

    $list = app(UploadService::class)
        ->setPath($path)
        ->setAction(BeforeBaseAction::class, 'before')
        ->setAfterAction(AfterModelAction::class)
        ->setAfterAction(function ($file) use ($section) {
            /** @var \Feugene\Files\Models\File $file */
            
            return File::create([
                'section_id' => $section->id,
                'author_id'  => \Auth::id(),
                'name'       => $file->getBaseFile()->getFilename(),
                'file_id'    => $file->getKey()
            ]);
        })
        ->upload();

    return [
        'success' => $list->isNotEmpty(),
        'files'   => $list,
    ];
}

Relations and image operations

// find image type from DB
/** @var ImageFile $file */
$file = ImageFile::find($id);

// create child relation with clone image     
$child = $file->createChild();

// Image scale to 50% from original
// without relation
$child = $file->scale(new ScaleModificator(50));
// create child relation 
$child = $file->createChild(new ScaleModificator(50));

// Image resize to 50px by width
// without relation
$child = $file->resize(new ResizeModificator(50));
// create child relation 
$child = $file->createChild(new ResizeModificator(50));


// Image resize to 50px by height
// without relation
$child = $file->resize(new ResizeModificator(null, 50));
// create child relation 
$child = $file->createChild(new ResizeModificator(null, 50));


// Image resize to 50px by height and 100px by width and bestFit options
// without relation
$child = $file->resize(new ResizeModificator(100, 50));
// create child relation 
$child = $file->createChild(new ResizeModificator(100, 50));


// Image resize to 50px by height and 100px by width and important size (100x50)
// without relation
$child = $file->resize(new ResizeModificator(100, 50, false));
$child = $file->resize(new ResizeModificator(100, 50, false), true);  // if original image is smaller than target image
// create child relation 
$child = $file->createChild(new ResizeModificator(100, 50, false));