feugene/laravel-files

此包的最新版本(v1.0.0)没有提供许可证信息。

Laravel 文件模型

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

This package is auto-updated.

Last update: 2024-09-24 04:38:44 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version

Build Status

Maintainability Test Coverage

信息

Laravel 模型附加文件模型。实现了与原生文件的工作。

安装

  • composer require feugene/laravel-files

示例

  • 将 ServiceProvider 添加到您的应用中:config/app.php(部分:providers

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

    或者如果 Laravel >= 5.7 - 使用服务发现。

  • 运行 php artisan migrate 添加文件表

文件上传

仅简单上传

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

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

通过 after actions 将包装文件上传到模型

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

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

将包装文件上传到自定义模型和自定义路径

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

关系和图像操作

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