UploadService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 5://京东云
  62. $config = [
  63. 'accessKey' => sys_config('jd_accessKey'),
  64. 'secretKey' => sys_config('jd_secretKey'),
  65. ];
  66. case 6://华为云
  67. $config = [
  68. 'accessKey' => sys_config('hw_accessKey'),
  69. 'secretKey' => sys_config('hw_secretKey'),
  70. ];
  71. case 7://天翼云
  72. $config = [
  73. 'accessKey' => sys_config('ty_accessKey'),
  74. 'secretKey' => sys_config('ty_secretKey'),
  75. ];
  76. case 1:
  77. break;
  78. default:
  79. throw new UploadException(400733);
  80. }
  81. //除了本地存储其他都去获取配置信息
  82. if (1 !== $type) {
  83. /** @var SystemStorageServices $make */
  84. $make = app()->make(SystemStorageServices::class);
  85. $res = $make->getConfig($type);
  86. $config['uploadUrl'] = $res['domain'];
  87. $config['storageName'] = $res['name'];
  88. $config['storageRegion'] = $res['region'];
  89. }
  90. $thumb = SystemConfigService::more(['thumb_big_height', 'thumb_big_width', 'thumb_mid_height', 'thumb_mid_width', 'thumb_small_height', 'thumb_small_width',]);
  91. $water = SystemConfigService::more([
  92. 'image_watermark_status',
  93. 'watermark_type',
  94. 'watermark_image',
  95. 'watermark_opacity',
  96. 'watermark_position',
  97. 'watermark_rotate',
  98. 'watermark_text',
  99. 'watermark_text_angle',
  100. 'watermark_text_color',
  101. 'watermark_text_size',
  102. 'watermark_x',
  103. 'watermark_y']);
  104. $config = array_merge($config, ['thumb' => $thumb], ['water' => $water]);
  105. return self::$upload['upload_' . $type] = new Upload($type, $config);
  106. }
  107. /**
  108. * 生辰缩略图水印实例化
  109. * @param string $filePath
  110. * @param bool $is_remote_down
  111. * @return Upload
  112. */
  113. public static function getOssInit(string $filePath, bool $is_remote_down = false)
  114. {
  115. //本地
  116. $uploadUrl = sys_config('site_url');
  117. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  118. $filePath = explode($uploadUrl, $filePath)[1] ?? '';
  119. return self::init(1)->setFilepath($filePath);
  120. }
  121. $fileArr = parse_url($filePath);
  122. $fileHost = $fileArr['scheme'] . '://' . $fileArr['host'];
  123. /** @var SystemStorageServices $storageServices */
  124. $storageServices = app()->make(SystemStorageServices::class);
  125. $storageArr = $storageServices->cacheDriver()->remember('storage_list', function () use ($storageServices) {
  126. return $storageServices->selectList([], 'domain')->toArray();
  127. });
  128. foreach ($storageArr as $item) {
  129. if ($fileHost == $item['domain']) {
  130. return self::init($item['type'])->setFilepath($filePath);
  131. }
  132. }
  133. //远程图片 下载到本地处理
  134. if ($is_remote_down) {
  135. try {
  136. /** @var DownloadImage $down */
  137. $down = app()->make(DownloadImage::class);
  138. $data = $down->path('thumb_water')->downloadImage($filePath);
  139. $filePath = $data['path'] ?? '';
  140. } catch (\Throwable $e) {
  141. //下载失败 传入原地址
  142. }
  143. }
  144. return self::init(1)->setFilepath($filePath);
  145. }
  146. }