Tencent.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace core\upload;
  3. use core\exception\UploadFileException;
  4. use Exception;
  5. use Qcloud\Cos\Client;
  6. /**
  7. * 腾讯云存储引擎 (COS)
  8. */
  9. class Tencent extends BaseUpload
  10. {
  11. private $position = array(
  12. 'top-left' => 'northwest',
  13. 'top-center' => 'north',
  14. 'top-right' => 'northeast',
  15. 'center-left' => 'west',
  16. 'center' => 'center',
  17. 'center-right' => 'east',
  18. 'bottom-left' => 'southwest',
  19. 'bottom-center' => 'south',
  20. 'bottom-right' => 'southeast',
  21. );
  22. protected function initialize(array $config = [])
  23. {
  24. parent::initialize($config);
  25. }
  26. /**
  27. * 获取服务主体
  28. * @return Client
  29. */
  30. public function client()
  31. {
  32. $secret_id = $this->config['access_key']; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi
  33. $secret_key = $this->config['secret_key']; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi
  34. $region = $this->config['region']; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.tencentcloud.com/cos5/bucket
  35. return new Client(
  36. array(
  37. 'region' => $region,
  38. // 'schema' => 'https', //协议头部,默认为http
  39. 'credentials' => array(
  40. 'secretId' => $secret_id,
  41. 'secretKey' => $secret_key)
  42. )
  43. );
  44. }
  45. /**
  46. * 执行上传
  47. * @param string $dir
  48. * @return true
  49. */
  50. public function upload(string $dir)
  51. {
  52. $this->validate();
  53. $bucket = $this->config['bucket'];
  54. try {
  55. $result = $this->client()->putObject(array(
  56. 'Bucket' => $bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket
  57. 'Key' => $this->getFullPath($dir),
  58. 'Body' => fopen($this->getRealPath(), 'rb'),
  59. ));
  60. // 请求成功
  61. return true;
  62. } catch ( Exception $e ) {
  63. throw new UploadFileException($e->getMessage());
  64. }
  65. }
  66. /**
  67. * base文件上云
  68. * @param string $base64_data
  69. * @param string|null $key
  70. * @return true
  71. */
  72. public function base64(string $base64_data, ?string $key = null)
  73. {
  74. $bucket = $this->config['bucket'];
  75. try {
  76. $base64_file = base64_decode($base64_data);
  77. if (!$base64_file) throw new UploadFileException('FILE_ERROE');
  78. $result = $this->client()->putObject(array(
  79. 'Bucket' => $bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket
  80. 'Key' => $key,
  81. 'Body' => $base64_file,
  82. ));
  83. // 请求成功
  84. return true;
  85. } catch ( Exception $e ) {
  86. throw new UploadFileException($e->getMessage());
  87. }
  88. }
  89. /**
  90. * notes: 抓取远程资源(最大支持上传5G文件)
  91. * @param string $url
  92. * @param string|null $key
  93. * @return true
  94. */
  95. public function fetch(string $url, ?string $key = null)
  96. {
  97. $bucket = $this->config['bucket'];
  98. try {
  99. $result = $this->client()->putObject(array(
  100. 'Bucket' => $bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket
  101. 'Key' => $key,
  102. 'Body' => fopen($url, 'rb'),
  103. ));
  104. // 请求成功
  105. return true;
  106. } catch ( Exception $e ) {
  107. throw new UploadFileException($e->getMessage());
  108. }
  109. }
  110. /**
  111. * 删除一个简单对象
  112. * @param string $file_name
  113. * @return true
  114. */
  115. public function delete(string $file_name)
  116. {
  117. $bucket = $this->config['bucket'];
  118. try {
  119. $this->client()->deleteObject(array(
  120. 'Bucket' => $bucket,
  121. 'Key' => $file_name
  122. ));
  123. return true;
  124. } catch ( Exception $e ) {
  125. throw new UploadFileException($e->getMessage());
  126. }
  127. }
  128. public function thumb($file_path, $thumb_type)
  129. {
  130. //腾讯云缩略图地址
  131. $thumb_config = config('upload.thumb.thumb_type');
  132. $thumb_data = [];
  133. foreach ($thumb_config as $k => $v) {
  134. if ($thumb_type == 'all' || $thumb_type == $k || (is_array($thumb_type) && in_array($k, $thumb_type))) {
  135. // ?x-oss-process=image/resize,m_fill,w_200,h_600,quality,q_60
  136. $width = $v['width'];
  137. $height = $v['height'];
  138. //拼装缩略路径
  139. $item_thumb = $file_path . '?imageMogr2/thumbnail/' . $width . 'x' . $height;
  140. $thumb_data[$k] = $item_thumb;
  141. }
  142. }
  143. return $thumb_data;
  144. }
  145. /**
  146. * 图片水印
  147. * @param $file_path
  148. * @return mixed
  149. * @throws Exception
  150. */
  151. public function water($file_path)
  152. {
  153. $water_config = [];
  154. $water_path = $file_path;
  155. if (!empty($water_config)) {
  156. $status = $water_config['status'];//是否启用
  157. if($status){
  158. //判断当前的云图片是否存在?,存在符号的话需要用|连接
  159. if(str_contains($file_path, '?')){
  160. $water_path .= '&watermark';
  161. }else{
  162. $water_path .= '?watermark';
  163. }
  164. if ($water_config['type'] == 'image') {
  165. $water_image = $water_config['image'];
  166. if(!empty($water_image)){
  167. //http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?watermark/1/image/aHR0cDovL2V4YW1wbGVzLTEyNTEwMDAwMDQucGljc2gubXlxY2xvdWQuY29tL3NodWl5aW4uanBn/gravity/southeast
  168. $water_path .= '/1/image/' . base64_encode($water_image) . '/gravity/' . $this->position[$water_config['position']] . '/blogo/1/dx/' . $water_config['offset_x'] . '/dy/' . $water_config['offset_y'].'/dissolve/'.$water_config['opacity'];
  169. }
  170. } else {
  171. //http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?q-sign-algorithm=<signature>&watermark/2/text/6IW-6K6v5LqRwrfkuIfosaHkvJjlm74/fill/IzNEM0QzRA/fontsize/20/dissolve/50/gravity/northeast/dx/20/dy/20/batch/1/degree/45
  172. $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'];
  173. }
  174. }
  175. }
  176. return $water_path;
  177. }
  178. }