monstrex/media-storage

Laravel 媒体存储

0.14 2021-01-11 17:20 UTC

This package is auto-updated.

Last update: 2024-09-24 20:30:01 UTC


README

这个 Laravel 扩展包允许你在项目中管理不同的媒体文件。该包受到了 Spatie Medialibrary 包的启发。但这个包也可以处理不绑定到特定模型记录的文件。因此,你可以使用 Spatie Medialibrary 的方式存储文件,或者你可以使用独立的媒体集合,完全不与任何模型相关。此外,该包还可以处理一些图像转换。

要求

Laravel 8.*

安装

composer require monstrex/media-storage

发布配置

$ php artisan vendor:publish --provider="MonstreX\MediaStorage\MediaStorageServiceProvider" --tag="config"

你可以在配置文件中使用自定义生成器来生成目标文件名,以使用你自己的方式
'url_generator' => MonstreX\MediaStorage\Services\URLGeneratorService::class,

创建迁移

$ php artisan vendor:publish --provider="MonstreX\MediaStorage\MediaStorageServiceProvider" --tag="migrations"
$ php artisan migrate

添加

$user1 = User::find(1);
// Add file from request and bind the file to model using collection name (string)
$collection = Media::add(request()->file('myfile'))->model($user1)->collection('new-gallery')->create();
// Add file from path and disk
$collection = Media::add('images/1.jpg', 'local')->collection('local-file')->create();
// Add file using Collection ID (int)
$collection = Media::model($model)->add($files)->collection(5403)->create();
// Add file with custom properties
$collection = Media::add($file)->props(['title' => 'Novus news','alt' => 'Image #2'])->collection('images')->create();
// Add files using noname collection. You can get collection ID from returned collection - collection_id field. 
$collection = Media::add($files)->create();
// Will save original source file as is
$collection = Media::add('images/1.jpg', 'local')->preserveOriginal()->create();
// Will replace target file if exist. 
$collection = Media::add($file)->replaceFile()->create();
// Will replace target file name using language table from config file
$collection = Media::add($file)->transliterate('ru')->collection('cyrillic-gallery')->create();

检索

// Get path by record ID
$path = Media::find(1)->path(); 
// Get path by MEDIA ID
$path = Media::id(3)->path();   
// Get relative URL
$url = Media::id(3)->url();
// Get full URL
$url = Media::id(3)->fullUrl();
// Generate conversion if not exist and return URL     
$url = Media::id(3)->crop(250,300)->url(); 
// Get collection by collection ID 
$collection = Media::collection(5403)->get();
// Get collection by collection Name
$collection = Media::model($user1)->collection('new-gallery')->get();
// Get All media entries
$collection = Media::all();
// Get all properties
Media::id(3)->props();
// Get certain propery
Media::id(3)->prop('title');
Media::id(3)->prop('setting.width');

修改

// Set properties
Media::id(3)->props(['title' => 'New title'])->save();
// Set one property
Media::id(3)->prop('settings.width', 500)->save();
// Set order
$collection = Media::collection('new-gallery')->get();
$collection[0]->order(1);
$collection[1]->order(2);
$collection[2]->order(3); 
Media::save($collection);

删除

// Delete one media entry
Media::id(3)->delete();
// Delete whole collection
Media::collection(5403)->delete();
// Delete collection binded to model
Media::model($user1)->collection('new-gallery')->delete()
// Delete All media entries
Media::deleteAll();

测试

提供所有测试。