Qiniu.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, $search = '')
  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 ($search) {
  51. $data['search'] = $search;
  52. }
  53. if ($info) {
  54. $data['where_id'] = $info['id'];
  55. Dever::db('upload/file')->update($data);
  56. } else {
  57. $data['source_name'] = $source;
  58. Dever::db('upload/file')->insert($data);
  59. }
  60. }
  61. public function addFile()
  62. {
  63. $file = Dever::input('file');
  64. $key = Dever::input('key');
  65. $source = Dever::input('source');
  66. $search = Dever::input('search');
  67. $config = Dever::db('upload/upload')->one($key);
  68. if ($config) {
  69. if ($config['yun'] > 0 && $config['vod_convert'] == 2) {
  70. $yun = Dever::db('upload/yun')->one($config['yun']);
  71. if ($yun) {
  72. $this->convert($key, $file, $config, $yun);
  73. }
  74. }
  75. $this->initFile($key, $file, $source, false, false, false, $search);
  76. }
  77. }
  78. private function convert($key, $file, $config, $yun)
  79. {
  80. $accessKey = $yun['appkey'];
  81. $secretKey = $yun['appsecret'];
  82. $host = $yun['host'];
  83. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  84. $bucket = $config['bucket'];
  85. # 转码
  86. //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
  87. $pipeline = $config['pipeline'];
  88. if ($pipeline) {
  89. $array = explode(',', $pipeline);
  90. $index = array_rand($array);
  91. if (isset($array[$index])) {
  92. $pipeline = $array[$index];
  93. }
  94. } else {
  95. $pipeline = 'convert';
  96. }
  97. $ext = 'mp4';
  98. if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
  99. $ext = 'mp3';
  100. }
  101. $force = false;
  102. //转码完成后通知到你的业务服务器。
  103. $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
  104. $notifyUrl = false;
  105. $config = new \Qiniu\Config();
  106. //$config->useHTTPS=true;
  107. $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
  108. //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
  109. //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
  110. $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
  111. list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
  112. /*
  113. echo "\n====> pfop avthumb result: \n";
  114. if ($err != null) {
  115. var_dump($err);
  116. } else {
  117. echo "PersistentFop Id: $id\n";
  118. }
  119. die;
  120. //查询转码的进度和状态
  121. list($ret, $err) = $pfop->status($id);
  122. echo "\n====> pfop avthumb status: \n";
  123. if ($err != null) {
  124. var_dump($err);
  125. } else {
  126. var_dump($ret);
  127. }*/
  128. }
  129. public function token()
  130. {
  131. $result = Dever::input('result', 2);
  132. $key = Dever::input('key');
  133. $upload = Dever::db('upload/upload')->one($key);
  134. if (!$upload) {
  135. return;
  136. }
  137. if (!$upload['yun']) {
  138. return;
  139. }
  140. $config = Dever::db('upload/yun')->one($upload['yun']);
  141. if (!$config) {
  142. return;
  143. }
  144. $accessKey = $config['appkey'];
  145. $secretKey = $config['appsecret'];
  146. $host = $config['host'];
  147. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  148. $bucket = $upload['bucket'];
  149. $policy = array(
  150. 'callbackUrl' => Dever::url('qiniu.callback', 'upload'),
  151. 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
  152. );
  153. // 生成上传Token
  154. $token = $auth->uploadToken($bucket, null, 3600);
  155. $domain = 'http://up-z1.qiniup.com/';
  156. $ret = array('uptoken' => $token, 'domain' => $domain, 'host' => $config['host']);
  157. if ($result == 1) {
  158. return $ret;
  159. }
  160. echo json_encode($ret);die;
  161. }
  162. }