123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace Upload\Src\Store;
- use Dever;
- Dever::apply('sdk/qiniu', 'upload');
- class Qiniu extends Core implements Config
- {
- /**
- * 创建文件和目录
- *
- * @return mixed
- */
- public function save()
- {
- $config = Dever::db('upload/yun')->one($this->config['yun']);
- $accessKey = $config['appkey'];
- $secretKey = $config['appsecret'];
- $this->host = $config['host'];
- $auth = new \Qiniu\Auth($accessKey, $secretKey);
- $bucket = $this->config['bucket'];
- $policy = array(
- 'callbackUrl' => 'http://your.domain.com/upload_verify_callback.php',
- 'callbackBody' => 'filename=$(fname)&filesize=$(fsize)'
- );
- // 生成上传Token
- $token = $auth->uploadToken($bucket, null, 3600);
- // 构建 UploadManager 对象
- $upload = new \Qiniu\Storage\UploadManager();
- $this->path()->name($this->data['file']['name'])->ext()->file();
- list($ret, $err) = $upload->putFile($token, $this->file, $this->data['file']['tmp_name'], $param = null);
- if (isset($ret['hash']) && isset($ret['key']) && $ret['key'] == $this->file) {
- $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
-
- if (!$this->limit) {
- //$this->limit = getimagesize($this->output['file']);
- }
- if (!$this->size && isset($this->data['file']['size'])) {
- $this->size = $this->data['file']['size'];
- }
-
- if (isset($file) && $file) {
- $this->update($file['id']);
-
- } else {
- $this->insert();
- }
- return $this->output['url'];
- }
- }
- public function getName()
- {
- $this->path()->name($this->data['file']['name'])->ext()->file();
- return $this->file;
- }
- private function path()
- {
- $date = explode('-', date("Y-m-d"));
- $this->path = $this->config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
- return $this;
- }
- private function name($name)
- {
- $this->name = md5($name);
- return $this;
- }
- private function ext()
- {
- $this->ext = $this->ext ? $this->ext : '.' . pathinfo($this->data['file']['name'], PATHINFO_EXTENSION);
- return $this;
- }
- private function file()
- {
- $this->file = $this->path . $this->name . $this->ext;
- $this->output['file'] = $this->file;
- $this->output['url'] = $this->host . $this->file;
- return $this->output['file'];
- }
- private function img()
- {
- $this->img = isset($this->img) ? $this->img : new Img();
- $this->img->setType('im');
- return $this->img;
- }
-
- /**
- * water
- *
- * @return mixed
- */
- public function handle_w($id)
- {
- $config = Dever::load('upload/pic_water-one', $id);
- if ($config) {
- $this->name .= '_w' . $id;
-
- $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
-
- $this->img()->mark($this->output['file'], $water, true, $this->file());
- }
- }
- /**
- * compress
- *
- * @return mixed
- */
- public function handle_p($num, $source = false, $dest = false)
- {
- if (!$source) {
- $source = $this->output['file'];
- }
- if (!$dest) {
- if (strpos($this->name, '_p') !== false) {
- $temp = explode('_p', $this->name);
- $this->name = $temp[0];
- }
- $file = $this->output['file'];
- $url = $this->output['url'];
- $this->name .= '_p' . $num;
- $dest = $this->file();
- $this->output['file'] = $file;
- $this->output['url'] = $url;
- }
- $this->img()->setType('im');
- $this->img()->compress($source, $num, $dest);
- }
-
- /**
- * webp
- *
- * @return mixed
- */
- public function handle_wp($num, $source = false, $dest = false)
- {
- if (!$source) {
- $source = $this->output['file'];
- }
- if (!$dest) {
- if (strpos($this->name, '_wp') !== false) {
- $temp = explode('_wp', $this->name);
- $this->name = $temp[0];
- }
- $file = $this->output['file'];
- $url = $this->output['url'];
- $this->ext = '.webp';
- $this->name .= '_wp' . $num;
- $dest = $this->file();
- $this->output['file'] = $file;
- $this->output['url'] = $url;
- }
- $this->img()->setType('im');
- $this->img()->webp($source, $num, $dest);
- }
- /**
- * thumb
- *
- * @return mixed
- */
- public function handle_t($id, $source = false, $dest = false)
- {
- $config = Dever::load('upload/pic_thumb-one', $id);
- if ($config) {
- if (!$source) {
- $source = $this->output['file'];
- }
- if (!$dest) {
- if (strpos($this->name, '_t') !== false) {
- $temp = explode('_t', $this->name);
- $this->name = $temp[0];
- }
- $this->name .= '_t' . $id;
- $dest = $this->file();
- }
-
- $size = $config['width'] . '_' . $config['height'] . '_2';
- $this->img()->thumb($source, $size, true, $dest);
- # 同时进行压缩
- if (isset($config['compress']) && $config['compress'] > 0) {
- $this->img()->setType('im');
- $this->img()->compress($dest, $config['compress'], $dest);
- }
- }
- }
-
- /**
- * crop
- *
- * @return mixed
- */
- public function handle_c($id, $source = false, $dest = false)
- {
- $config = Dever::load('upload/pic_crop-one', $id);
-
- if ($config) {
- if (!$source) {
- $source = $this->output['file'];
- }
- if (!$dest) {
- if (strpos($this->name, '_c') !== false) {
- $temp = explode('_c', $this->name);
- $this->name = $temp[0];
- }
- $this->name .= '_c' . $id;
- $dest = $this->file();
- }
-
- $size = $config['width'] . '_' . $config['height'] . '_2';
- $this->img()->crop($source, $size, false, true, $dest);
- }
- }
- }
|