Qiniu.php 6.5 KB

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