getUploadFilePath($upload, 'img', $group_id, ['png', 'jpg', 'jpeg', 'gif'], 2048000); } else { return responseMessage(2002, '上传失败,请重试!'); } } /** * 文件上传 * * @return JsonResponse */ public function file() { $type = Request::post('type'); $group_id = Request::post('group_id'); if (empty($type)) { return responseMessage(2001, '请选择类型'); } $this->isRename = false; $upload = Request::file('file'); if ($upload) { return $this->getUploadFilePath($upload, 'file', $group_id, [], 300*1024*1000); } else { return responseMessage(2005, '上传失败,请重试!'); } } /** * 上传 * * @param UploadedFile $file * @param string $type * @param int $group_id * @param array $isAllowFileExtension * @param int $maxFileSize * @return JsonResponse */ private function getUploadFilePath(UploadedFile $file, string $type, int $group_id, array $isAllowFileExtension = [], int $maxFileSize = 2048000): JsonResponse { $fileName = $file->getClientOriginalName(); $size = $file->getSize(); // 2.是否符合文件类型 getClientOriginalExtension 获得文件后缀名 $fileExtension = $file->getClientOriginalExtension(); if ($isAllowFileExtension && !in_array($fileExtension, $isAllowFileExtension)) { return responseMessage(3001, '该文件类型不支持上传!'); } // 3.判断大小是否符合 2M $tmpFile = $file->getRealPath(); if (filesize($tmpFile) >= $maxFileSize) { return responseMessage(3002, '该文件大小,超过允许的最大值!'); } $upload_dir = $this->getUploadDir($type); $disk_dir = public_path('static/' . $upload_dir); if (!file_exists($disk_dir) || !is_dir($disk_dir)) { @mkdir($disk_dir, 0777, true); } // todo 是否需要重命名 通过软件可以对图片压缩 if ($this->isRename) { $file_name = md5(microtime()) . '.' . $fileExtension; } else { $file_name = $fileName; } $file->move($disk_dir, $file_name); $path = '/static/' . $upload_dir . $file_name; switch ($type) { case 'img': $imgType = 1; break; case 'video': $imgType = 2; break; case 'audio': $imgType = 3; break; case 'file': $imgType = 4; break; default: return responseMessage(3003, '该文件类型不支持上传!'); } return $this->saveDb([ 'group_id' => $group_id, 'type' => $imgType, 'extension' => $fileExtension, 'size' => $size, 'filename' => $fileName, 'path' => $path ]); } /** * 上传视频 * * @return JsonResponse */ public function video(): JsonResponse { $type = Request::post('type'); $group_id = Request::post('group_id'); if (empty($type)) { return responseMessage(2001, '请选择类型'); } $upload = Request::file('file'); if ($upload) { return $this->getUploadFilePath($upload, 'video', $group_id, ['mp4', 'mov'], 2048000); } else { return responseMessage(2005, '上传失败,请重试!'); } } /** * 上传音频 */ public function audio(): JsonResponse { $type = Request::post('type'); $group_id = Request::post('group_id'); if (empty($type)) { return responseMessage(2001, '请选择类型'); } $upload = Request::file('audio'); if ($upload) { return $this->getUploadFilePath($upload, 'audio', $group_id, ['mp3'], 2048000); } else { return responseMessage(2005, '上传失败,请重试!'); } } /** * 上传图片base64 * * @return String * @throws \Exception */ public function upBase64() { $requestData = Request::all(); $group_id = $requestData['group_id'] ?? ''; $base64Data = $requestData['img'] ?? ''; if (empty($base64Data)) { return responseMessage(2001, '上传失败'); } //截取字符串 $base64Data = preg_replace('/data:image\/png;base64,/is', '', $base64Data); $img = base64_decode($base64Data); if (empty($img)) { return responseMessage(2002, '上传失败'); } $upload_dir = $this->getUploadDir('img'); $disk_dir = public_path('static/' . $upload_dir); if (!file_exists($disk_dir) || !is_dir($disk_dir)) { @mkdir($disk_dir, 0777, true); } //移动文件 $file_name = md5(microtime()) . '.png'; $filePath = $disk_dir . '/' . $file_name; $path = $upload_dir . $file_name; if (!file_put_contents($filePath, $img)) { //移动失败 return responseMessage(2003, '上传失败,请重试!'); } else { //移动成功 $imgType = 1; $fileExtension = 'png'; return $this->saveDb([ 'group_id' => intval($group_id), 'type' => $imgType, 'extension' => $fileExtension, 'size' => strlen($img), 'filename' => $file_name, 'path' => $path ]); } } /** * 裁剪图片 */ public function crop() { $requestData = Request::all(); $imgId = $requestData['id'] ?? ''; $x = $requestData['x'] ?? 0; $y = $requestData['y'] ?? 0; $w = $requestData['w'] ?? ''; $h = $requestData['h'] ?? ''; if (empty($imgId)) { return responseMessage(2001, '图片不存在!'); } if (empty($w) || empty($h)) { return responseMessage(2002, '参数错误!'); } //获取图片信息 $imgInfo = ES::table('media_list')->getEsInfo($imgId); if (empty($imgInfo)) { return responseMessage(2003, '图片不存在!'); } $path = $imgInfo['path']; if (file_exists(public_path('static/' . $path))) { $upload = new UploaderService( '', [ 'filePath' => '/static' . $imgInfo['path'], 'thumb' => [ [ 'x' => $x, 'y' => $y, 'width' => $w, 'height' => $h, ] ] ], 'crop'); if (isset($upload->thumbs[0])) { $info = $upload->thumbs[0]; $imgType = 1; $path = $info['path'] ? '/' . trim($info['path'], '/static') : ''; return $this->saveDb([ 'group_id' => $imgInfo['group_id'], 'type' => $imgType, 'extension' => $imgInfo['extension'], 'size' => $info['metadata']['file.FileSize'] ?? 0, 'filename' => $imgInfo['filename'], 'path' => $path ]); } else { return responseMessage(2004, '操作失败,请重试!!'); } } else { return responseMessage(2005, '图片不存在!'); } } /** * 获取当前用户的唯一标识 * * @param $type * @return string */ private function getUploadDir($type) { return '/upload/' . substr($this->siteInfo['mid'], 2, 8) . '/' . $type . '/' . mt_rand(0, 99) . '/'; } ################################################################################################################# ##### 百度上传 ##################################################################################################### ################################################################################################################# public function ueditor() { $config_path = config_path('ueditor.json'); $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents($config_path)), true); $action = Request::input('action'); // 动态设置图片前缀,显示全路径 $static_url = config('app.asset_url'); $CONFIG = array_merge($CONFIG, [ 'imageUrlPrefix' => $static_url, 'scrawlUrlPrefix' => $static_url, 'snapscreenUrlPrefix' => $static_url, 'catcherUrlPrefix' => $static_url, 'videoUrlPrefix' => $static_url, 'fileUrlPrefix' => $static_url, 'imageManagerUrlPrefix' => $static_url, 'fileManagerUrlPrefix' => $static_url, ]); switch ($action) { case 'config': $result = json_encode($CONFIG); break; /* 上传图片 */ case 'uploadimage': /* 上传涂鸦 */ case 'uploadscrawl': $result = (new UploadAction($CONFIG, $action))->doUpload(); //保存到数据库 $this->saveToDb(1, 'media_list', $result); break; /* 上传视频 */ case 'uploadvideo': $result = (new UploadAction($CONFIG, $action))->doUpload(); //保存到数据库 $this->saveToDb(2, 'media_list', $result); break; /* 上传文件 */ case 'uploadfile': $result = (new UploadAction($CONFIG, $action))->doUpload(); //保存到数据库 $this->saveToDb(4, 'media_list', $result); break; /* 列出图片 */ case 'listimage': /* 列出文件 */ case 'listfile': $result = (new ListAction($CONFIG, $action))->doList(); break; /* 抓取远程文件 */ case 'catchimage': $result = (new CrawlerAction($CONFIG, $action))->doCrawler(); break; default: $result = json_encode(array( 'state' => '请求地址出错' )); break; } /* 输出结果 */ if (isset($_GET["callback"])) { if (preg_match("/^[\w_]+$/", $_GET["callback"])) { echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')'; } else { echo json_encode(array( 'state' => 'callback参数不合法' )); } } else { echo $result; } die(); } /** * 编辑器上传保存 * * @param $type * @param $alias * @param $result * @return JsonResponse|void */ private function saveToDb($type, $alias, $result) { $result = json_decode($result, true); if (empty($result) || !is_array($result)) { return; } return $this->saveDb([ 'group_id' => 0, 'type' => $type, 'extension' => $result['original'] ?? '', 'size' => $result['size'] ?? 0, 'filename' => $result['original'] ?? '', 'path' => $result['url'] ?? '' ]); } /** * 保存到数据库 * * @param $data * @return JsonResponse */ private function saveDb($data): JsonResponse { if ($this->isSaveToDb) { $data['mid'] = Str::random(12); $data['created_at'] = time(); $data['updated_at'] = time(); // 保存数据库 $isSuccess = DB::table($this->table)->insert($data); if ($isSuccess) { return responseMessage(1001, '恭喜你,上传成功!', $data['filename']); } else { return responseMessage(2003, '上传失败,请重试!'); } } else { return responseMessage(1001, '恭喜你,上传成功!!', $data['filename']); } } }