Qiniu.php 6.0 KB

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