Upload.php 1000 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace app\common\util;
  3. class Upload
  4. {
  5. public function file(): array
  6. {
  7. $file = request()->file('file');
  8. // 上传到本地服务器
  9. $rename = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
  10. $rename = str_replace('\\', '/', $rename);
  11. $url = request()->domain() . getVirRootDir() . '/storage/' . $rename;
  12. $file_path = app()->getRootPath() . 'public/storage/' . $rename;
  13. $file_info = mime_content_type($file_path);
  14. if (str_contains($file_info, 'image')) {
  15. $attr = getimagesize($file_path);
  16. $size = filesize($file_path);
  17. if ($size / 1000 >= 700 || $attr[0] >= 900) {
  18. $img_compress = new \app\common\util\ImgCompress($file_path, 0.5);
  19. $img_compress->compressImg($file_path);
  20. }
  21. }
  22. return [
  23. "file" => $rename,
  24. "url" => $url,
  25. "size"=>filesize($file_path)
  26. ];
  27. }
  28. }