UploadService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\services\system\config\SystemStorageServices;
  13. use crmeb\services\upload\Upload;
  14. /**
  15. * Class UploadService
  16. * @package crmeb\services
  17. */
  18. class UploadService
  19. {
  20. protected static $upload = [];
  21. /**
  22. * @param null $type
  23. * @return Upload|mixed
  24. */
  25. public static function init($type = null, $is_cache = false)
  26. {
  27. if (is_null($type)) {
  28. $type = (int)sys_config('upload_type', 1);
  29. }
  30. if ($is_cache && isset(self::$upload['upload_' . $type]) && self::$upload['upload_' . $type]) {
  31. return self::$upload['upload_' . $type];
  32. }
  33. $type = (int)$type;
  34. $config = [];
  35. switch ($type) {
  36. case 2://七牛
  37. $config = [
  38. 'accessKey' => sys_config('qiniu_accessKey'),
  39. 'secretKey' => sys_config('qiniu_secretKey'),
  40. ];
  41. break;
  42. case 3:// oss 阿里云
  43. $config = [
  44. 'accessKey' => sys_config('accessKey'),
  45. 'secretKey' => sys_config('secretKey'),
  46. ];
  47. break;
  48. case 4:// cos 腾讯云
  49. $config = [
  50. 'accessKey' => sys_config('tengxun_accessKey'),
  51. 'secretKey' => sys_config('tengxun_secretKey'),
  52. 'appid' => sys_config('tengxun_appid'),
  53. ];
  54. break;
  55. case 5://京东云
  56. $config = [
  57. 'accessKey' => sys_config('jd_accessKey'),
  58. 'secretKey' => sys_config('jd_secretKey'),
  59. 'storageRegion' => sys_config('jd_storage_region'),
  60. ];
  61. break;
  62. case 6://华为云
  63. $config = [
  64. 'accessKey' => sys_config('hw_accessKey'),
  65. 'secretKey' => sys_config('hw_secretKey'),
  66. ];
  67. break;
  68. case 7://天翼云
  69. $config = [
  70. 'accessKey' => sys_config('ty_accessKey'),
  71. 'secretKey' => sys_config('ty_secretKey'),
  72. ];
  73. break;
  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. //除了本地存储其他都去获取配置信息
  91. if (1 !== $type) {
  92. /** @var SystemStorageServices $make */
  93. $make = app()->make(SystemStorageServices::class);
  94. $res = $make->getConfig($type);
  95. $config['uploadUrl'] = $res['domain'];
  96. $config['storageName'] = $res['name'];
  97. if ($type != 5) $config['storageRegion'] = $res['region'];
  98. }
  99. return self::$upload['upload_' . $type] = new Upload($type, $config);
  100. }
  101. /**
  102. * 生辰缩略图水印实例化
  103. * @param string $filePath
  104. * @param bool $is_remote_down
  105. * @return Upload
  106. */
  107. public static function getOssInit(string $filePath, bool $is_remote_down = false)
  108. {
  109. //本地
  110. $uploadUrl = sys_config('site_url');
  111. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  112. $filePath = explode($uploadUrl, $filePath)[1] ?? '';
  113. return self::init(1)->setFilepath($filePath);
  114. }
  115. $fileArr = parse_url($filePath);
  116. $fileHost = ($fileArr['scheme'] ?? '') . '://' . ($fileArr['host'] ?? '');
  117. /** @var SystemStorageServices $storageServices */
  118. $storageServices = app()->make(SystemStorageServices::class);
  119. $storageArr = $storageServices->cacheTag()->remember('storage_list', function () use ($storageServices) {
  120. return $storageServices->getStorageList([], ['domain','type']);
  121. });
  122. foreach ($storageArr as $item) {
  123. if ($fileHost == $item['domain']) {
  124. return self::init($item['type'])->setFilepath($filePath);
  125. }
  126. }
  127. //远程图片 下载到本地处理
  128. if ($is_remote_down) {
  129. try {
  130. /** @var DownloadImageService $down */
  131. $down = app()->make(DownloadImageService::class);
  132. $data = $down->path('thumb_water')->downloadImage($filePath);
  133. $filePath = $data['path'] ?? '';
  134. } catch (\Throwable $e) {
  135. //下载失败 传入原地址
  136. }
  137. }
  138. return self::init(1)->setFilepath($filePath);
  139. }
  140. }