labspace/ upload-api
labspace的upload-api软件包
Requires
- php: ^7.0
README
母项目需要完成的事情
步骤.0
图片处理intervention先安装设置好、jwt机制要准备好、cors也要准备好
============
步骤.1
安装软件包
composer require labspace/upload-api
============
步骤.2
到config/app.php的providers中添加
Labspace\UploadApi\UploadApiServiceProvider::class,
============
步骤.3
在app\App\Providers\AppServiceProvider的boot中新增base64图片检查code
(记得use Validator)
Validator::extend('base64image', function ($attribute, $value, $parameters, $validator) { $explode = explode(',', $value); $allow = ['png', 'jpg', 'svg']; $format = str_replace( [ 'data:image/', ';', 'base64', ], [ '', '', '', ], $explode[0] ); // check file format if (!in_array($format, %', $explode[1])) { return false; } return true; });
============
步骤.4
php artisan vendor:publish --tag=config
会新增专属config文件 labspace-upload-api.php,里面可以设置登录user model的位置
============
步骤.5
迁移新增
php artisan vendor:publish --tag=migration-temp
====================
使用说明
[上传图片(file) - POST] file:文件(必填) width:宽度(必填) type:类型 token:用户token
http://[服务器地址]/lab/api/upload/image/file
{ "status": true, "data": { "filepath": "/avatar/1/20200121182207182207.jpg" }, "success_code": "SUCCESS" }
[上传图片(base64) - POST] file:base64字符串(必填) width:宽度(必填) type:类型 token:用户token extension:文件类型
http://[服务器地址]/lab/api/upload/image/base64
{ "status": true, "data": { "filepath": "/avatar/1/20200121182207182207.jpg" }, "success_code": "SUCCESS" }
[上传视频(file) - POST] file:文件(必填) type:类型 token:用户token
http://[服务器地址]/lab/api/upload/video/file
{ "status": true, "data": { "filepath": "/avatar/1/20200121182207182207.jpg" }, "success_code": "SUCCESS" }
====================
[文件管理器]
filesystems.php需要新增文件管理器file-upload存储位置
'file-upload' => [ 'driver' => 'local', 'root' =>storage_path('app/public/manager'), ],
==========
helper.php需要有以下function
//删除特殊符号 function clean_file_name($filename){
$bad = array(
"<!--",
"-->",
"'",
"<",
">",
'"',
'&',
'$',
'=',
';',
':',
'@',
'?',
'/'
);
$filename = str_replace($bad, '', $filename);
return stripslashes($filename);
}
/**
-
获取缩略图 @param path 字符串 @return string **/ function get_thumbnail($path){
$arr = explode("/",$path);
$filename = end($arr);
$new_path = ''; foreach($arr as $key=> $item){ if($key == (count($arr) -1 )){ $new_path .= 'thumbs/'.$filename; } else { $new_path .= $item.'/'; } } return $new_path; }