escapework/laramedias

一个集成Glide的Laravel包,用于轻松管理项目中的媒体。

0.9.0 2022-04-21 21:18 UTC

README

Laramedias

Latest Stable Version Downloads Travis - Build Status License MIT Scrutinizer Quality Score

一个集成Glide和简化Laravel项目中媒体管理的Laravel包。

    <img src="{{ $product->present->picture(500, 300, 'crop') }}" alt="Easy media management">

版本兼容性

安装

通过Composer

$ composer require escapework/laramedias:"0.7.*"

配置

并执行以下代码

$ php artisan vendor:publish --provider="EscapeWork\LaraMedias\Providers\MediasServiceProvider"
$ php artisan migrate

配置说明

'disk' => null, // if you dont want to use filesystems.default disk config, change it here...
                // ...for saving on another disk

'max_size' => [
    'width'  => 2000, // when creating medias, the images will be resized to this max_size...
    'height' => 2000, // ...for reducing disk usage
],

'url'  => 'medias',  // if you want to change the laravel glide medias URL
'dir'  => 'medias',  // if you want to change the default directory where the medias are saved
'path' => 'general', // if you want to change the directory where the multipleMedias are saved (you will undestand this later)

使用方法

此包允许您轻松地在laravel模型中使用媒体。有两种基本的使用方式

一个模型有多个媒体

假设您有一个需要多个媒体的Product模型。您必须这样做

  • 在您的模型中导入以下特性;
use EscapeWork\LaraMedias\Traits\Medias;

class Product extends Model
{

    use Medias;
}

现在,您可以这样做

上传并创建多个媒体
$product->uploadMedias($request->file('medias'));
遍历您的媒体

$product->medias将是一个默认的Laravel集合,包含EscapeWork\LaraMedias\Models\Media模型,您可以使用任何集合方法。

@foreach ($product->medias as $media)
    <?php /*
    all media models have an presenter class so you can easily show the image in different forms
    ->picture($width, $height, $fit)
    */ ?>

    <img src="{{ $media->present->picture(600, 300, 'crop') }}">
@endforeach

每个$media对象都将是一个LaraMedias\Models\Media eloquent模型,它将有一个用于轻松显示图片的呈现器(见上面示例)。

示例中的参数是Glide的宽度(w),高度(h)和fit。您可以在这里看到简单的示例(http://glide.thephpleague.com/1.0/simple-example/)。

如果您的模型被删除,所有媒体也将被删除。

删除媒体

要删除媒体,只需调用方法removeMedias

$product->removeMedias([1, 2]); // just pass the IDs

要删除所有媒体,只需不带任何参数调用removeMedias方法。

$product->removeMedias();

事件

在这些使用场景中,将触发以下事件

一个模型有一个媒体字段

假设您有一个Banner模型并希望为其上传单个图片。使用Laramedias,您可以这样做

首先,配置config/medias.php文件

    'models' => [
        'banners' => [
            'model'  => 'App\Models\Banner',
            'fields' => ['banner'] // here you have to put the fields in your model which use medias
        ],
    ],

其次,在您的Banner模型中使用EscapeWork\LaraMedias\Traits\Medias特性。

use EscapeWork\LaraMedias\Traits\Medias;

class Banner extends Model
{

    use Medias;
}

然后,您可以使用uploadSingleMedia方法。

$banner = Banner::find(1);
$banner->uploadSingleMedia($request->file('banner'), 'banner'); // the second parameter is the field name to be updated
$banner->save();

之后,您可以使用media辅助方法显示您的横幅。

<img src="{{ media_path($banner, 'banner', 1920, 400, 'crop') }}" alt="...">

我还建议在这种情况下使用呈现器。您可以使用自定义设置或使用此包,它使它非常容易。

然后,您可以这样设置

use EscapeWork\LaravelSteroids\Presenter;

class BannerPresenter extends Presenter
{
    public function banner($w = 100, $h = 50, $fit = 'fit')
    {
        return media_path($this->model, 'banner', $w, $h, $fit);
    }
}

然后,您会喜欢这样

    <img src="{{ $banner->present->banner(1920, 500, 'crop') }}">

贡献

请随时提交任何pull request/issue,包括您的想法/错误/建议。

许可

查看许可文件。