Upload.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\system\controller;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-08-31 15:05
  12. // +----------------------------------------------------------------------
  13. use app\Request;
  14. use app\system\controller\AuthController;
  15. use library\lib\upMp4;
  16. use library\utils\Qiniu;
  17. use app\model\api\Sys as SysModel;
  18. use think\Image;
  19. class Upload extends AuthController{
  20. /**
  21. * 七牛上传
  22. * @param Request $request
  23. */
  24. public function index(Request $request){
  25. $file = $request->file('file');
  26. if (empty($file)) {
  27. return app('json')->fail("未上传文件");
  28. }
  29. $code = $request->post("code","");
  30. //上传到本地
  31. // if($code=="invite"){
  32. // $sysData = (new SysModel())->where("id",1)->find();
  33. // $size = $file->getSize();
  34. // if(!empty($size) && $size>2*1024*1024){
  35. // return app('json')->fail("图片不能超过{$uploadConfig["image"]["size"]}M");
  36. // }
  37. //
  38. // $ext = $file->extension();
  39. // if(!in_array($ext, ["jpg","jpeg","png"])){
  40. // return app('json')->fail("图片类型错误");
  41. // }
  42. // $upPath = \think\facade\Filesystem::disk('resource')->putFile( $code, $file);
  43. // if(!$upPath){
  44. // return app('json')->fail("图片上传失败01");
  45. // }
  46. // $savePath = $sysData['system_url'].'/resource/' . $upPath;
  47. // $imgUrl = str_replace("\\", "/", $savePath);
  48. // return app('json')->success(['img'=>$imgUrl]);
  49. // }
  50. $rootTmp = config('filesystem.disks.local.root') . '/' . \think\facade\Filesystem::putFile('tmp', $file);
  51. $image_size = @getimagesize($rootTmp);
  52. if ($image_size[0] > 1000) {
  53. $imgS = Image::open($rootTmp);
  54. $imgS->thumb(1000, $image_size[1]);
  55. $imgS->save($rootTmp);
  56. } else {
  57. if ($image_size[1] > 1000) {
  58. $imgS = Image::open($rootTmp);
  59. $imgS->thumb($image_size[0], 1000);
  60. $imgS->save($rootTmp);
  61. }
  62. }
  63. $qiniu = new Qiniu;
  64. $img_url = $qiniu->updateFile('sysimg', $rootTmp, $rootTmp);
  65. if (empty($img_url['url'])) {
  66. return app('json')->fail($qiniu->getError());
  67. }
  68. @unlink($rootTmp);
  69. return app('json')->success(['img' => $img_url['url']]);
  70. }
  71. /**
  72. * 本地上传
  73. * @param Request $request
  74. * @return type
  75. */
  76. public function index2(Request $request){
  77. $uploadConfig = config('filesystem');
  78. $sysData = (new SysModel())->where("id",1)->find();
  79. $file = $request->file('file');
  80. $code = $request->post("code","image");
  81. if(empty($file)) {
  82. return app('json')->fail("未上传文件");
  83. }
  84. $size = $file->getSize();
  85. if(!empty($size) && $size>$uploadConfig["image"]["size"]*1024*1024){
  86. return app('json')->fail("图片不能超过{$uploadConfig["image"]["size"]}M");
  87. }
  88. if(!in_array($code, $uploadConfig['image']['path'])){
  89. return app('json')->fail("上传路径错误");
  90. }
  91. $ext = $file->extension();
  92. if(!in_array($ext, $uploadConfig['image']['ext'])){
  93. return app('json')->fail("图片类型错误");
  94. }
  95. $upPath = \think\facade\Filesystem::disk('resource')->putFile( $code, $file);
  96. if(!$upPath){
  97. return app('json')->fail("图片上传失败01");
  98. }
  99. $savePath = $sysData['system_url'].'/resource/' . $upPath;
  100. $imgUrl = str_replace("\\", "/", $savePath);
  101. return app('json')->success(['img'=>$imgUrl]);
  102. }
  103. /**
  104. * 证书文件上传
  105. * @param Request $request
  106. */
  107. public function uploadCertFile(Request $request){
  108. $uploadConfig = config('filesystem');
  109. $file = $request->file('file');
  110. $type = $request->post("type","");
  111. if(empty($file)) {
  112. return app('json')->fail("未上传文件");
  113. }
  114. $upPath = \think\facade\Filesystem::disk('resource')->putFileAs( "cert", $file,"weixin". DIRECTORY_SEPARATOR.date('Ymd').DIRECTORY_SEPARATOR.$file->getOriginalName());
  115. if(!$upPath){
  116. return app('json')->fail("上传失败01");
  117. }
  118. $savePath = '/resource/' . $upPath;
  119. return app('json')->success(['cert'=>$savePath,"type"=>$type,"name"=>$file->getOriginalName()]);
  120. }
  121. /**
  122. * 上传文件
  123. * @param Request $request
  124. */
  125. public function uploadFile(Request $request){
  126. $fileType = $request->post('type','');
  127. $file =$request->file('file');
  128. if(empty($file)) {
  129. return app('json')->fail("未上传文件");
  130. }
  131. //上传文件夹选择
  132. if(!in_array($fileType,["animated"])){
  133. return app('json')->fail("请选择上传文件夹");
  134. }
  135. $rootTmp = config('filesystem.disks.local.root') .'/' . \think\facade\Filesystem::putFile( 'tmp', $file);
  136. $qiniu = new Qiniu;
  137. $file_url = $qiniu->pathFileUpload($fileType, $rootTmp, $rootTmp);
  138. if(empty($file_url['url'])){
  139. return app('json')->fail( $qiniu->getError());
  140. }
  141. return app('json')->success(['url'=>$file_url['url']]);
  142. }
  143. /**
  144. * 上传视频
  145. * @param Request $request
  146. */
  147. public function mp4(Request $request) {
  148. $file = $request->file('Filedata');
  149. if(empty($file)) {
  150. return app('json')->fail('上传失败');
  151. }
  152. $name = $file->getOriginalName();
  153. $expAr= explode('.',$name);
  154. $exp = end($expAr);
  155. $dEXp = ['mp4'];
  156. if(!in_array(strtolower($exp),$dEXp)) {
  157. return app('json')->fail('上传格式不支持.'.$exp);
  158. }
  159. $mp4up = new upMp4([
  160. 'file' => 'Filedata',
  161. 'checkTmpRoot' => config('filesystem.disks.local.root') . "/mp4/",
  162. 'mp4Root' => config('filesystem.disks.local.root') . "/mp4/"
  163. ]);
  164. $mp4up->setParam($request->param());
  165. $b = $mp4up->render('jpg');
  166. if ($b) {
  167. $return = $mp4up->getResult();
  168. return app('json')->success($return);
  169. }
  170. return app('json')->fail( $mp4up->getError());
  171. }
  172. }