Qiniu.php 6.6 KB

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