123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php namespace Upload\Lib\Tool;
- use Dever;
- class Local
- {
- private $config;
- public function __construct($config)
- {
- $this->config = $config;
- }
- public function upload($type, $source, $dest, $chunk, $upload)
- {
- $file = Dever::file('upload/' . $dest);
- if ($chunk) {
- $chunk['file'] = $file;
- $file = Dever::file('upload/' . $dest . '_chunk/' . $chunk['cur'] . '.blob');
- }
- if ($type == 1) {
- if (!copy($source, $file)) {
- Dever::error('上传失败');
- }
- } elseif ($type == 2) {
- file_put_contents($file, $source);
- } else {
- Dever::error('上传类型无效');
- }
- if (!is_file($file)) {
- Dever::error('上传失败');
- }
- if ($chunk) {
- $dir = dirname($file);
- if (is_dir($dir)) {
- $files = scandir($dir);
- $num = count($files) - 2;
- if ($num == $chunk['total']) {
- $blob = '';
- foreach ($files as $k => $v) {
- if (strstr($v, '.blob')) {
- $temp = $dir . '/' . $v;
- $blob .= file_get_contents($temp);
- @unlink($temp);
- }
- }
- file_put_contents($chunk['file'], $blob);
- @rmdir($dir);
- $file = $chunk['file'];
- } else {
- return '';
- }
- } else {
- return '';
- }
- }
- $after = $upload->after();
- if ($after) {
- $tool = Dever::load('tool', 'image')->get()->cover(true);
- foreach ($after as $k => $v) {
- if (isset($v['table'])) {
- $method = 'handle_' . $v['table'];
- $file = $this->$method($tool, $file, $v['param']);
- }
- }
- }
- return $this->url($file);
- }
- private function url($file)
- {
- $dest = str_replace(Dever::data() . DEVER_PROJECT . '/', '', $file);
- if ($this->config['host']) {
- return $this->config['host'] . $dest;
- }
- return Dever::host() . 'data/' . DEVER_PROJECT . '/' . $dest;
- }
- public function handle($id, $file, $type = 'thumb')
- {
- $param = Dever::db($type, 'image')->find($id);
- $tool = Dever::load('tool', 'image')->get()->cover(true);
- $file = $this->$method($tool, $file, $param);
- return $this->url($file);
- }
- private function handle_thumb($tool, $file, $param)
- {
- $tool->source($file);
- $result = $tool->thumb($param['width'] . '_' . $param['height']);
- if ($result) {
- return $result[0];
- }
- return $file;
- }
- private function handle_crop($tool, $file, $param)
- {
- $tool->source($file);
- $result = $tool->crop($param['width'] . '_' . $param['height'], $param['position']);
- if ($result) {
- return $result[0];
- }
- return $file;
- }
- }
|