Qiniu.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 查看图片的几个方法
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Upload\Src;
  8. use Dever;
  9. Dever::apply('sdk/qiniu', 'upload');
  10. class Qiniu
  11. {
  12. public function callback()
  13. {
  14. $body = file_get_contents('php://input');
  15. file_put_contents(Dever::data() . "test.txt",$body);
  16. $body = json_decode($body, true);
  17. $file = $body['filename'];
  18. $key = $body['key'];
  19. $size = $body['filesize'];
  20. $width = $body['width'];
  21. $height = $body['height'];
  22. $config = Dever::db('upload/upload')->one($key);
  23. if ($config) {
  24. $this->initFile($key, $file, $file, $width, $height, $size);
  25. }
  26. return 'ok';
  27. }
  28. private function initFile($key, $file, $source, $width = false, $height = false, $size = false)
  29. {
  30. $temp = explode('/', $file);
  31. $name = $temp[count($temp) - 1];
  32. $temp = explode('.', $name);
  33. $name = $temp[0];
  34. $ext = $temp[count($temp) - 1];
  35. $info = Dever::load('upload/file-name', array('where_name' => $name, 'where_upload' => $key));
  36. $data['name'] = $name;
  37. $data['file'] = $file;
  38. $data['key'] = md5($file);
  39. $data['ext'] = '.' . $ext;
  40. if ($width) {
  41. $data['width'] = $width;
  42. }
  43. if ($height) {
  44. $data['height'] = $height;
  45. }
  46. if ($size) {
  47. $data['size'] = $size;
  48. }
  49. $data['upload'] = $key;
  50. if ($info) {
  51. $data['where_id'] = $info['id'];
  52. Dever::db('upload/file')->update($data);
  53. } else {
  54. $data['source_name'] = $source;
  55. Dever::db('upload/file')->insert($data);
  56. }
  57. }
  58. public function addFile()
  59. {
  60. $file = Dever::input('file');
  61. $key = Dever::input('key');
  62. $source = Dever::input('source');
  63. $config = Dever::db('upload/upload')->one($key);
  64. if ($config) {
  65. if ($config['yun'] > 0 && $config['vod_convert'] == 2) {
  66. $yun = Dever::db('upload/yun')->one($config['yun']);
  67. if ($yun) {
  68. $this->convert($key, $file, $config, $yun);
  69. }
  70. }
  71. $this->initFile($key, $file, $source);
  72. }
  73. }
  74. private function convert($key, $file, $config, $yun)
  75. {
  76. $accessKey = $yun['appkey'];
  77. $secretKey = $yun['appsecret'];
  78. $host = $yun['host'];
  79. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  80. $bucket = $config['bucket'];
  81. # 转码
  82. //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
  83. $pipeline = $config['pipeline'];
  84. if ($pipeline) {
  85. $array = explode(',', $pipeline);
  86. $index = array_rand($array);
  87. if (isset($array[$index])) {
  88. $pipeline = $array[$index];
  89. }
  90. } else {
  91. $pipeline = 'convert';
  92. }
  93. $ext = 'mp4';
  94. if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
  95. $ext = 'mp3';
  96. }
  97. $force = false;
  98. //转码完成后通知到你的业务服务器。
  99. $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
  100. $notifyUrl = false;
  101. $config = new \Qiniu\Config();
  102. //$config->useHTTPS=true;
  103. $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
  104. //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
  105. //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
  106. $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
  107. list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
  108. /*
  109. echo "\n====> pfop avthumb result: \n";
  110. if ($err != null) {
  111. var_dump($err);
  112. } else {
  113. echo "PersistentFop Id: $id\n";
  114. }
  115. die;
  116. //查询转码的进度和状态
  117. list($ret, $err) = $pfop->status($id);
  118. echo "\n====> pfop avthumb status: \n";
  119. if ($err != null) {
  120. var_dump($err);
  121. } else {
  122. var_dump($ret);
  123. }*/
  124. }
  125. public function token()
  126. {
  127. $result = Dever::input('result', 2);
  128. $key = Dever::input('key');
  129. $upload = Dever::db('upload/upload')->one($key);
  130. if (!$upload) {
  131. return;
  132. }
  133. if (!$upload['yun']) {
  134. return;
  135. }
  136. $config = Dever::db('upload/yun')->one($upload['yun']);
  137. if (!$config) {
  138. return;
  139. }
  140. $accessKey = $config['appkey'];
  141. $secretKey = $config['appsecret'];
  142. $host = $config['host'];
  143. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  144. $bucket = $upload['bucket'];
  145. $policy = array(
  146. 'callbackUrl' => Dever::url('qiniu.callback', 'upload'),
  147. 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
  148. );
  149. // 生成上传Token
  150. $token = $auth->uploadToken($bucket, null, 3600);
  151. $domain = 'http://up-z1.qiniup.com/';
  152. $ret = array('uptoken' => $token, 'domain' => $domain, 'host' => $config['host']);
  153. if ($result == 1) {
  154. return $ret;
  155. }
  156. echo json_encode($ret);die;
  157. }
  158. }