UploadController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Http\Controllers\Api;
  12. use Illuminate\Http\Request;
  13. /**
  14. * 上传文件-控制器
  15. * @since 2020/11/11
  16. * Class UploadController
  17. * @package App\Http\Controllers
  18. */
  19. class UploadController extends webApp
  20. {
  21. /**
  22. * 上传图片
  23. * @param Request $request 网络请求
  24. * @return array
  25. * @since 2020/11/11
  26. */
  27. public function uploadImage(Request $request)
  28. {
  29. // 上传单图统一调取方法
  30. $fromName = $request->post('from_name','file');
  31. $result = upload_image($request, $fromName, '',$this->userId);
  32. if (!$result['success']) {
  33. return showJson($result['msg'], false, '');
  34. }
  35. // 文件路径
  36. $file_path = $result['data']['img_path'];
  37. if (!$file_path) {
  38. return showJson('文件上传失败', false, '');
  39. }
  40. // 网络域名拼接
  41. if ($file_path && strpos($file_path, IMG_URL) === false) {
  42. $file_path = get_image_url($file_path, false);
  43. }
  44. // 返回结果
  45. return showJson(MESSAGE_OK, true, ['url'=> $file_path, 'path'=> $result['data']['img_path']]);
  46. }
  47. /**
  48. * 上传文件(单个上传)
  49. * @param Request $request
  50. * @since 2020/11/11
  51. */
  52. public function uploadFile(Request $request)
  53. {
  54. $result = upload_file($request,'file','', $this->userId);
  55. if (!$result['success']) {
  56. return showJson($result['msg'], false, '');
  57. }
  58. // 文件路径
  59. $file_path = isset($result['data']['file_path'])? $result['data']['file_path'] : '';
  60. if (!$file_path) {
  61. return showJson("文件上传失败",false);
  62. }
  63. // 网络域名拼接
  64. if (strpos($file_path, IMG_URL) === false) {
  65. $file_path = get_image_url($file_path, false);
  66. }
  67. // 返回结果
  68. return showJson(MESSAGE_OK, true, ['url'=> $file_path, 'path'=> $result['data']['img_path']]);
  69. }
  70. /**
  71. * 图片上传
  72. * @return array
  73. */
  74. public function uploadBase64()
  75. {
  76. $base64 = request()->post('base64','');
  77. $uuid = request()->post('uuid', '');
  78. if ($base64) {
  79. $fileDir = ATTACHMENT_PATH . '/images/base64img/' . date('Ymd');
  80. // 检测文件路径是否存在,不存在则创建
  81. if (!file_exists($fileDir)) {
  82. mkdir($fileDir, 0777, true);
  83. }
  84. $fileName = 'qrcode_'.md5($uuid? $uuid : $base64) . '.jpeg';
  85. $base64 = str_replace('data:image/jpeg;base64,','',$base64);
  86. file_put_contents($fileDir.'/'.$fileName, base64_decode($base64));
  87. if(file_exists($fileDir.'/'.$fileName)){
  88. $filePath = str_replace(ATTACHMENT_PATH, '', $fileDir) . '/' . $fileName;
  89. return showJson(MESSAGE_OK, true, ['url'=>get_image_url($filePath, false),'path'=>$filePath]);
  90. }
  91. }
  92. return showJson(MESSAGE_FAILED, false);
  93. }
  94. }