gterrusa/frontendmedialibrary

1.1.4 2022-02-14 19:55 UTC

This package is auto-updated.

Last update: 2024-09-06 23:21:12 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

FrontendMedialibrary 是一个特质,使得在 Laravel 模型中使用 Spatie Medialibrary 的媒体在前端框架中更容易访问。查看 contributing.md 了解待办事项列表。

安装

通过 Composer

$ composer require gterrusa/frontendmedialibrary

用法

设置您的模型

/**
 * Model must be using Spatie Medialibrary
 */

class User extends Model implements HasMedia
{
    use InteractsWithMedia, HasFrontendMedia;
    
    // If you would like the frontendMedia attribute included with your model automatically
    protected $appends = ['frontendMedia'];
    
    
    /**
     * Spatie medialibrary media collections
     * any media collections can be set up here
     * this is just an example.
     */
    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('avatar')->singleFile();

        $this->addMediaCollection('user_gallery');
    }

    /**
     * Spatie medialibrary media conversions
     * any media conversions can be set up here
     * this is just an example
     */
    public function registerMediaConversions(Media $media = null): void
    {
        $this->addMediaConversion('optimized')
            ->fit(Manipulations::FIT_MAX, 1500, 1500);

        $this->addMediaConversion('thumb')
            ->fit(Manipulations::FIT_MAX, 250, 250);
    }
}

示例结果

** HasFrontendMedia 将自动获取所有已注册的集合、转换和自定义属性。它们不必是这些确切的属性。这只是一个输出结构的示例。

>>> $user->frontendMedia;
=> Illuminate\Support\Collection {#4382
     all: [
       "avatar" => Illuminate\Support\Collection {#4439
         all: [
           [
             "src" => "http://frontend-medialibrary.local/storage/1/41301408.jpeg",
             "conversions" => Illuminate\Support\Collection {#4425
               all: [
                 "thumb" => "http://frontend-medialibrary.local/storage/1/conversions/41301408-thumb.jpg",
                 "optimized" => "http://frontend-medialibrary.local/storage/1/conversions/41301408-optimized.jpg",
               ],
             },
             "custom_properties" => Illuminate\Support\Collection {#4384
               all: [
                 "alt" => "my alt tag",
               ],
             },
           ],
         ],
       },
       "user_gallery" => Illuminate\Support\Collection {#4469
         all: [
           [
             "src" => "http://frontend-medialibrary.local/storage/2/49268400_10155665639226152_4845558111659884544_n.jpg",
             "conversions" => Illuminate\Support\Collection {#4443
               all: [
                 "thumb" => "http://frontend-medialibrary.local/storage/2/conversions/49268400_10155665639226152_4845558111659884544_n-thumb.jpg",
                 "optimized" => "http://frontend-medialibrary.local/storage/2/conversions/49268400_10155665639226152_4845558111659884544_n-optimized.jpg",
               ],
             },
             "custom_properties" => Illuminate\Support\Collection {#4456
               all: [],
             },
           ],
           [
             "src" => "http://frontend-medialibrary.local/storage/3/68544499_10156079858431152_5954628979427115008_n.jpg",
             "conversions" => Illuminate\Support\Collection {#4444
               all: [
                 "thumb" => "http://frontend-medialibrary.local/storage/3/conversions/68544499_10156079858431152_5954628979427115008_n-thumb.jpg",
                 "optimized" => "http://frontend-medialibrary.local/storage/3/conversions/68544499_10156079858431152_5954628979427115008_n-optimized.jpg",
               ],
             },
             "custom_properties" => Illuminate\Support\Collection {#4470
               all: [],
             },
           ],
         ],
       },
     ],
   }

变更日志

请参阅 changelog 了解最近的变化信息。

测试

$ composer test

贡献

请参阅 contributing.md 了解详细信息及待办事项列表。

安全性

如果您发现任何安全相关的问题,请通过作者的电子邮件而不是使用问题跟踪器。

致谢

许可

许可。请参阅 许可文件 了解更多信息。