sands / uploadable
Laravel 5 模型的 Uploadable 特性
Requires
- php: >=5.5.9
- illuminate/support: ~5.0
- intervention/image: ^2.3
Suggests
- ext-gd: to use GD library based image processing.
- ext-imagick: to use Imagick based image processing.
This package is not auto-updated.
Last update: 2024-09-20 21:35:48 UTC
README
本包旨在简化 Laravel 应用中的上传处理。适用于 Laravel 5 和 Lumen。
关键特性
- 简单易用
- 可扩展
- 处理单个和多个上传
核心概念
- 所有上传都属于数据库中的一个记录
- 文件上传处理、操作和保存是通过一系列(类似于 gulp)过滤器来完成的。
- 你可以创建自己的过滤器。
安装
- 在你的应用程序中安装它:
composer require sands/uploadable - 将
Sands\Uploadable\UploadableServiceProvider::class添加到你的app/config/app.php文件中,在providers数组内。 - 发布包的迁移:
php artisan vendor:publish。 - 运行
php artisan migrate以创建uploads表。
用法
在你的 model 中使用 UploadableTrait 特性
use Sands\Uploadable\UploadableTrait; class Task extends Model { use UploadableTrait; ... }
在你的 model 文件中添加 $uploadableConfig 数组属性
protected $uploadableConfig = [ // handle <input type="file" name="images"/> or <input type="file" name="images[]" multiple/> 'images' => [ 'fix-image-orientation', // fixes the image orientation 'save', // saves the image prefixed wth "original-" 'resize-image:800', // resize the image to 800px width, maintain aspect ratio 'save:medium', // saves the image prefixed with "medium-" 'resize-image:400', // resize the image to 400px width, maintain aspect ratio 'save:small', // saves the image prefixed with "small-" 'thumbnail-image:140', // creates a 140px x 140px thumbnail of the image, resized then center cropped 'save:thumbnail', // saves the image prefixed with "thumbnail-" ], // handle <input type="file" name="images[main]"/> use 'dot' notation 'images.main' => [ ... ], ];
在模型的生命周期方法中,监听 saved 和 deleted 事件以将文件附加到/从 model 中分离
public static function boot() { self::saved(function (Task $task) { $task->attachFiles(); }); self::deleted(function (Task $task) { $task->detachFiles(); }); }
每次模型被保存时,此包都会寻找与请求中匹配的任何 uploadableConfig。如果有,上传的文件将通过配置的过滤器传递。每
内置过滤器
修复图像方向过滤器
修复图像方向。
过滤器名称: fix-image-orientation
调整图像大小过滤器
调整图像大小。
过滤器名称: resize-image
过滤器参数
number默认值: 800目标宽度,以像素为单位number默认值: 800目标高度,以像素为单位boolean默认值: true在调整大小时约束纵横比boolean默认值: false约束放大。在调整大小时不要放大图像
缩略图图像过滤器
调整图像以进行缩放和中心裁剪。
过滤器名称: thumbnail-image
过滤器参数
number默认值: 140目标宽度和高度,以像素为单位string默认值: #000000如果裁剪后存在空白区域,则使用此颜色填充
保存过滤器
将修改后的图像保存到 public/uploads 目录中的一个混淆目录中。每次触发 save 过滤器时,都会在 uploads 表中插入一条新记录。
过滤器名称: save
过滤器参数
string默认值: original-将此字符串作为文件名的前缀。
获取与模型相关的上传
通过 Laravel 的简单 morphMany 方法实现 model 和 uploads 之间的关系。你可以通过 $task->uploads 来访问文件。这将返回与 model 相关的所有文件及其变异。
要过滤与 model 相关的文件,你可以简单地执行 $task->uploads()->where() ...。
还有一个简写方法 files($type = null, $prefix = null),它接受 $type 参数,其中它过滤上传的文件类型(在上面的例子中是 images),以及 $prefix 参数,其中它会搜索具有前缀的上传。此方法将返回一个 Eloquent 查询,因此你可以进一步细化搜索。
upload 对象属性如下
{
"id": 5,
"uploadable_type": "App\\Task",
"uploadable_id": "2",
"type": "images",
"name": "original-2016-03-12-121732-am.png",
"mime_type": "image/png",
"url": "/uploads/MjAxNjAz/YXBwdGFzazI,/original-2016-03-12-121732-am.png",
"path": "/var/www/project/public/uploads/MjAxNjAz/YXBwdGFzazI,/original-2016-03-12-121732-am.png",
"created_at": "2016-03-30 21:52:20",
"updated_at": "2016-03-30 21:52:20",
}
获取上传图像的 URL 示例
<ul> @foreach($tasks as $task) <li> <img src="{{$task->files(null, 'thumbnail-')->first()->url}}" alt=""> {{$task->name}} </li> @endforeach </ul>
创建您自己的过滤器
创建自己的过滤器非常简单
- 创建一个实现
Sands\Uploadable\FilterInterface的class。 - 注册过滤器:
app('uploadable')->registerFilter('filterName', 'filterClass')。 - 没有第三步。
例如,假设我们想要将所有上传的图片旋转一定可配置的度数
创建一个 RotateImage 类
namespace App\Filters\RotateImage; use Sands\Uploadable\FilterInterface; class RotateImage implements FilterInterface { public function process($type, $file, $model) { $image = app('image')->make($file->getPathname()); $image->rotate($this->degrees); $image->save(); } public function __construct($degrees = 0) { $this->degrees = $degrees; } }
注册过滤器
namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Filters\RotateImage; class AppServiceProvider extends ServiceProvider { public function boot() { app('uploadable')->registerFilter('rotate-image', RotateImage::class); } .... }
在模型中使用它
use Sands\Uploadable\UploadableTrait; class Task extends Model { use UploadableTrait; protected $uploadableConfig = [ 'images' => [ 'rotate-image:63', // rotate the image by 63 degrees 'save', // save the image ], ]; ... }
就这样了!
贡献
非常欢迎您提交有关此项目的错误、功能请求或相关问题的反馈。如果您想贡献代码,请进行分支并提交 pull request。本项目遵循 PSR-2 编码风格。
许可协议
MIT 许可协议 (MIT) 版权所有 (c) 2016 Sands Consulting Sdn. Bhd.
在此特此授予任何人无限制地使用本软件及其相关文档文件(以下简称“软件”)的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供方提供软件的人员执行上述操作,前提是必须遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“现状”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该责任是基于合同、侵权或其他原因,以及软件或其使用或其他交易产生、产生于或与此相关。