Qiniu.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace core\upload;
  3. use core\exception\UploadFileException;
  4. use Exception;
  5. use Qiniu\Auth;
  6. use Qiniu\Config;
  7. use Qiniu\Storage\BucketManager;
  8. use Qiniu\Storage\UploadManager;
  9. /**
  10. * 文件管理驱动类
  11. */
  12. class Qiniu extends BaseUpload
  13. {
  14. private $position = array(
  15. 'top-left' => 'NorthWest',
  16. 'top-center' => 'North',
  17. 'top-right' => 'NorthEast',
  18. 'center-left' => 'West',
  19. 'center' => 'Center',
  20. 'center-right' => 'East',
  21. 'bottom-left' => 'SouthWest',
  22. 'bottom-center' => 'South',
  23. 'bottom-right' => 'SouthEast',
  24. );
  25. protected function initialize(array $config = [])
  26. {
  27. parent::initialize($config);
  28. }
  29. /**
  30. * 获取一个鉴权对象
  31. * @return Auth
  32. */
  33. public function auth()
  34. {
  35. $access_key = $this->config['access_key'];
  36. $secret_key = $this->config['secret_key'];
  37. return new Auth($access_key, $secret_key);
  38. }
  39. /**
  40. * @throws Exception
  41. */
  42. public function upload(string $dir)
  43. {
  44. $this->validate();
  45. $bucket = $this->config['bucket'];
  46. //todo 这儿可以定义凭证的过期时间
  47. $up_token = $this->auth()->uploadToken($bucket);
  48. // 初始化 UploadManager 对象并进行文件的上传。
  49. $upload_mgr = new UploadManager();
  50. [$ret, $err] = $upload_mgr->putFile($up_token, $this->getFullPath($dir), $this->getRealPath());
  51. if ($err !== null)
  52. throw new UploadFileException($err->message());
  53. return true;
  54. }
  55. /**
  56. * 抓取网络资源到空间
  57. * @param string $url
  58. * @param string|null $key
  59. * @return true
  60. * @throws Exception
  61. */
  62. public function fetch(string $url, ?string $key = null)
  63. {
  64. $bucket = $this->config['bucket'];
  65. $auth = $this->auth();
  66. if (!str_contains($url, 'http://') && !str_contains($url, 'https://')) {
  67. $token = $auth->uploadToken($bucket);
  68. $upload_mgr = new UploadManager();
  69. [$ret, $err] = $upload_mgr->putFile($token, $key, $url);
  70. } else {
  71. //抓取网络资源到空间
  72. $bucket_manager = new BucketManager($auth);
  73. [$ret, $err] = $bucket_manager->fetch($url, $bucket, $key);//不指定key时,以文件内容的hash作为文件名
  74. }
  75. if ($err !== null)
  76. throw new UploadFileException($err->message());
  77. return true;
  78. }
  79. /**
  80. * base64资源上传
  81. * @param string $base64_data
  82. * @param string|null $key
  83. * @return true
  84. */
  85. public function base64(string $base64_data, ?string $key = null)
  86. {
  87. $bucket = $this->config['bucket'];
  88. $auth = $this->auth();
  89. $up_token = $this->auth()->uploadToken($bucket);
  90. // 初始化 UploadManager 对象并进行文件的上传。
  91. $upload_mgr = new UploadManager();
  92. //将 base64 编码的图片数据解码
  93. $base64_file = base64_decode($base64_data);
  94. if (!$base64_file) throw new UploadFileException('FILE_ERROE');
  95. // 初始化 UpLoadManager 对象并进行文件的上传
  96. list($ret, $err) = $upload_mgr->put($up_token, $key, $base64_file);
  97. if ($err !== null) throw new UploadFileException($err->message);
  98. return true;
  99. }
  100. /**
  101. * 删除空间中的文件
  102. * @param string $file_name
  103. * @return true
  104. */
  105. public function delete(string $file_name)
  106. {
  107. $bucket = $this->config['bucket'];
  108. $auth = $this->auth();
  109. $config = new Config();
  110. $bucket_manager = new BucketManager($auth, $config);
  111. [$ret, $err] = $bucket_manager->delete($bucket, $file_name);
  112. if ($err !== null)
  113. throw new UploadFileException($err->message());
  114. return true;
  115. }
  116. public function thumb($file_path, $thumb_type)
  117. {
  118. // mageView2/1/w/400/h/600/q/85
  119. $thumb_config = config('upload.thumb.thumb_type');
  120. $thumb_data = [];
  121. foreach ($thumb_config as $k => $v) {
  122. if ($thumb_type == 'all' || $thumb_type == $k || (is_array($thumb_type) && in_array($k, $thumb_type))) {
  123. // ?x-oss-process=image/resize,m_fill,w_200,h_600,quality,q_60
  124. $width = $v['width'];
  125. $height = $v['height'];
  126. //拼装缩略路径
  127. $item_thumb = $file_path . '?imageView2/2/w/' . $width . '/h/' . $height;
  128. $thumb_data[$k] = $item_thumb;
  129. }
  130. }
  131. return $thumb_data;
  132. }
  133. /**
  134. * 图片水印
  135. * @param $file_path
  136. * @return mixed
  137. * @throws Exception
  138. */
  139. public function water($file_path)
  140. {
  141. $water_config = [];
  142. $water_path = $file_path;
  143. if (!empty($water_config)) {
  144. $status = $water_config['status'];//是否启用
  145. if ($status) {
  146. //判断当前的云图片是否存在?,存在符号的话需要用|连接
  147. if (str_contains($file_path, '?')) {
  148. $water_path .= '|watermark';
  149. } else {
  150. $water_path .= '?watermark';
  151. }
  152. if ($water_config['type'] == 'image') {
  153. $water_image = $water_config['image'];
  154. if (!empty($water_image)) {
  155. $water_path .= '/1/image/' . base64_encode($water_image) . '/gravity/' . $this->position[$water_config['position']] . '/dissolve/' . $water_config['opacity'] . '/dx/' . $water_config['offset_x'] . '/dy/' . $water_config['offset_y'];
  156. }
  157. } else {
  158. $water_path .= '/2/text/' . base64_encode($water_config['text']) . '/font/' . base64_encode($water_config['font']) . '/fill/' . base64_encode($water_config['color']) . '/fontsize/' . $water_config['size'] . '/gravity/' . $this->position[$water_config['position']] . '/dx/' . $water_config['offset_x'] . '/dy/' . $water_config['offset_y'];
  159. }
  160. }
  161. }
  162. return $water_path;
  163. }
  164. }