voerro / laravel-file-uploader
此包已被弃用且不再维护。未建议替代包。
一个简化 Laravel 文件和图片上传的小助手类
v1.2.0
2020-02-04 11:39 UTC
Requires
- php: ^7.0
- illuminate/http: 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || 5.9.* || ^6.
- illuminate/support: 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || 5.9.* || ^6.
- intervention/image: ^2.4
Requires (Dev)
- orchestra/testbench: ^3.5
- phpunit/phpunit: ^6.5
README
一个非常简单的助手类,让这些任务变得容易如履平地
- 替换旧文件为新文件
- 上传时减小图片大小
- 从存储中删除文件
- 确定文件是否为图片
安装
通过 composer
composer require voerro/laravel-file-uploader
对于 Laravel 5.5 以下的版本
将 FileUploaderServiceProvider
添加到你的 config/app.php
配置文件中的 providers
数组
Voerro\FileUploader\FileUploaderServiceProvider::class,
然后将 Facade
添加到 aliases
数组
'FileUploader' => Voerro\FileUploader\FileUploaderFacade::class,
基本用法
像这样导入 FileUploader
类
use Voerro\FileUploader\FileUploader;
将上传的文件(一个 Illuminate\Http\UploadedFile
实例)传递给 make
方法,然后链式调用 upload
方法,这将返回新存储文件的路径。
$path = FileUploader::make($file)->upload();
方法
静态方法
初始化
::make(Illuminate\Http\UploadedFile $file)
删除文件(该方法检查文件是否存在以消除可能的错误)
::delete(string $filePath, string $storage = 'public')
确定文件是否为图片。将上传文件实例或指向文件的路径的字符串传递给该方法
::isImage($file, $storage = 'public')
以下方法应该在 make
方法之后链式调用。
->upload(string $path = '', string $storage = 'public')
以指定名称上传文件
->uploadAs(string $filename, string $path = '', string $storage = 'public')
替换旧文件
->replace(string $oldFilePath, string $path = '', string $storage = 'public')
替换旧文件,并将新文件存储在指定名称下
->replaceAs(string $oldFilePath, string $newFilename, string $path = '', string $storage = 'public')
如果图片大小超过指定宽度和/或高度,则减小图片大小(将保持宽高比)。当对非图片文件调用此方法时,不会发生任何操作,因此无需在调用此方法之前手动检查文件是否为图片。
->downsize(integer $maxWidth, integer $maxHeight)
裁剪并调整图片大小以适应指定尺寸(与 Intervention Image 的 ->fit()
相同)
->fit(integer $width, integer $height, boolean $dontUpsize = false)
在调用上述任何方法之前调用此方法,例如
FileUploader::make($image)->downsize(200, 200)->replace('old_image_file.jpg');
许可证
这是一个开源软件,根据 MIT 许可证 授权。