小说推广数据系统

UploadService.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Services;
  3. use App\Log;
  4. use App\Support\PhpQrcode;
  5. use Illuminate\Support\Facades\Storage;
  6. use App\Services\OssService as oss;
  7. use Ramsey\Uuid\Uuid;
  8. class UploadService
  9. {
  10. /*
  11. * 统一上传至OSS(图片类-base64)
  12. * */
  13. public static function upload($dataUri)
  14. {
  15. $fileUrl = '';
  16. try {
  17. preg_match('/^(data:\s*image\/(\w+);base64,)/', $dataUri, $res);
  18. $dataUri = base64_decode(str_replace($res[1],'', $dataUri));
  19. $ext = $res[2]; // 扩展名
  20. $fileName = strtoupper(Uuid::uuid4()->toString()) . "." . $ext;
  21. // 使用uploads存储目录
  22. $result = Storage::disk('uploads')->put($fileName, $dataUri);
  23. if($result) {
  24. $oss = new oss();
  25. $file = $oss->upload($ext, '../storage/uploads/'.$fileName, 'image/'.$ext.'/'.date("Y-m-d"));
  26. $fileUrl=$file['oss-request-url'];
  27. @unlink('../storage/uploads/'.$fileName);
  28. }
  29. }
  30. catch (\Exception $e) {
  31. Log::logError('文件上传失败,错误信息:'.$e->getMessage(), [], 'UploadService');
  32. }
  33. return $fileUrl;
  34. }
  35. public static function uploadQrcode($url)
  36. {
  37. $fileUrl = '';
  38. try {
  39. $ext = 'png'; // 扩展名
  40. $fileName = strtoupper(Uuid::uuid4()->toString()) . "." . $ext;
  41. $filePath = '../storage/uploads/' . $fileName;
  42. PhpQrcode::png($url, $filePath);
  43. // 使用uploads存储目录
  44. $oss = new oss();
  45. $file = $oss->upload($ext, $filePath, 'image/qrcode/'.date("Y-m-d"));
  46. $fileUrl=$file['oss-request-url'];
  47. @unlink($filePath);
  48. }
  49. catch (\Exception $e) {
  50. Log::logError('文件上传失败,错误信息:'.$e->getMessage(), [], 'UploadService');
  51. }
  52. return $fileUrl;
  53. }
  54. }