turahe/media

v1.0.3 2024-09-10 08:35 UTC

This package is auto-updated.

Last update: 2024-09-14 06:09:41 UTC


README

一个简单的方法将文件附加到您的eloquent模型,内置图像处理!

安装

您可以通过composer安装此包

composer require turahe/media

安装后,您应该发布提供的资源以创建必要的迁移和配置文件。

php artisan vendor:publish --provider="Turahe\Media\MediaServiceProvider"

关键概念

在继续之前,应该理解一些关键概念

  • 媒体可以是任何类型的文件,从jpeg到zip文件。在尝试上传文件之前,您应该在应用程序的验证逻辑中指定任何文件限制。

  • 媒体作为其自己的实体上传。在创建时,它不属于系统中的另一个模型,因此项目可以独立管理(这使得它是媒体管理器的完美引擎)。

  • 媒体必须附加到模型才能建立关联。

  • 媒体项目绑定到“组”。这使得将多种类型的媒体关联到模型变得容易。例如,一个模型可能有一个“图像”组和“文档”组。

  • 您可以使用转换来处理图像。您可以在媒体项目与模型关联时指定要执行的转换。例如,您可以为将图像附加到模型的“画廊”组时运行的“缩略图”转换进行注册。

  • 转换是全局注册的。这意味着它们可以在您的应用程序中重用,例如,Post和User可以具有相同大小的缩略图,而无需两次注册相同的转换。

使用方法

上传媒体

您应该使用Turahe\Media\MediaUploader类来处理文件上传。

默认情况下,此类将文件更新到媒体配置中指定的磁盘。它将它们保存为其原始文件名的净化版本,并在数据库中创建一个包含文件详细信息的媒体记录。

您还可以在上传之前自定义文件的一些属性。

$file = $request->file('file');

// Default usage
$media = MediaUploader::fromFile($file)->upload();

// Custom usage
$media = MediaUploader::fromFile($file)
    ->useFileName('custom-file-name.jpeg')
    ->useName('Custom media name')
    ->upload();

将媒体与模型关联

为了将媒体项目与模型关联,您必须首先包含Turahe\Media\HasMedia特性。

class Post extends Model
{
    use HasMedia;
}

此特性将为您的模型和媒体模型设置关系。其主要目的是提供用于附加和检索媒体的流畅API。

一旦包含,您就可以像下面演示的那样将媒体附加到模型。附加媒体方法的第一个参数可以是媒体模型实例、id或模型/ids的可迭代列表。

$post = Post::first();

// To the default group
$post->attachMedia($media);

// To a custom group
$post->attachMedia($media, 'custom-group');

从模型中分离媒体

要从不关联媒体,您应调用提供的detachMedia方法。

// Detach all the media
$post->detachMedia();

// Detach the specified media
$post->detachMedia($media);

// Detach all the media in a group
$post->clearMediaGroup('your-group');

如果您想删除媒体项,您应该像删除应用程序中的任何其他模型一样进行操作。

Media::first()->delete();

这样做将从文件系统中删除文件,并删除媒体项与您的应用程序模型之间的任何关联。

检索媒体

HasMedia特性的另一个功能是检索媒体。

// All media in the default group
$post->getMedia();

// All media in a custom group
$post->getMedia('custom-group');

// First media item in the default group 
$post->getFirstMedia();

// First media item in a custom group
$post->getFirstMedia('custom-group');

除了检索媒体项目外,您还可以直接从您的模型中检索媒体模型的属性。

// Url of the first media item in the default group
$post->getFirstMediaUrl();

// Url of the first media item in a custom group
$post->getFirstMediaUrl('custom-group');

处理图像

此包提供了一种流畅的API来处理图像。您可以在媒体附加到组时指定要执行“转换”的模型。它使用熟悉的intervention/image库,因此可以使用库提供的所有选项来处理图像。

要开始,您应该在应用程序的服务提供者之一中注册一个转换

use Intervention\Image\Image;
use Turahe\Media\Facades\Conversion;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Conversion::register('thumb', function (Image $image) {
            return $image->fit(64, 64);
        });
    }
}

一旦注册了转换,您应配置一个媒体组,以便在媒体附加到您的模型时执行转换。

class Post extends Model
{
    use HasMedia;
    
    public function registerMediaGroups()
    {
        $this->addMediaGroup('gallery')
             ->performConversions('thumb');
    }
}

现在,当一个媒体项目被附加到“相册”组时,将生成一个转换后的图像。您可以按照以下示例获取转换后图像的URL

// The thumbnail of the first image in the gallery group
$post->getFirstMediaUrl('gallery', 'thumb');