cbcaio/image-attacher

以简单快捷的方式将上传的图片添加到模型中。处理并保存修改后的图片版本,如头像、缩略图、不同尺寸,由您决定。使用 flysystem 编写图片。

1.0.1 2016-01-20 15:15 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:09:46 UTC


README

License Quality Score Build Status Latest Stable Version

Total Downloads Monthly Downloads Daily Downloads

此包使用多态关系轻松将图片附加到模型上。基本上,您只需在模型中使用包中的一个特性即可(请参阅用法)。在后台发生的事情是,模型通过 MorphOne 关系与图片相关联,每次将图片持久化到相关表时,都会自动使用所选驱动程序(使用 Flysystem)写入文件,如果需要则更新。

安装

1 - 通过 Composer

要开始使用 Image Attacher,请将其添加到您的 composer.json 文件中作为依赖项

$ composer require cbcaio/image-attacher

2 - 提供者

安装 Image Attacher 后,在您的配置文件(config/app.php)中注册 CbCaio\ImgAttacher\Providers\ImgAttacherServiceProvider

'providers' => [
    // Other service providers...

    CbCaio\ImgAttacher\Providers\ImgAttacherServiceProvider::class,
],

3 - 配置

要发布配置文件,请运行以下命令

$ php artisan vendor:publish

此命令将创建 3 个新文件

  1. config/img-attacher.php:此文件包含 Image Attacher 配置。

    • path_to_save:定义图片的保存位置,相对于在 flysystem.php 中指定的驱动程序和本地位置。在使用之前,将解析此路径。:attribute 引用有关 attacherImage 模型的信息,而 owner_classowner_id 与关系的所有者类相关。这些是组织文件夹和确保删除正确图片所必需的。

      重要:此路径应仅由包使用,不要在相同文件夹中放置其他文件,否则可能会意外删除。

    • processing_stylesprocessing_style_routines:'routine' 代表一系列 'styles' 及其所需方法。每个 'style' 会保存原始图片的副本,并按其方法中指定的修改进行保存。它们可用于在将图片添加到模型时自动保存原始图片的不同版本(模型可以具有与单个关系相同图片的不同 '版本'/'styles')。

      • 例如,以下代码将保存原始图片和同一图片的 '缩略图版本',并在各自的解析 path_to_save 中保存::style 替换为 original_stylethumbnail

           $model->addImage(uploaded_file,'default_routine');
           ....
            'default_routine' =>
            [
                'original_style' => function ($image) {
                    return $image;
                },
                'thumbnail' => function ($image) {
                   $image->resize(null, 500, function ($constraint) {
                       $constraint->aspectRatio();
                       $constraint->upsize();
                   });
                   return $image;
                },
            ]
        
  2. database/migrations/2016_01_12_000000_create_attacher_images_table:这是一个迁移文件,用于创建将包含您在 MorphOne 关系中引用的模型的表以及有关图片的信息的表。

  3. config/flysystem.php:此文件相对于 Flysystem。这里定义了文件将如何以及在哪里写入。重要的是要说,此包仅使用 'local' 驱动程序进行了测试。

用法

要开始将图片附加到您的模型,您只需在模型中使用可用的特性之一(目前只有 hasImage)。

 class RandomModel extends Model
 {
     use hasImage;
 }

1 - hasImage 特性的基本用法

从上传的文件中向模型添加图片。
```php
    $upload = Input::file('image');
    $model->addImage($upload);
     // Directly from $request
    $model->addImage($request->file('image'));
     // With parameters
    $model->addImage($request->file('image', 'processing_style_routine','newfilename.jpg'));
```
从模型中检索图片。
```php
    $image = $user->getImage();
      // Path is relative
    $image->getPath('original_style);
    $image->getPath('thumbnail);
    
      // Url includes full path
    $image->getUrl('original_style);
    $image->getUrl('thumbnail);
``` 
添加另一张图片
```php
    // The same as adding, the package will identify if the model already has an image, delete the previous 
    images and update the relationship.
    $upload = Input::file('image2');
    $model->addImage($upload);
```
删除图片及其所有样式
```php
    $model->deleteImage();
```

变更日志

请参阅变更日志了解最近更改了哪些内容。

贡献

请参阅贡献指南行为准则以获取详细信息。

安全

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

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。