UploadService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\other;
  12. use app\services\system\config\SystemStorageServices;
  13. use crmeb\exceptions\UploadException;
  14. use crmeb\services\SystemConfigService;
  15. use crmeb\services\upload\Upload;
  16. use crmeb\utils\DownloadImage;
  17. /**
  18. * Class UploadService
  19. * @package crmeb\services
  20. */
  21. class UploadService
  22. {
  23. /**
  24. * @var array
  25. */
  26. protected static $upload = [];
  27. /**
  28. * @param null $type
  29. * @return Upload|mixed
  30. */
  31. public static function init($type = null)
  32. {
  33. if (is_null($type)) {
  34. $type = (int)sys_config('upload_type', 1);
  35. }
  36. if (isset(self::$upload['upload_' . $type])) {
  37. return self::$upload['upload_' . $type];
  38. }
  39. $type = (int)$type;
  40. $config = [];
  41. switch ($type) {
  42. case 2://七牛
  43. $config = [
  44. 'accessKey' => sys_config('qiniu_accessKey'),
  45. 'secretKey' => sys_config('qiniu_secretKey'),
  46. ];
  47. break;
  48. case 3:// oss 阿里云
  49. $config = [
  50. 'accessKey' => sys_config('accessKey'),
  51. 'secretKey' => sys_config('secretKey'),
  52. ];
  53. break;
  54. case 4:// cos 腾讯云
  55. $config = [
  56. 'accessKey' => sys_config('tengxun_accessKey'),
  57. 'secretKey' => sys_config('tengxun_secretKey'),
  58. 'appid' => sys_config('tengxun_appid'),
  59. ];
  60. break;
  61. case 1:
  62. break;
  63. default:
  64. throw new UploadException(400733);
  65. }
  66. //除了本地存储其他都去获取配置信息
  67. if (1 !== $type) {
  68. /** @var SystemStorageServices $make */
  69. $make = app()->make(SystemStorageServices::class);
  70. $res = $make->getConfig($type);
  71. $config['uploadUrl'] = $res['domain'];
  72. $config['storageName'] = $res['name'];
  73. $config['storageRegion'] = $res['region'];
  74. }
  75. $thumb = SystemConfigService::more(['thumb_big_height', 'thumb_big_width', 'thumb_mid_height', 'thumb_mid_width', 'thumb_small_height', 'thumb_small_width',]);
  76. $water = SystemConfigService::more([
  77. 'image_watermark_status',
  78. 'watermark_type',
  79. 'watermark_image',
  80. 'watermark_opacity',
  81. 'watermark_position',
  82. 'watermark_rotate',
  83. 'watermark_text',
  84. 'watermark_text_angle',
  85. 'watermark_text_color',
  86. 'watermark_text_size',
  87. 'watermark_x',
  88. 'watermark_y']);
  89. $config = array_merge($config, ['thumb' => $thumb], ['water' => $water]);
  90. return self::$upload['upload_' . $type] = new Upload($type, $config);
  91. }
  92. /**
  93. * 生辰缩略图水印实例化
  94. * @param string $filePath
  95. * @param bool $is_remote_down
  96. * @return Upload
  97. */
  98. public static function getOssInit(string $filePath, bool $is_remote_down = false)
  99. {
  100. //本地
  101. $uploadUrl = sys_config('site_url');
  102. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  103. $filePath = explode($uploadUrl, $filePath)[1] ?? '';
  104. return self::init(1)->setFilepath($filePath);
  105. }
  106. $fileArr = parse_url($filePath);
  107. $fileHost = $fileArr['scheme'] . '://' . $fileArr['host'];
  108. /** @var SystemStorageServices $storageServices */
  109. $storageServices = app()->make(SystemStorageServices::class);
  110. $storageArr = $storageServices->cacheDriver()->remember('storage_list', function () use ($storageServices) {
  111. return $storageServices->selectList([], 'domain')->toArray();
  112. });
  113. foreach ($storageArr as $item) {
  114. if ($fileHost == $item['domain']) {
  115. return self::init($item['type'])->setFilepath($filePath);
  116. }
  117. }
  118. //远程图片 下载到本地处理
  119. if ($is_remote_down) {
  120. try {
  121. /** @var DownloadImage $down */
  122. $down = app()->make(DownloadImage::class);
  123. $data = $down->path('thumb_water')->downloadImage($filePath);
  124. $filePath = $data['path'] ?? '';
  125. } catch (\Throwable $e) {
  126. //下载失败 传入原地址
  127. }
  128. }
  129. return self::init(1)->setFilepath($filePath);
  130. }
  131. }