123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- declare (strict_types=1);
- namespace app\system\controller;
- // +----------------------------------------------------------------------
- // | [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018-2020 rights reserved.
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // | Date: 2020-08-31 15:05
- // +----------------------------------------------------------------------
- use app\Request;
- use app\system\controller\AuthController;
- use library\lib\upMp4;
- use library\utils\Qiniu;
- use app\model\api\Sys as SysModel;
- use think\Image;
- class Upload extends AuthController{
- /**
- * 七牛上传
- * @param Request $request
- */
- public function index(Request $request){
- $file = $request->file('file');
- if (empty($file)) {
- return app('json')->fail("未上传文件");
- }
- $code = $request->post("code","");
- //上传到本地
- // if($code=="invite"){
- // $sysData = (new SysModel())->where("id",1)->find();
- // $size = $file->getSize();
- // if(!empty($size) && $size>2*1024*1024){
- // return app('json')->fail("图片不能超过{$uploadConfig["image"]["size"]}M");
- // }
- //
- // $ext = $file->extension();
- // if(!in_array($ext, ["jpg","jpeg","png"])){
- // return app('json')->fail("图片类型错误");
- // }
- // $upPath = \think\facade\Filesystem::disk('resource')->putFile( $code, $file);
- // if(!$upPath){
- // return app('json')->fail("图片上传失败01");
- // }
- // $savePath = $sysData['system_url'].'/resource/' . $upPath;
- // $imgUrl = str_replace("\\", "/", $savePath);
- // return app('json')->success(['img'=>$imgUrl]);
- // }
-
- $rootTmp = config('filesystem.disks.local.root') . '/' . \think\facade\Filesystem::putFile('tmp', $file);
- $image_size = @getimagesize($rootTmp);
- if ($image_size[0] > 1000) {
- $imgS = Image::open($rootTmp);
- $imgS->thumb(1000, $image_size[1]);
- $imgS->save($rootTmp);
- } else {
- if ($image_size[1] > 1000) {
- $imgS = Image::open($rootTmp);
- $imgS->thumb($image_size[0], 1000);
- $imgS->save($rootTmp);
- }
- }
- $qiniu = new Qiniu;
- $img_url = $qiniu->updateFile('sysimg', $rootTmp, $rootTmp);
- if (empty($img_url['url'])) {
- return app('json')->fail($qiniu->getError());
- }
- @unlink($rootTmp);
- return app('json')->success(['img' => $img_url['url']]);
- }
- /**
- * 本地上传
- * @param Request $request
- * @return type
- */
- public function index2(Request $request){
- $uploadConfig = config('filesystem');
- $sysData = (new SysModel())->where("id",1)->find();
- $file = $request->file('file');
- $code = $request->post("code","image");
- if(empty($file)) {
- return app('json')->fail("未上传文件");
- }
- $size = $file->getSize();
- if(!empty($size) && $size>$uploadConfig["image"]["size"]*1024*1024){
- return app('json')->fail("图片不能超过{$uploadConfig["image"]["size"]}M");
- }
- if(!in_array($code, $uploadConfig['image']['path'])){
- return app('json')->fail("上传路径错误");
- }
- $ext = $file->extension();
- if(!in_array($ext, $uploadConfig['image']['ext'])){
- return app('json')->fail("图片类型错误");
- }
- $upPath = \think\facade\Filesystem::disk('resource')->putFile( $code, $file);
- if(!$upPath){
- return app('json')->fail("图片上传失败01");
- }
- $savePath = $sysData['system_url'].'/resource/' . $upPath;
- $imgUrl = str_replace("\\", "/", $savePath);
- return app('json')->success(['img'=>$imgUrl]);
- }
-
- /**
- * 证书文件上传
- * @param Request $request
- */
- public function uploadCertFile(Request $request){
- $uploadConfig = config('filesystem');
- $file = $request->file('file');
- $type = $request->post("type","");
- if(empty($file)) {
- return app('json')->fail("未上传文件");
- }
- $upPath = \think\facade\Filesystem::disk('resource')->putFileAs( "cert", $file,"weixin". DIRECTORY_SEPARATOR.date('Ymd').DIRECTORY_SEPARATOR.$file->getOriginalName());
- if(!$upPath){
- return app('json')->fail("上传失败01");
- }
- $savePath = '/resource/' . $upPath;
- return app('json')->success(['cert'=>$savePath,"type"=>$type,"name"=>$file->getOriginalName()]);
- }
-
-
-
-
- /**
- * 上传文件
- * @param Request $request
- */
- public function uploadFile(Request $request){
- $fileType = $request->post('type','');
- $file =$request->file('file');
- if(empty($file)) {
- return app('json')->fail("未上传文件");
- }
- //上传文件夹选择
- if(!in_array($fileType,["animated"])){
- return app('json')->fail("请选择上传文件夹");
- }
- $rootTmp = config('filesystem.disks.local.root') .'/' . \think\facade\Filesystem::putFile( 'tmp', $file);
- $qiniu = new Qiniu;
- $file_url = $qiniu->pathFileUpload($fileType, $rootTmp, $rootTmp);
- if(empty($file_url['url'])){
- return app('json')->fail( $qiniu->getError());
- }
- return app('json')->success(['url'=>$file_url['url']]);
- }
- /**
- * 上传视频
- * @param Request $request
- */
- public function mp4(Request $request) {
- $file = $request->file('Filedata');
- if(empty($file)) {
- return app('json')->fail('上传失败');
- }
- $name = $file->getOriginalName();
- $expAr= explode('.',$name);
- $exp = end($expAr);
- $dEXp = ['mp4'];
- if(!in_array(strtolower($exp),$dEXp)) {
- return app('json')->fail('上传格式不支持.'.$exp);
- }
- $mp4up = new upMp4([
- 'file' => 'Filedata',
- 'checkTmpRoot' => config('filesystem.disks.local.root') . "/mp4/",
- 'mp4Root' => config('filesystem.disks.local.root') . "/mp4/"
- ]);
- $mp4up->setParam($request->param());
- $b = $mp4up->render('jpg');
- if ($b) {
- $return = $mp4up->getResult();
- return app('json')->success($return);
- }
- return app('json')->fail( $mp4up->getError());
- }
- }
|