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