Qiniu.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php namespace Upload\Lib\Tool;
  2. use Dever;
  3. Dever::apply('upload/sdk/qiniu');
  4. use Upload\Lib\Save;
  5. use Image\Lib\Tool;
  6. class Qiniu
  7. {
  8. private $token;
  9. private $auth;
  10. private $bucket;
  11. private $host;
  12. public function init($config)
  13. {
  14. $this->auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
  15. $this->token = $this->auth->uploadToken($config['bucket'], null, 3600);
  16. $this->bucket = $config['bucket'];
  17. $this->host = $config['host'];
  18. }
  19. # 获取基本信息
  20. public function getInfo()
  21. {
  22. return [
  23. 'token' => $this->token,
  24. 'bucket' => $this->bucket,
  25. 'host' => $this->host,
  26. ];
  27. }
  28. # 查看文件
  29. public function view($file)
  30. {
  31. return $this->auth->privateDownloadUrl($file);
  32. }
  33. # 上传文件
  34. public function upload($type, $source, $dest, $chunk, $upload)
  35. {
  36. $result = [];
  37. $options = [];
  38. if ($type == 1) {
  39. $client = new \Qiniu\Storage\UploadManager();
  40. if ($chunk) {
  41. $log = Dever::file('upload/log');
  42. list($result, $err) = $client->putFile($this->token, $dest, $source, $options, 'application/octet-stream', false, $log, 'v2', $chunk['size'] * 1024 * 1024);
  43. } else {
  44. list($result, $err) = $client->putFile($this->token, $dest, $source, $options);
  45. }
  46. } elseif ($type == 2) {
  47. $client = new \Qiniu\Storage\UploadManager();
  48. list($result, $err) = $client->put($this->token, $dest, $source, $options);
  49. } elseif ($type == 3) {
  50. $client = new \Qiniu\Storage\BucketManager($this->auth);
  51. $items = $client->fetch($source, $this->bucket, $dest);
  52. if (isset($items[0])) {
  53. $result = $items[0];
  54. }
  55. } else {
  56. Dever::error('上传类型无效');
  57. }
  58. if (isset($result['hash']) && isset($result['key'])) {
  59. $file = $result['key'];
  60. $url = $this->host . $file;
  61. return $this->getUrl($type, $url, $upload);
  62. }
  63. Dever::error('上传失败');
  64. }
  65. public function getUrl($type, $url, $upload)
  66. {
  67. if ($type == 3) {
  68. $image = Dever::curl($url . '?imageInfo')->result();
  69. if ($image && !strstr($image, 'unsupported format')) {
  70. $image = Dever::json_decode($image);
  71. if (isset($image['width'])) {
  72. $state = $upload->check($image['format'], $image['size'], [$image['width'], $image['height']]);
  73. if (is_string($state)) {
  74. $this->delete($file);
  75. Dever::error($state);
  76. }
  77. } else {
  78. $this->delete($file);
  79. Dever::error('上传失败');
  80. }
  81. }
  82. }
  83. $after = $upload->after();
  84. if ($after) {
  85. foreach ($after as $k => $v) {
  86. if (isset($v['table'])) {
  87. $method = 'handle_' . $v['table'];
  88. $url = $this->$method($url, $v['param']);
  89. }
  90. }
  91. }
  92. return $url;
  93. }
  94. public function handle($id, $file, $type = 'thumb')
  95. {
  96. $param = Dever::db('image/' . $type)->find($id);
  97. return $this->$method($file, $param);
  98. }
  99. private function handle_thumb($file, $param)
  100. {
  101. //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
  102. $prefix = '';
  103. if (!strstr($file, 'imageMogr2')) {
  104. $prefix = '?imageMogr2';
  105. }
  106. if (strstr($file, '|imageslim')) {
  107. $file = str_replace('|imageslim', '', $file);
  108. }
  109. if ($param['height'] <= 0) {
  110. $param['height'] = '';
  111. }
  112. $dest = $file . $prefix . '/thumbnail/'.$param['width'].'x'.$param['height'] . '>';
  113. if (isset($param['compress']) && $param['compress'] > 0) {
  114. $dest .= '/quality/' . $param['compress'];
  115. }
  116. $dest .= '|imageslim';
  117. return $dest;
  118. }
  119. private function handle_crop($file, $param)
  120. {
  121. //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
  122. $temp = parse_url($file);
  123. $info = ltrim($temp['path'], '/');
  124. $info = Dever::db('file', 'upload')->find(['file' => $info]);
  125. if (strstr($file, '|imageslim')) {
  126. $file = str_replace('|imageslim', '', $file);
  127. }
  128. $x = $y = 0;
  129. if ($info) {
  130. if (strstr($file, 'thumbnail/')) {
  131. $temp = explode('thumbnail/', $file);
  132. if (isset($temp[1])) {
  133. $temp = explode('x>', $temp[1]);
  134. $width = $info['width'];
  135. $info['width'] = $temp[0];
  136. $radio = $info['width']/$width;
  137. if (isset($temp[1]) && $temp[1]) {
  138. $info['height'] = $temp[1];
  139. } else {
  140. $info['height'] = round($radio*$info['height'], 2);
  141. }
  142. }
  143. }
  144. list($x, $y) = Dever::load(Tool::class)->get()->position($info['width'], $info['height'], $param['width'], $param['height'], $param['position']);
  145. }
  146. $prefix = '';
  147. if (!strstr($file, 'imageMogr2')) {
  148. $prefix = '?imageMogr2';
  149. }
  150. $dest = $file . $prefix . '/crop/!'.$param['width'].'x'.$param['height'].'a'.$x.'a' . $y;
  151. $dest .= '|imageslim';
  152. return $dest;
  153. }
  154. # 删除文件
  155. public function delete($file, $bucket = false)
  156. {
  157. $bucket = $bucket ? $bucket : $this->bucket;
  158. $client = new \Qiniu\Storage\BucketManager($this->auth);
  159. $result = $client->delete($bucket, $file);
  160. return $result;
  161. }
  162. # 下载文件
  163. public function download($file, $bucket = false)
  164. {
  165. $bucket = $bucket ? $bucket : $this->bucket;
  166. $client = new \Qiniu\Storage\UploadManager();
  167. $content = $client->getObject($bucket, $file, []);
  168. return $content;
  169. }
  170. # 视频截图 vframe/jpg/offset/7/w/480/h/360
  171. public function cover($key, $file, $num = 1, $local = 2)
  172. {
  173. $file .= '?vframe/jpg/offset/' . $num;
  174. if ($local == 1) {
  175. $data = Dever::load(Save::class)->act(6, $file, 'jpg');
  176. return $data['url'] . '?time=' . time();
  177. } else {
  178. return $file;
  179. }
  180. }
  181. }