saad/laravel-model-images

这个库将使保存和上传模型图片变得容易

v1.1.4 2023-05-07 07:23 UTC

This package is auto-updated.

Last update: 2024-09-07 14:20:55 UTC


README

模型图片

License

此包使保存(上传或从存储)具有任何尺寸集的图片并将其附加到您的模型变得非常简单,只需几行代码即可
对于任何自定义,也完全控制保存过程

依赖项

此库使用两个提供者,一个是intervention/image库,另一个是saad/image(我的包)用于图像处理。默认提供者是我的包(saad/image),但您可以根据需要在这两者之间切换,并且您还可以创建自己的驱动程序并使用它。

安装

	composer require saad/laravel-model-images

基本用法

1- 模型设置

  • 假设我们有一个模型,例如用户模型有一个名为 image 的字符串字段,该字段将包含其个人资料图片

    我们所需做的就是在用户模型文件 User.php

    1. 实现 Saad\ModelImages\Contracts\ImageableContract
    2. 使用特性 Saad\ModelImages\Traits\HasImages
    3. 通过定义一个静态方法 imageableFields() 为此模型定义图像字段,该方法将返回包含模型图像字段的数组
// User.php File
class User extends Autheticatable implements ImageableContract
{
    use HasImages;
    
    public static function imageableFields() {
        return ['image'];
    }
}

2- 存储图片

现在要设置用户个人资料图片,假设在个人资料控制器中我们有一个名为 uploadUserProfile 的方法,用于响应上传图片请求,还有一个名为 setUserProfile 的方法来从存储图像文件设置个人资料图片

// ProfilesController.php File
class ProfilesController
{		
    // Save Image From Upload Request    
    public function setUserProfile(Request $request, User $user) {
        // You can Validate
        $image = $request->file('image');
        if ($image) {
            $user->image = $image;
        }
        
        $user->save();
    }
    
    // Set Image from image file on storage
    public function setUserProfile() {
        $user = User::find(1);
        $user->image = storage_path('app/images/profile.png');
        $user->save();
    }
}

3- 公共链接

好的,现在我们已经存储了用户个人资料图片,那么我们如何获取我们的用户个人资料图片的公共链接
这非常简单,只需使用 $user->getImagePublicLink() 即可,它会获取个人照片的URL
默认情况下,所有图片都将存储在 public 目录下的以下目录中 images/uploads

此库使用约定命名 getImageFieldPublicLink()
因此,如果我们有一个名为 profile_photo 的图像字段,方法将是 getProfilePhotoPublicLink()

自定义

1- 多个图片尺寸

要设置自定义尺寸,如缩略图或不同尺寸的上传图片,您只需在您的模型上定义 imageFieldImageSizes() 方法即可

		
// User.php File
class User extends Autheticatable implements ImageableContract
{
    use HasImages;
    ...
    public function imageImageSizes() {
        return [
            [512, 512], // Full size version
            [256, 256], // Thumbnail version
            [46, 46],   // Thumbnail version
        ];
    }
}
		

获取公共链接
$user->getImagePublicLink() => 512x512
$user->getImagePublicLink('256x256') => 256x256
$user->getImagePublicLink('46x46') => 46x46

或者,您可以使用通用方法 getPublicLink($field)
$user->getPublicLink('image') => 512x512
$user->getPublicLink('image', '256x256') => 256x256
$user->getPublicLink('image', '46x46') => 46x46

2- 自定义保存目录

要设置自定义保存目录(在公共文件夹内),请定义方法

imageGeneralSavePath() 此方法将设置所有模型图像字段的保存路径

imageFieldNameSavePath 用于设置每个字段的不同路径的命名约定

		
// User.php File
class User extends Autheticatable implements ImageableContract
{
    use HasImages;
    ...
    // path for saving user profile images
    public function imageImageSavePath() :string
    {
        return 'images/users/profile';
    }
    
    // user model default saving directory for all model image fields if any
    public function imageGeneralSavePath() :string
    {
        return 'images/users';
    }
}
		

3- 自定义保存选项

您可以通过定义这些方法来控制保存格式、质量和PNG过滤器

imageSaveExtension() 定义图像格式默认 jpg
imageSaveQuality() 定义图像质量默认 70 对于 jpg

对于jpg质量将是从 0 到 100低到高 质量
对于PNG质量,将从0到9,质量从高到低

imagePNGFilter()定义PNG图像过滤器默认为PNG_NO_FILTER

		
// User.php File
class User extends Autheticatable implements ImageableContract
{
    use HasImages;
    ...
    public function imageSaveExtension() :string
    {
        return 'png';
    }
    
    public function imageSaveQuality() :int
    {
        return 7;
    }
    
    public function imagePNGFilter() :int
    {
        return PNG_ALL_FILTERS;
    }
}
		

4- 自定义保存提供者

此软件包附带两个图像处理提供者

提供者
SaadImageProvider这是默认提供者,它使用我的图像包saad/image进行图像处理过程
InterventionImageProvider提供者,用于著名的包intervention/image进行图像处理过程

定义自定义提供者
您可以通过实现此接口Saad\ModelImages\Contracts\ImageProviderContract来定义自己的自定义提供者类

更改提供者
您可以通过在模型中定义imageSaveProvider()方法来更改提供者,该方法应返回提供者实例。

注意

在宽度和高度被定义并且与原始宽高比不同的情况下,intervention和我的图像库处理图像缩放的方式有所不同。

saad/image通过将原始图像居中然后根据需要调整大小和裁剪来调整图像到新尺寸

intervention/image将图像拉伸到新尺寸或将宽高比保持不变,因此它将尊重一个维度并根据原始宽高比自动设置另一个维度

如果此说明不够清楚,您可以尝试使用两个提供者并查看结果。

		
// User.php File
class User extends Autheticatable implements ImageableContract
{
    use HasImages;
    ...
    public function imageSaveProvider() :ImageProviderContract
    {
        return new InterventionImageProvider;
    }

}
		

4- 默认图像

每个模型图像字段都应该有一个默认图像,因此考虑以下情况

1-记录没有图像当我们尝试获取尚未附加图像的记录的图像时

2- 记录有图像但文件已删除或不存在当我们尝试获取记录的图像但此图像文件已删除或不存在时

因此,我们应该为每个字段定义默认图像,并且如果发生上述情况之一,此软件包将自动返回此默认图像

设置默认图像:定义默认图像的过程与将图像附加到模型的过程相同,唯一不同的是我们告诉模型我们正在定义默认图像
通过在模型上调用静态方法settingDefaultImage(),例如:User::settingDefaultImage()
在这里我们可以看到它是如何工作的

// ProfilesController.php File
class ProfilesController
{		
    // Set image field default image
    public function setDefaultProfileImage() {
        // telling User model we are defining default image
        User::settingDefaultImage();
        
        // assign default image
        $user = new User;
        $user->image = storage_path('app/images/default_profile.png');
    }
}