123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php namespace Upload\Lib\Tool;
- use Dever;
- Dever::apply('qiniu', 'upload', 'sdk');
- class Qiniu
- {
- private $token;
- private $auth;
- private $bucket;
- private $host;
- public function __construct($config)
- {
- $this->auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
- $this->token = $this->auth->uploadToken($config['bucket'], null, 3600);
- $this->bucket = $config['bucket'];
- $this->host = $config['host'];
- }
- # 获取基本信息
- public function getInfo()
- {
- return array
- (
- 'token' => $this->token,
- 'bucket' => $this->bucket,
- 'host' => $this->host,
- );
- }
- # 查看文件
- public function view($file)
- {
- return $this->auth->privateDownloadUrl($file);
- }
- # 上传文件
- public function upload($type, $source, $dest, $chunk, $upload)
- {
- $result = array();
- $options = array();
- if ($type == 1) {
- $client = new \Qiniu\Storage\UploadManager();
- if ($chunk) {
- $log = Dever::file('upload/log');
- list($result, $err) = $client->putFile($this->token, $dest, $source, $options, 'application/octet-stream', false, $log, 'v2', $chunk['size'] * 1024 * 1024);
- } else {
- list($result, $err) = $client->putFile($this->token, $dest, $source, $options);
- }
- } elseif ($type == 2) {
- $client = new \Qiniu\Storage\UploadManager();
- list($result, $err) = $client->put($this->token, $dest, $source, $options);
- } elseif ($type == 3) {
- $client = new \Qiniu\Storage\BucketManager($this->auth);
- $items = $client->fetch($source, $this->bucket, $dest);
- if (isset($items[0])) {
- $result = $items[0];
- }
- } else {
- Dever::error('上传类型无效');
- }
- if (isset($result['hash']) && isset($result['key'])) {
- $file = $result['key'];
- $url = $this->host . $file;
- return $this->getUrl($type, $url, $upload);
- }
- Dever::error('上传失败');
- }
- public function getUrl($type, $url, $upload)
- {
- if ($type == 3) {
- $image = Dever::curl($url . '?imageInfo')->result();
- if ($image && !strstr($image, 'unsupported format')) {
- $image = Dever::json_decode($image);
- if (isset($image['width'])) {
- $state = $upload->check($image['format'], $image['size'], array($image['width'], $image['height']));
- if (is_string($state)) {
- $this->delete($file);
- Dever::error($state);
- }
- } else {
- $this->delete($file);
- Dever::error('上传失败');
- }
- }
- }
- $after = $upload->after();
- if ($after) {
- foreach ($after as $k => $v) {
- if (isset($v['table'])) {
- $method = 'handle_' . $v['table'];
- $url = $this->$method($url, $v['param']);
- }
- }
- }
- return $url;
- }
- public function handle($id, $file, $type = 'thumb')
- {
- $param = Dever::db($type, 'image')->find($id);
- return $this->$method($file, $param);
- }
- private function handle_thumb($file, $param)
- {
- //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
- $prefix = '';
- if (!strstr($file, 'imageMogr2')) {
- $prefix = '?imageMogr2';
- }
- if (strstr($file, '|imageslim')) {
- $file = str_replace('|imageslim', '', $file);
- }
- if ($param['height'] <= 0) {
- $param['height'] = '';
- }
- $dest = $file . $prefix . '/thumbnail/'.$param['width'].'x'.$param['height'] . '>';
- if (isset($param['compress']) && $param['compress'] > 0) {
- $dest .= '/quality/' . $param['compress'];
- }
- $dest .= '|imageslim';
- return $dest;
- }
- private function handle_crop($file, $param)
- {
- //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
- $temp = parse_url($file);
- $info = ltrim($temp['path'], '/');
- $info = Dever::db('file', 'upload')->find(array('file' => $info));
- if (strstr($file, '|imageslim')) {
- $file = str_replace('|imageslim', '', $file);
- }
- $x = $y = 0;
- if ($info) {
- if (strstr($file, 'thumbnail/')) {
- $temp = explode('thumbnail/', $file);
- if (isset($temp[1])) {
- $temp = explode('x>', $temp[1]);
- $width = $info['width'];
- $info['width'] = $temp[0];
- $radio = $info['width']/$width;
- if (isset($temp[1]) && $temp[1]) {
- $info['height'] = $temp[1];
- } else {
- $info['height'] = round($radio*$info['height'], 2);
- }
- }
- }
- list($x, $y) = Dever::load('tool', 'image')->get()->position($info['width'], $info['height'], $param['width'], $param['height'], $param['position']);
- }
- $prefix = '';
- if (!strstr($file, 'imageMogr2')) {
- $prefix = '?imageMogr2';
- }
- $dest = $file . $prefix . '/crop/!'.$param['width'].'x'.$param['height'].'a'.$x.'a' . $y;
- $dest .= '|imageslim';
- return $dest;
- }
- # 删除文件
- public function delete($file, $bucket = false)
- {
- $bucket = $bucket ? $bucket : $this->bucket;
- $client = new \Qiniu\Storage\BucketManager($this->auth);
- $result = $client->delete($bucket, $file);
- return $result;
- }
- # 下载文件
- public function download($file, $bucket = false)
- {
- $bucket = $bucket ? $bucket : $this->bucket;
- $client = new \Qiniu\Storage\UploadManager();
- $content = $client->getObject($bucket, $file, array());
- return $content;
- }
- # 视频截图 vframe/jpg/offset/7/w/480/h/360
- public function cover($key, $file, $num = 1, $local = 2)
- {
- $file .= '?vframe/jpg/offset/' . $num;
- if ($local == 1) {
- $data = Dever::load('save', 'upload')->act(6, $file, 'jpg');
- return $data['url'] . '?time=' . time();
- } else {
- return $file;
- }
- }
- }
|