123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | upload.php 上传图片
- |--------------------------------------------------------------------------
- */
- namespace MazeApp\Upload;
- use Maze;
- use Maze\Img\Handle;
- class Upload
- {
- private $handle;
-
- /**
- * __construct
- *
- * @return mixed
- */
- public function __construct()
- {
- }
-
- /**
- * 初始化上传的功能
- *
- * @return mixed
- */
- public function init()
- {
-
- }
-
- /**
- * 开始上传
- *
- * @return mixed
- */
- public function start()
- {
- $image['file'] = isset($_FILES['Filedata']) ? $_FILES['Filedata'] : $_FILES['upfile'];
- $image['key'] = Maze::input('key');
- $output = $this->save($image);
- Maze::out($output);
- }
-
- /**
- * 复制网络文件
- *
- * @return mixed
- */
- public function copy()
- {
- $image['file'] = Maze::input('file');
- $image['key'] = Maze::input('key');
- $output = $this->save($image);
- Maze::out($output);
- }
-
- /**
- * 编辑器上传:ueditor
- *
- * @return mixed
- */
- public function ueditor()
- {
- $callback = Maze::input('callback');
- $image['file'] = isset($_FILES['Filedata']) ? $_FILES['Filedata'] : $_FILES['upfile'];
- $image['key'] = Maze::input('key');
- $output = $this->save($image);
- if($output['status'] == 1)
- {
- $output['state'] = 'SUCCESS';
- $output['original'] = '1';
- }
- else
- {
- $output['url'] = '';
- $output['fileType'] = 1;
- $output['original'] = 1;
- $output['state'] = $output['message'];
- }
-
- Maze::out($output);
- }
-
- /**
- * 上传操作
- *
- * @return mixed
- */
- private function save($image)
- {
- $output['status'] = 0;
- if(strstr($image['key'], '_'))
- {
- $handle = array();
- $temp = explode('_', $image['key']);
- $image['key'] = $temp[0];
- unset($temp[0]);
- foreach($temp as $k => $v)
- {
- $handle[] = explode('=', $v);
- }
- }
- if($image['file']['tmp_name'] == '')
- {
- $output['message'] = '没有选择文件';
- return $output;
- }
-
- if(trim($image['key']) == '')
- {
- $output['message'] = '请添加配置key';
- return $output;
- }
-
- $type = 'image/png,image/jpg,image/x-png,image/jpeg,image/pjpeg,image/gif,image/bmp,application/octet-stream';
-
- if(strpos($type, $image['file']['type']) === false)
- {
- $output['message'] = '文件格式不符合要求';
- return $output;
- }
-
- $config = Maze::load('image/upload-one', $image['key']);
-
- if(!is_array($config) || empty($config))
- {
- $output['message'] = '此配置不存在';
- return $output;
- }
-
- $size = $config['size'] > 0 ? 1024*$config['size'] : 30*1024*1024;
-
- if(($size < $image['file']['size']) && $size > 0)
- {
- $output['message'] = '文件不能超过'.$config['size'].'k';
- return $output;
- }
- $limit = getimagesize($image['file']['tmp_name']);
-
- if(($config['width'] && $config['width'] < $limit[0]) || ($config['height'] && $config['height'] < $limit[1]))
- {
- $output['message'] = '宽图片不能超过'.$config['width'].',高不能超过'.$config['height'];
- return $output;
- }
-
- $base = MAZE_PATH . 'data/upload/';
-
- $date = explode('-', date("Y-m-d"));
- $name = md5($image['file']['name'] . rand(0,100) . microtime());
- $path = $config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
- $ext = '.' . pathinfo($image['file']['name'], PATHINFO_EXTENSION);
- $file = Maze::path($base, $path . $name . $ext);
-
- # 不覆盖
- if(is_file($file) && $config['cover'] == 2)
- {
- $output['message'] = '文件已经存在';
- return $output;
- }
- # 不覆盖 生成新文件
- elseif(is_file($file) && $config['cover'] == 3)
- {
- $name = md5($this->data['tmp']['name'] . microtime() . mt_rand(0, 1000));
- $file = $base . $path . $name . $ext;
- }
- # 覆盖旧文件
- else
- {
- $pic = Maze::load('image/pic-info', array('where_name' => $name));
- }
-
- if(isset($pic) && $pic)
- {
- $param['set_name'] = $name;
- $param['set_file'] = $path;
- $param['set_ext'] = $ext;
- $param['set_width'] = $limit[0];
- $param['set_height'] = $limit[1];
- $param['set_size'] = $image['file']['size'];
- $pic = Maze::load('image/pic-update', $param);
- }
- else
- {
- $param['add_name'] = $name;
- $param['add_file'] = $path;
- $param['add_ext'] = $ext;
- $param['add_width'] = $limit[0];
- $param['add_height'] = $limit[1];
- $param['add_size'] = $image['file']['size'];
- $param['add_state'] = 1;
- //file_put_contents(MAZE_PATH . 'data/test', var_export($param,true));
- $pic = Maze::load('image/pic-insert', $param);
- }
-
- if(!copy($image['file']['tmp_name'], $file))
- {
- $output['message'] = '文件上传失败';
- return $output;
- }
-
- # 增加水印
- if($config['water'])
- {
- $handle[] = array('w', $water = array('water'=> $config['water'],'position'=> ($config['water_position'] ? $config['water_position'] : 1)));
- }
-
- if(isset($handle) && is_array($handle))
- {
- $this->handle = new Handle();
- foreach($handle as $k => $v)
- {
- $method = 'handle_' . $v[0];
- $this->$method($v[1], $file, $base, $path, $name, $ext);
- }
- }
- $output['status'] = 1;
- $output['url'] = Maze::$global['host']['image'] . $path . $name . $ext;
- return $output;
- }
-
- /**
- * water
- *
- * @return mixed
- */
- private function handle_w($water, $file, $base, $path, &$name, $ext)
- {
- $name .= '_w';
-
- $file = $this->handle->mark($file, $water, true, $base . $path . $name . $ext);
- }
-
- /**
- * thumb
- *
- * @return mixed
- */
- private function handle_t($id, $file, $base, $path, &$name, $ext)
- {
- $config = Maze::load('image/thumb-one', $id);
- if($config)
- {
- if(strpos($name, '_t') !== false)
- {
- $temp = explode('_t', $name);
- $name = $temp[0];
- }
- $name .= '_t' . $id;
-
- $size = $config['width'] . '_' . $config['height'] . '_2';
-
- $file = $this->handle->thumb($file, $size, true, $base . $path . $name . $ext);
- }
- }
-
- /**
- * crop
- *
- * @return mixed
- */
- private function handle_c($id, $base, $path, &$name, $ext)
- {
- $config = Maze::load('image/crop-one', $id);
-
- if($config)
- {
- $name .= '_c' . $id;
-
- $size = $config['width'] . '_' . $config['height'] . '_2';
-
- //false position
- $file = $this->handle->crop($file, $size, false, true, $base . $path . $name . $ext);
-
- echo $file;die;
- }
- }
-
- /**
- * @desc 根据文件名称判断文件格式
- * @param $filename 文件名称
- * return string
- **/
- private function _checktype($filename)
- {
- $file = fopen($filename,"rb");
- $bin = fread($file,2);
- fclose($file);
- $strInfo = @unpack("c2chars",$bin);
- $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
- $fileType = '';
- switch($typeCode)
- {
- case 7790:
- $fileType = 'exe';
- break;
- case 7784:
- $fileType = 'midi';
- break;
- case 8297:
- $fileType = 'rar';
- break;
- case 255216:
- $fileType = 'jpg';
- break;
- case 7173:
- $fileType = 'gif';
- break;
- case 13780:
- $fileType = 'png';
- break;
- case 6677:
- $fileType = 'bmp';
- break;
- case 6787:
- $fileType = 'swf';
- break;
- case 6063;
- $fileType = 'php|xml';
- break;
- case 6033:
- $fileType = 'html|htm|shtml';
- break;
- case 8075:
- $fileType = 'zip';
- break;
- case 6782:
- $fileType = 'txt';
- break;
- case 4742:
- $fileType = 'js';
- break;
- case 8273:
- $fileType = 'wav';
- break;
- case 7368:
- $fileType = 'mp3';
- break;
- default:
- $fileType = 'unknown'.$typeCode;
- break;
- }
- if($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40')
- {
- return 'jpg';
- }
- if($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80')
- {
- return 'png';
- }
- if($strInfo['chars1'] == '-48' && $strInfo['chars2'] == '-49')
- {
- return 'msi';
- }
- return $fileType;
- }
- }
|