Qiniu.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Upload\Lib\View;
  3. use Dever;
  4. Dever::apply('sdk/qiniu', 'upload');
  5. class Qiniu
  6. {
  7. private $client;
  8. private $token;
  9. private $auth;
  10. private $bucket;
  11. public function token($config, $upload, $file = null)
  12. {
  13. $cur = time();
  14. //if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
  15. $this->auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
  16. if (true) {
  17. # 暂时没用到callback
  18. $policy = array(
  19. 'callbackUrl' => Dever::url('yun.callback?config=' . $config['id'], 'upload'),
  20. 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
  21. );
  22. $token = $this->auth->uploadToken($upload['bucket'], $file, 3600);
  23. $up['token'] = $token;
  24. $up['token_endtime'] = $cur + 3600 - 60;
  25. $up['where_id'] = $config['id'];
  26. Dever::db('upload/yun')->update($up);
  27. } else {
  28. $token = $config['token'];
  29. }
  30. $domain = 'http://up-z1.qiniup.com/';
  31. return array('qiniu', $token, $domain, $upload['bucket']);
  32. }
  33. public function callback()
  34. {
  35. $body = file_get_contents('php://input');
  36. Dever::log($body, 'qiniu_callback');
  37. $body = json_decode($body, true);
  38. return $body;
  39. }
  40. # 连接
  41. public function connect($config, $upload, $file = null)
  42. {
  43. list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);
  44. $this->token = $token;
  45. $this->bucket = $bucket;
  46. return $this;
  47. }
  48. # 下载文件
  49. public function download($bucket, $file, $local = false)
  50. {
  51. $this->client = new \Qiniu\Storage\UploadManager();
  52. if ($local) {
  53. $options = array(
  54. OssClient::OSS_FILE_DOWNLOAD => $local
  55. );
  56. } else {
  57. $options = array();
  58. }
  59. $content = $this->client->getObject($bucket, $file, $options);
  60. return $content;
  61. }
  62. # 上传文件
  63. public function upload($file, $source_file, $options = array(), $base64 = false)
  64. {
  65. if ($base64) {
  66. $method = 'put';
  67. $this->client = new \Qiniu\Storage\UploadManager();
  68. list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
  69. } elseif (strstr($source_file, 'http')) {
  70. $method = 'fetch';
  71. $this->client = new \Qiniu\Storage\BucketManager($this->auth);
  72. $items = $this->client->$method($source_file, $this->bucket, $file);
  73. if (isset($items[0])) {
  74. $ret = $items[0];
  75. }
  76. } else {
  77. $method = 'putFile';
  78. $this->client = new \Qiniu\Storage\UploadManager();
  79. list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
  80. }
  81. return $ret;
  82. }
  83. # 视频转码
  84. public function convert($key, $file, $config, $upload)
  85. {
  86. $accessKey = $config['appkey'];
  87. $secretKey = $config['appsecret'];
  88. $host = $yun['host'];
  89. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  90. $bucket = $upload['bucket'];
  91. # 转码
  92. //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
  93. $pipeline = $upload['pipeline'];
  94. if ($pipeline) {
  95. $array = explode(',', $pipeline);
  96. $index = array_rand($array);
  97. if (isset($array[$index])) {
  98. $pipeline = $array[$index];
  99. }
  100. } else {
  101. $pipeline = 'convert';
  102. }
  103. $ext = 'mp4';
  104. if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
  105. $ext = 'mp3';
  106. }
  107. $force = false;
  108. //转码完成后通知到你的业务服务器。
  109. $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
  110. $notifyUrl = false;
  111. $config = new \Qiniu\Config();
  112. //$config->useHTTPS=true;
  113. $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
  114. //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
  115. //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
  116. $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
  117. list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
  118. /*
  119. echo "\n====> pfop avthumb result: \n";
  120. if ($err != null) {
  121. var_dump($err);
  122. } else {
  123. echo "PersistentFop Id: $id\n";
  124. }
  125. die;
  126. //查询转码的进度和状态
  127. list($ret, $err) = $pfop->status($id);
  128. echo "\n====> pfop avthumb status: \n";
  129. if ($err != null) {
  130. var_dump($err);
  131. } else {
  132. var_dump($ret);
  133. }*/
  134. }
  135. # 视频截图
  136. public function cover($key, $video, $num = 1, $local = 2)
  137. {
  138. $file = $video . '?vframe/jpg/offset/' . $num;
  139. if ($local == 1) {
  140. $data = Dever::load('upload/save')->copy($file, 7, false, false, 'jpg');
  141. return $data['url'] . '?time=' . time();
  142. } else {
  143. return $file;
  144. }
  145. //vframe/jpg/offset/7/w/480/h/360
  146. }
  147. }