123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace Upload\Src\Store;
- use Dever;
- use Dever\Img\Handle;
- class Local extends Core implements Config
- {
- /**
- * 创建文件和目录
- *
- * @return mixed
- */
- public function save()
- {
- $this->root();
-
- $this->path()->name($this->data['file']['name'])->ext()->file();
-
- # 不覆盖
- if (is_file($this->output['file']) && $this->config['cover'] == 2) {
- $this->output['status'] = -1;
- $this->output['message'] = '文件已经存在';
- } else {
- # 不覆盖 生成新文件
- if (is_file($this->output['file']) && $this->config['cover'] == 3) {
- // . microtime() . rand(0,1000)
- $this->name($this->output['file'])->file();
- } else {
- $file = Dever::load('upload/file-name', array('where_name' => $this->name));
- }
- if (!$this->limit) {
- //$this->limit = getimagesize($this->output['file']);
- }
- if (!$this->size) {
- $this->size = filesize($this->output['file']);
- }
-
- if (isset($file) && $file) {
- $this->update($file['id']);
-
- } else {
- $this->insert();
- }
-
- if (!copy($this->data['file']['tmp_name'], $this->output['file'])) {
- $this->output['status'] = -1;
- $this->output['message'] = '文件已经存在';
- } else {
- unlink($this->data['file']['tmp_name']);
- }
- return $this->output['url'];
- }
- }
- 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'] = is_file($this->base . $this->file) ? $this->base . $this->file : Dever::path($this->base, $this->file);
- $this->output['url'] = Dever::config('host')->uploadRes . $this->file;
- return $this->output['file'];
- }
- private function handle()
- {
- $this->handle = isset($this->handle) ? $this->handle : new Handle();
- return $this->handle;
- }
-
- /**
- * 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->handle()->mark($this->output['file'], $water, true, $this->file());
- }
- }
-
- /**
- * thumb
- *
- * @return mixed
- */
- public function handle_t($id)
- {
- $config = Dever::load('upload/pic_thumb-one', $id);
- if ($config) {
- if (strpos($this->name, '_t') !== false) {
- $temp = explode('_t', $this->name);
- $this->name = $temp[0];
- }
- $this->name .= '_t' . $id;
-
- $size = $config['width'] . '_' . $config['height'] . '_2';
-
- $this->handle()->thumb($this->output['file'], $size, true, $this->file());
- }
- }
-
- /**
- * crop
- *
- * @return mixed
- */
- public function handle_c($id)
- {
- $config = Dever::load('upload/pic_crop-one', $id);
-
- if ($config) {
- $this->name .= '_c' . $id;
-
- $size = $config['width'] . '_' . $config['height'] . '_2';
-
- //false position
- $this->handle()->crop($this->output['file'], $size, false, true, $this->file());
- }
- }
- }
|