123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- namespace Upload\Lib\Store;
- use Dever;
- Dever::apply('sdk/qiniu', 'upload');
- class Qiniu extends Core implements Config
- {
- public $yun = true;
- /**
- * 上传文件
- *
- * @return mixed
- */
- public function save()
- {
- list($yun, $file) = $this->yun();
- $options = array();
- $method = Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config, $file);
- $ret = $method->upload($file, $this->data['file']['tmp_name'], $options, $this->base64);
- if (isset($ret['hash']) && isset($ret['key'])) {
- $this->file = $ret['key'];
- if (!$this->limit) {
- $data = Dever::curl($this->output['url'] . '?imageInfo');
- if ($data && !strstr($data, 'unsupported format')) {
- $data = Dever::json_decode($data);
- if (isset($data['width'])) {
- $this->limit = array($data['width'], $data['height']);
- $this->size = $data['size'];
- $this->ext = '.' . $data['format'];
- $this->limit();
- if ($this->output['status'] == -1) {
- $method->delete($this->file);
- return $this->output;
- }
- } else {
- $this->output['status'] = -1;
- $this->output['message'] = '上传失败';
- $method->delete($this->file);
- return $this->output;
- }
- }
- }
- $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'clear' => true));
-
- if (isset($file) && $file) {
- $this->update($file['id']);
-
- } else {
- $this->insert();
- }
- return $this->output['url'];
- }
- }
- public function file()
- {
- if ($this->base64 && isset($this->data['local'])) {
- $data = parse_url($this->data['local']);
- $data['path'] = str_replace(Dever::data() . 'upload/', '', $data['path']);
- $this->path = ltrim($data['path'], '/');
- }
- $this->file = $this->path . $this->name . $this->ext;
- $this->output['file'] = $this->file;
- $this->output['url'] = $this->host . $this->file;
-
- //$this->output['url'] = '{uploadYun}' . $this->file;
- return $this->output['file'];
- }
-
- /**
- * water
- *
- * @return mixed
- */
- public function handle_w($id, $source = false)
- {
- $config = Dever::load('upload/pic_water-one', $id);
- if ($config) {
- if (isset(Dever::config('base')->handle_w) && Dever::config('base')->handle_w) {
- # 走本地水印
- list($yun, $file) = $this->yun();
- $this->name .= '_w' . $id;
- $file = $this->file();
- $options = array();
- Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config, $file)->upload($file, Dever::config('base')->handle_w, $options);
- } else {
- # 走七牛的水印
- # 参考:https://developer.qiniu.com/dora/1316/image-watermarking-processing-watermark
- # 暂时未实现
- return;
- if (!$source) {
- $source = $this->output['file'];
- }
- //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
- if (!strstr($source, 'http')) {
- $source = $this->data['host'] . $source;
- }
- if (strstr($source, '?')) {
- $prefix = '&';
- } else {
- $prefix = '?';
- }
- $dest = $source . $prefix . 'watermark/1/image/a29kbzovL2RldmVsb3Blci1kb2N1bWVudHMtaW1hZ2UvcWluaXV5dW4ucG5n/dissolve/50/gravity/SouthEast/dx/20/dy/20/ws/0.2';
- $dest .= '|imageslim';
- }
- }
- }
- /**
- * 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, $path = '')
- {
- $config = Dever::load('upload/pic_thumb-one', $id);
- if ($config) {
- if (!$source) {
- $source = $this->output['file'];
- }
- //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
- if (!strstr($source, 'http')) {
- $source = $this->data['host'] . $source;
- }
- /*
- if (strstr($source, '?')) {
- $prefix = '&';
- } else {
- $prefix = '?';
- }*/
- $prefix = '';
- if (!strstr($source, 'imageMogr2')) {
- $prefix = '?imageMogr2';
- }
- if (strstr($source, '|imageslim')) {
- $source = str_replace('|imageslim', '', $source);
- }
- //$dest = $source . $prefix . 'imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
- if ($config['height'] <= 0) {
- $config['height'] = '';
- }
- $dest = $source . $prefix . '/thumbnail/'.$config['width'].'x'.$config['height'] . '>';
- if (isset($config['compress']) && $config['compress'] > 0) {
- //$dest .= '/q/' . $config['compress'];
- $dest .= '/quality/' . $config['compress'];
- }
- $dest .= '|imageslim';
- $this->output['url'] = $dest;
- }
- return $dest;
- }
-
- /**
- * crop
- *
- * @return mixed
- */
- public function handle_c($id, $source = false, $dest = false, $path = '')
- {
- $config = Dever::load('upload/pic_crop-one', $id);
- if ($config) {
- if (!$source) {
- $source = $this->output['file'];
- }
- //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
- if (!strstr($source, 'http')) {
- $source = $this->data['host'] . $source;
- }
- $temp = parse_url($source);
- $file = ltrim($temp['path'], '/');
- $file = Dever::db('upload/file')->one(array('file' => $file));
- if (strstr($source, '|imageslim')) {
- $source = str_replace('|imageslim', '', $source);
- }
- $x = $y = 0;
- if ($file) {
- if (strstr($source, 'thumbnail/')) {
- $temp = explode('thumbnail/', $source);
- if (isset($temp[1])) {
- $temp = explode('x>', $temp[1]);
- $width = $file['width'];
- $file['width'] = $temp[0];
- $radio = $file['width']/$width;
- if (isset($temp[1]) && $temp[1]) {
- $file['height'] = $temp[1];
- } else {
- $file['height'] = round($radio*$file['height'], 2);
- }
- }
- }
- list($x, $y) = $this->img()->get_position($file['width'], $file['height'], $config['width'], $config['height'], 'crop', $config['type']);
- }
-
- $prefix = '';
- if (!strstr($source, 'imageMogr2')) {
- $prefix = '?imageMogr2';
- }
- $dest = $source . $prefix . '/crop/!'.$config['width'].'x'.$config['height'].'a'.$x.'a' . $y;
- $dest .= '|imageslim';
- $this->output['url'] = $dest;
- }
- return $dest;
- }
- }
|