UploadService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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([
  97. 'image_thumb_status',
  98. 'thumb_big_height',
  99. 'thumb_big_width',
  100. 'thumb_mid_height',
  101. 'thumb_mid_width',
  102. 'thumb_small_height',
  103. 'thumb_small_width'
  104. ]);
  105. $water = SystemConfigService::more([
  106. 'image_watermark_status',
  107. 'watermark_type',
  108. 'watermark_image',
  109. 'watermark_opacity',
  110. 'watermark_position',
  111. 'watermark_rotate',
  112. 'watermark_text',
  113. 'watermark_text_angle',
  114. 'watermark_text_color',
  115. 'watermark_text_size',
  116. 'watermark_x',
  117. 'watermark_y'
  118. ]);
  119. $config = array_merge($config, ['thumb' => $thumb], ['water' => $water]);
  120. return self::$upload['upload_' . $type] = new Upload($type, $config);
  121. }
  122. /**
  123. * 生辰缩略图水印实例化
  124. * @param string $filePath
  125. * @param bool $is_remote_down
  126. * @return Upload
  127. */
  128. public static function getOssInit(string $filePath, bool $is_remote_down = false)
  129. {
  130. //本地
  131. $uploadUrl = sys_config('site_url');
  132. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  133. $filePath = explode($uploadUrl, $filePath)[1] ?? '';
  134. return self::init(1)->setFilepath($filePath);
  135. }
  136. $fileArr = parse_url($filePath);
  137. $fileHost = $fileArr['scheme'] . '://' . $fileArr['host'];
  138. /** @var SystemStorageServices $storageServices */
  139. $storageServices = app()->make(SystemStorageServices::class);
  140. $storageArr = CacheService::remember('storage_list', function () use ($storageServices) {
  141. return $storageServices->selectList([], 'domain,type')->toArray();
  142. });
  143. foreach ($storageArr as $item) {
  144. if ($fileHost == $item['domain']) {
  145. return self::init($item['type'])->setFilepath($filePath);
  146. }
  147. }
  148. //远程图片 下载到本地处理
  149. if ($is_remote_down) {
  150. try {
  151. /** @var DownloadImage $down */
  152. $down = app()->make(DownloadImage::class);
  153. $data = $down->path('thumb_water')->downloadImage($filePath);
  154. $filePath = $data['path'] ?? '';
  155. } catch (\Throwable $e) {
  156. //下载失败 传入原地址
  157. }
  158. }
  159. return self::init(1)->setFilepath($filePath);
  160. }
  161. }