UploadController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\controller;
  3. use Exception;
  4. use Intervention\Image\ImageManagerStatic as Image;
  5. use plugin\admin\app\controller\Crud;
  6. use plugin\admin\app\model\Upload;
  7. use support\Db;
  8. use support\exception\BusinessException;
  9. use support\Request;
  10. use support\Response;
  11. use hg\apidoc\annotation as Apidoc;
  12. #[Apidoc\Title("上传")]
  13. #[Apidoc\Group("Upload")]
  14. #[Apidoc\Sort(1)]
  15. class UploadController extends Crud
  16. {
  17. /**
  18. * @var Upload
  19. */
  20. protected $model = null;
  21. /**
  22. * 只返回当前管理员数据
  23. * @var string
  24. */
  25. protected $dataLimit = 'personal';
  26. /**
  27. * 构造函数
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new Upload;
  33. }
  34. #[Apidoc\Title("上传")]
  35. #[Apidoc\Url("api/file")]
  36. #[Apidoc\Method("POST")]
  37. #[Apidoc\Param("file", type: "file", require: true, desc: "上传图片")]
  38. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  39. #[Apidoc\Returned(name: "url", type: "string", require: true, desc: '前端展示地址', default: '12317309127904')]
  40. public function image(Request $request): Response
  41. {
  42. $data = $this->base($request, '/upload/img/' . date('Ymd'));
  43. $realpath = $data['realpath'];
  44. try {
  45. $img = Image::make($realpath);
  46. $max_height = 1170;
  47. $max_width = 1170;
  48. $width = $img->width();
  49. $height = $img->height();
  50. $ratio = 1;
  51. if ($height > $max_height || $width > $max_width) {
  52. $ratio = $width > $height ? $max_width / $width : $max_height / $height;
  53. }
  54. $img->resize($width * $ratio, $height * $ratio)->save($realpath);
  55. } catch (Exception $e) {
  56. unlink($realpath);
  57. return error('处理图片发生错误');
  58. }
  59. /* Db::table('wa_users')->where('id',$request->user_data['id'])->update(['img'=>$data['url']]);*/
  60. return success([
  61. 'url' => imageToBase64($data['url']),
  62. 'path' => $data['url'],
  63. ]);
  64. }
  65. /**
  66. * 获取上传数据
  67. * @param Request $request
  68. * @param $relative_dir
  69. * @return array
  70. * @throws BusinessException|RandomException
  71. */
  72. protected function base(Request $request, $relative_dir): array
  73. {
  74. $relative_dir = ltrim($relative_dir, '\\/');
  75. $file = current($request->file());
  76. if (!$file || !$file->isValid()) {
  77. throw new BusinessException('未找到上传文件', 400);
  78. }
  79. $admin_public_path = rtrim(config('app.public_path', ''), '\\/');
  80. $base_dir = $admin_public_path ? $admin_public_path . DIRECTORY_SEPARATOR : base_path() . '/plugin/admin/public/';
  81. $full_dir = $base_dir . $relative_dir;
  82. if (!is_dir($full_dir)) {
  83. mkdir($full_dir, 0777, true);
  84. }
  85. $ext = $file->getUploadExtension() ?: null;
  86. $mime_type = $file->getUploadMimeType();
  87. $file_name = $file->getUploadName();
  88. $file_size = $file->getSize();
  89. if (!$ext && $file_name === 'blob') {
  90. [$___image, $ext] = explode('/', $mime_type);
  91. unset($___image);
  92. }
  93. $ext = strtolower($ext);
  94. $ext_forbidden_map = ['php', 'php3', 'php5', 'css', 'js', 'html', 'htm', 'asp', 'jsp'];
  95. if (in_array($ext, $ext_forbidden_map)) {
  96. throw new BusinessException('不支持该格式的文件上传', 400);
  97. }
  98. $relative_path = $relative_dir . '/' . bin2hex(pack('Nn', time(), random_int(1, 65535))) . ".$ext";
  99. $full_path = $base_dir . $relative_path;
  100. $file->move($full_path);
  101. $image_with = $image_height = 0;
  102. if ($img_info = getimagesize($full_path)) {
  103. [$image_with, $image_height] = $img_info;
  104. $mime_type = $img_info['mime'];
  105. }
  106. return [
  107. 'url' => "/$relative_path",
  108. 'name' => $file_name,
  109. 'realpath' => $full_path,
  110. 'size' => $file_size,
  111. 'mime_type' => $mime_type,
  112. 'image_with' => $image_with,
  113. 'image_height' => $image_height,
  114. 'ext' => $ext,
  115. ];
  116. }
  117. #[Apidoc\Title("上传图片base64")]
  118. #[Apidoc\Url("api/base_file")]
  119. #[Apidoc\Method("POST")]
  120. #[Apidoc\Param("baseimg", type: "string", require: true, desc: "base64编码")]
  121. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  122. #[Apidoc\Returned(name: "url", type: "string", require: true, desc: '前端展示地址', default: '12317309127904')]
  123. public function base_file(Request $request): Response
  124. {
  125. $param = $request->all();
  126. try {
  127. $base64Image_one = $param['baseimg']; // 获取Base64字符串
  128. $imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64Image_one)); // 移除数据URL的前缀并解码
  129. $imageName = $request->user_data['id'] . time() . '.png'; // 生成一个唯一的文件名
  130. $data = '/upload/img/' . date('Ymd');
  131. $relative_dir = ltrim($data, '\\/');
  132. $admin_public_path = rtrim(config('app.public_path', ''), '\\/');
  133. $base_dir = $admin_public_path ? $admin_public_path . DIRECTORY_SEPARATOR : base_path() . '/plugin/admin/public/';
  134. $full_dir = $base_dir . $relative_dir;
  135. if (!is_dir($full_dir)) {
  136. mkdir($full_dir, 0777, true);
  137. }
  138. $imagePath_one = '/upload/img/' . date('Ymd') . '/' . $imageName; // 指定保存路径和文件名
  139. // 保存图片到服务器
  140. file_put_contents($base_dir . $imagePath_one, $imageData);
  141. } catch (Exception $e) {
  142. return error('处理图片发生错误');
  143. }
  144. Db::table('wa_users')->where('id',$request->user_data['id'])->update(['sign_img'=>$imagePath_one]);
  145. return adminsuccess([
  146. 'url' => imageToBase64($imagePath_one),
  147. 'path' => $imagePath_one,
  148. ]);
  149. }
  150. }