You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UploadService.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace crmeb\services;
  3. use app\services\system\config\SystemStorageServices;
  4. use crmeb\services\upload\Upload;
  5. /**
  6. * Class UploadService
  7. */
  8. class UploadService
  9. {
  10. protected static $upload = [];
  11. /**
  12. * @param null $type
  13. * @return Upload|mixed
  14. */
  15. public static function init($type = null, $is_cache = false)
  16. {
  17. if (is_null($type)) {
  18. $type = (int)sys_config('upload_type', 1);
  19. }
  20. if ($is_cache && isset(self::$upload['upload_' . $type]) && self::$upload['upload_' . $type]) {
  21. return self::$upload['upload_' . $type];
  22. }
  23. $type = (int)$type;
  24. $config = [];
  25. switch ($type) {
  26. case 2://七牛
  27. $config = [
  28. 'accessKey' => sys_config('qiniu_accessKey'),
  29. 'secretKey' => sys_config('qiniu_secretKey'),
  30. ];
  31. break;
  32. case 3:// oss 阿里云
  33. $config = [
  34. 'accessKey' => sys_config('accessKey'),
  35. 'secretKey' => sys_config('secretKey'),
  36. ];
  37. break;
  38. case 4:// cos 腾讯云
  39. $config = [
  40. 'accessKey' => sys_config('tengxun_accessKey'),
  41. 'secretKey' => sys_config('tengxun_secretKey'),
  42. 'appid' => sys_config('tengxun_appid'),
  43. ];
  44. break;
  45. }
  46. $thumb = SystemConfigService::more(['thumb_big_height', 'thumb_big_width', 'thumb_mid_height', 'thumb_mid_width', 'thumb_small_height', 'thumb_small_width',]);
  47. $water = SystemConfigService::more([
  48. 'image_watermark_status',
  49. 'watermark_type',
  50. 'watermark_image',
  51. 'watermark_opacity',
  52. 'watermark_position',
  53. 'watermark_rotate',
  54. 'watermark_text',
  55. 'watermark_text_angle',
  56. 'watermark_text_color',
  57. 'watermark_text_size',
  58. 'watermark_x',
  59. 'watermark_y']);
  60. $config = array_merge($config, ['thumb' => $thumb], ['water' => $water]);
  61. //除了本地存储其他都去获取配置信息
  62. if (1 !== $type) {
  63. /** @var SystemStorageServices $make */
  64. $make = app()->make(SystemStorageServices::class);
  65. $res = $make->getConfig($type);
  66. $config['uploadUrl'] = $res['domain'];
  67. $config['storageName'] = $res['name'];
  68. $config['storageRegion'] = $res['region'];
  69. }
  70. return self::$upload['upload_' . $type] = new Upload($type, $config);
  71. }
  72. /**
  73. * 生辰缩略图水印实例化
  74. * @param string $filePath
  75. * @param bool $is_remote_down
  76. * @return Upload
  77. */
  78. public static function getOssInit(string $filePath, bool $is_remote_down = false)
  79. {
  80. //本地
  81. $uploadUrl = sys_config('site_url');
  82. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  83. $filePath = explode($uploadUrl, $filePath)[1] ?? '';
  84. return self::init(1)->setFilepath($filePath);
  85. }
  86. //七牛云
  87. $uploadUrl = sys_config('qiniu_uploadUrl');
  88. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  89. return self::init(2)->setFilepath($filePath);
  90. }
  91. //阿里云
  92. $uploadUrl = sys_config('uploadUrl');
  93. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  94. return self::init(3)->setFilepath($filePath);
  95. }
  96. //腾讯云
  97. $uploadUrl = sys_config('tengxun_uploadUrl');
  98. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  99. return self::init(4)->setFilepath($filePath);
  100. }
  101. //远程图片 下载到本地处理
  102. if ($is_remote_down) {
  103. try {
  104. /** @var DownloadImageService $down */
  105. $down = app()->make(DownloadImageService::class);
  106. $data = $down->path('thumb_water')->downloadImage($filePath);
  107. $filePath = $data['path'] ?? '';
  108. } catch (\Throwable $e) {
  109. //下载失败 传入原地址
  110. }
  111. }
  112. return self::init(1)->setFilepath($filePath);
  113. }
  114. }