123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | 查看图片的几个方法
- |--------------------------------------------------------------------------
- */
- namespace Upload\Src;
- use Dever;
- Dever::apply('sdk/qiniu', 'upload');
- class Qiniu
- {
- public function callback()
- {
- $body = file_get_contents('php://input');
- file_put_contents(Dever::data() . "test.txt",$body);
- $body = json_decode($body, true);
- $file = $body['filename'];
- $key = $body['key'];
- $size = $body['filesize'];
- $width = $body['width'];
- $height = $body['height'];
- $config = Dever::db('upload/upload')->one($key);
- if ($config) {
- $this->initFile($key, $file, $file, $width, $height, $size);
- }
- return 'ok';
- }
- private function initFile($key, $file, $source, $width = false, $height = false, $size = false)
- {
- $temp = explode('/', $file);
- $name = $temp[count($temp) - 1];
- $temp = explode('.', $name);
- $name = $temp[0];
- $ext = $temp[count($temp) - 1];
- $info = Dever::load('upload/file-name', array('where_name' => $name, 'where_upload' => $key));
- $data['name'] = $name;
- $data['file'] = $file;
- $data['key'] = md5($file);
- $data['ext'] = '.' . $ext;
- if ($width) {
- $data['width'] = $width;
- }
- if ($height) {
- $data['height'] = $height;
- }
- if ($size) {
- $data['size'] = $size;
- }
- $data['upload'] = $key;
- if ($info) {
- $data['where_id'] = $info['id'];
- Dever::db('upload/file')->update($data);
- } else {
- $data['source_name'] = $source;
- Dever::db('upload/file')->insert($data);
- }
- }
- public function addFile()
- {
- $file = Dever::input('file');
- $key = Dever::input('key');
- $source = Dever::input('source');
- $config = Dever::db('upload/upload')->one($key);
- if ($config) {
- if ($config['yun'] > 0 && $config['vod_convert'] == 2) {
- $yun = Dever::db('upload/yun')->one($config['yun']);
- if ($yun) {
- $this->convert($key, $file, $config, $yun);
- }
- }
- $this->initFile($key, $file, $source);
- }
- }
- private function convert($key, $file, $config, $yun)
- {
- $accessKey = $yun['appkey'];
- $secretKey = $yun['appsecret'];
- $host = $yun['host'];
- $auth = new \Qiniu\Auth($accessKey, $secretKey);
- $bucket = $config['bucket'];
- # 转码
- //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
- $pipeline = $config['pipeline'];
- if ($pipeline) {
- $array = explode(',', $pipeline);
- $index = array_rand($array);
- if (isset($array[$index])) {
- $pipeline = $array[$index];
- }
- } else {
- $pipeline = 'convert';
- }
- $ext = 'mp4';
- if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
- $ext = 'mp3';
- }
-
- $force = false;
- //转码完成后通知到你的业务服务器。
- $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
- $notifyUrl = false;
- $config = new \Qiniu\Config();
- //$config->useHTTPS=true;
- $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
- //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
- //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
- $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
- list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
- /*
- echo "\n====> pfop avthumb result: \n";
- if ($err != null) {
- var_dump($err);
- } else {
- echo "PersistentFop Id: $id\n";
- }
- die;
- //查询转码的进度和状态
- list($ret, $err) = $pfop->status($id);
- echo "\n====> pfop avthumb status: \n";
- if ($err != null) {
- var_dump($err);
- } else {
- var_dump($ret);
- }*/
- }
- public function token()
- {
- $result = Dever::input('result', 2);
- $key = Dever::input('key');
- $upload = Dever::db('upload/upload')->one($key);
- if (!$upload) {
- return;
- }
-
- if (!$upload['yun']) {
- return;
- }
- $config = Dever::db('upload/yun')->one($upload['yun']);
- if (!$config) {
- return;
- }
- $accessKey = $config['appkey'];
- $secretKey = $config['appsecret'];
- $host = $config['host'];
- $auth = new \Qiniu\Auth($accessKey, $secretKey);
- $bucket = $upload['bucket'];
- $policy = array(
- 'callbackUrl' => Dever::url('qiniu.callback', 'upload'),
- 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
- );
- // 生成上传Token
- $token = $auth->uploadToken($bucket, null, 3600);
- $domain = 'http://up-z1.qiniup.com/';
- $ret = array('uptoken' => $token, 'domain' => $domain, 'host' => $config['host']);
- if ($result == 1) {
- return $ret;
- }
- echo json_encode($ret);die;
- }
- }
|