UploadService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\CacheService;
  15. use crmeb\services\SystemConfigService;
  16. use crmeb\services\upload\Upload;
  17. use crmeb\utils\DownloadImage;
  18. /**
  19. * Class UploadService
  20. * @package crmeb\services
  21. */
  22. class UploadService
  23. {
  24. /**
  25. * @var array
  26. */
  27. protected static $upload = [];
  28. /**
  29. * @param null $type
  30. * @return Upload|mixed
  31. */
  32. public static function init($type = null)
  33. {
  34. if (is_null($type)) {
  35. $type = (int)sys_config('upload_type', 1);
  36. }
  37. if (isset(self::$upload['upload_' . $type])) {
  38. return self::$upload['upload_' . $type];
  39. }
  40. $type = (int)$type;
  41. $config = [];
  42. switch ($type) {
  43. case 2://七牛
  44. $config = [
  45. 'accessKey' => sys_config('qiniu_accessKey'),
  46. 'secretKey' => sys_config('qiniu_secretKey'),
  47. ];
  48. break;
  49. case 3:// oss 阿里云
  50. $config = [
  51. 'accessKey' => sys_config('accessKey'),
  52. 'secretKey' => sys_config('secretKey'),
  53. ];
  54. break;
  55. case 4:// cos 腾讯云
  56. $config = [
  57. 'accessKey' => sys_config('tengxun_accessKey'),
  58. 'secretKey' => sys_config('tengxun_secretKey'),
  59. 'appid' => sys_config('tengxun_appid'),
  60. ];
  61. break;
  62. case 5://京东云
  63. $config = [
  64. 'accessKey' => sys_config('jd_accessKey'),
  65. 'secretKey' => sys_config('jd_secretKey'),
  66. 'storageRegion' => sys_config('jd_storageRegion'),
  67. ];
  68. break;
  69. case 6://华为云
  70. $config = [
  71. 'accessKey' => sys_config('hw_accessKey'),
  72. 'secretKey' => sys_config('hw_secretKey'),
  73. ];
  74. break;
  75. case 7://天翼云
  76. $config = [
  77. 'accessKey' => sys_config('ty_accessKey'),
  78. 'secretKey' => sys_config('ty_secretKey'),
  79. ];
  80. break;
  81. case 1:
  82. break;
  83. default:
  84. throw new UploadException(400733);
  85. }
  86. //除了本地存储其他都去获取配置信息
  87. if (1 !== $type) {
  88. /** @var SystemStorageServices $make */
  89. $make = app()->make(SystemStorageServices::class);
  90. $res = $make->getConfig($type);
  91. $config['uploadUrl'] = $res['domain'];
  92. $config['storageName'] = $res['name'];
  93. if (5 !== $type) $config['storageRegion'] = $res['region'];
  94. $config['cdn'] = $res['cdn'];
  95. }
  96. $thumb = SystemConfigService::more(['thumb_big_height', 'thumb_big_width', 'thumb_mid_height', 'thumb_mid_width', 'thumb_small_height', 'thumb_small_width',]);
  97. $water = SystemConfigService::more([
  98. 'image_watermark_status',
  99. 'watermark_type',
  100. 'watermark_image',
  101. 'watermark_opacity',
  102. 'watermark_position',
  103. 'watermark_rotate',
  104. 'watermark_text',
  105. 'watermark_text_angle',
  106. 'watermark_text_color',
  107. 'watermark_text_size',
  108. 'watermark_x',
  109. 'watermark_y']);
  110. $config = array_merge($config, ['thumb' => $thumb], ['water' => $water]);
  111. return self::$upload['upload_' . $type] = new Upload($type, $config);
  112. }
  113. /**
  114. * 生辰缩略图水印实例化
  115. * @param string $filePath
  116. * @param bool $is_remote_down
  117. * @return Upload
  118. */
  119. public static function getOssInit(string $filePath, bool $is_remote_down = false)
  120. {
  121. //本地
  122. $uploadUrl = sys_config('site_url');
  123. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  124. $filePath = explode($uploadUrl, $filePath)[1] ?? '';
  125. return self::init(1)->setFilepath($filePath);
  126. }
  127. $fileArr = parse_url($filePath);
  128. $fileHost = $fileArr['scheme'] . '://' . $fileArr['host'];
  129. /** @var SystemStorageServices $storageServices */
  130. $storageServices = app()->make(SystemStorageServices::class);
  131. $storageArr = CacheService::remember('storage_list', function () use ($storageServices) {
  132. return $storageServices->selectList([], 'domain,type')->toArray();
  133. });
  134. foreach ($storageArr as $item) {
  135. if ($fileHost == $item['domain']) {
  136. return self::init($item['type'])->setFilepath($filePath);
  137. }
  138. }
  139. //远程图片 下载到本地处理
  140. if ($is_remote_down) {
  141. try {
  142. /** @var DownloadImage $down */
  143. $down = app()->make(DownloadImage::class);
  144. $data = $down->path('thumb_water')->downloadImage($filePath);
  145. $filePath = $data['path'] ?? '';
  146. } catch (\Throwable $e) {
  147. //下载失败 传入原地址
  148. }
  149. }
  150. return self::init(1)->setFilepath($filePath);
  151. }
  152. }