digbang/files

文件处理库

v2.0.0 2022-02-23 12:58 UTC

This package is auto-updated.

Last update: 2024-09-23 18:15:48 UTC


README

此库帮助处理文件和图片。文件使用Laravel的文件系统配置物理存储;并在数据库的表中逻辑存储。

使用方法

映射

$builder->manyToOne(File::class, 'propertyName'); $builder->belongsTo(ImageFile::class, 'imagePropertyName');

图片处理

每个ImageFile都有一个可选的父级图片(原始图片)。这允许将一系列精心挑选的图片附加到原始图片上。

例如

public function __construct(FileRepository $repository, ImageCurator $curator)
{
    $this->repository = $repository;
    $this->curator = $curator;
}

public function curateImage(ImageFile $originalImage): ImageFile
{
    if (! ($curatedImage = $originalImage->getCurated('SOME_IDENTIFICATION_KEY'))) {
        $originalImage->setContentGetter(function () use ($originalImage) {
            return $this->repository->getContents($originalImage->getId());
        });
    
        $curation = $this->curator
            ->resize(WIDTH, HEIGHT)
            ->interlace()
            ->optimize() //not yet implemented
            ->buildCuration();
    
        $curatedImage = $originalImage->curate('SOME_IDENTIFICATION_KEY', $curation);
        $curatedImage->setContentGetter(function () use ($curatedImage) {
            return $this->repository->getContents($curatedImage->getId());
        });
    
        $this->repository->persist($curatedImage);
    }
    
    return $curatedImage;
}

imageCurator可以扩展或替换。你也可以实现另一个curator与现有的curator并行使用。

唯一的要求是curator应该返回一个回调数组,Curation对象可以应用于图片。