c7edad3dc71a64f3300efc0ac68f0ea5f79e9e38.svn-base 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | upload.php 上传图片
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace MazeApp\Upload;
  8. use Maze;
  9. use Maze\Img\Handle;
  10. class Upload
  11. {
  12. private $handle;
  13. /**
  14. * __construct
  15. *
  16. * @return mixed
  17. */
  18. public function __construct()
  19. {
  20. }
  21. /**
  22. * 初始化上传的功能
  23. *
  24. * @return mixed
  25. */
  26. public function init()
  27. {
  28. }
  29. /**
  30. * 开始上传
  31. *
  32. * @return mixed
  33. */
  34. public function start()
  35. {
  36. $image['file'] = isset($_FILES['Filedata']) ? $_FILES['Filedata'] : $_FILES['upfile'];
  37. $image['key'] = Maze::input('key');
  38. $output = $this->save($image);
  39. Maze::out($output);
  40. }
  41. /**
  42. * 复制网络文件
  43. *
  44. * @return mixed
  45. */
  46. public function copy()
  47. {
  48. $image['file'] = Maze::input('file');
  49. $image['key'] = Maze::input('key');
  50. $output = $this->save($image);
  51. Maze::out($output);
  52. }
  53. /**
  54. * 编辑器上传:ueditor
  55. *
  56. * @return mixed
  57. */
  58. public function ueditor()
  59. {
  60. $callback = Maze::input('callback');
  61. $image['file'] = isset($_FILES['Filedata']) ? $_FILES['Filedata'] : $_FILES['upfile'];
  62. $image['key'] = Maze::input('key');
  63. $output = $this->save($image);
  64. if($output['status'] == 1)
  65. {
  66. $output['state'] = 'SUCCESS';
  67. $output['original'] = '1';
  68. }
  69. else
  70. {
  71. $output['url'] = '';
  72. $output['fileType'] = 1;
  73. $output['original'] = 1;
  74. $output['state'] = $output['message'];
  75. }
  76. Maze::out($output);
  77. }
  78. /**
  79. * 上传操作
  80. *
  81. * @return mixed
  82. */
  83. private function save($image)
  84. {
  85. $output['status'] = 0;
  86. if(strstr($image['key'], '_'))
  87. {
  88. $handle = array();
  89. $temp = explode('_', $image['key']);
  90. $image['key'] = $temp[0];
  91. unset($temp[0]);
  92. foreach($temp as $k => $v)
  93. {
  94. $handle[] = explode('=', $v);
  95. }
  96. }
  97. if($image['file']['tmp_name'] == '')
  98. {
  99. $output['message'] = '没有选择文件';
  100. return $output;
  101. }
  102. if(trim($image['key']) == '')
  103. {
  104. $output['message'] = '请添加配置key';
  105. return $output;
  106. }
  107. $type = 'image/png,image/jpg,image/x-png,image/jpeg,image/pjpeg,image/gif,image/bmp,application/octet-stream';
  108. if(strpos($type, $image['file']['type']) === false)
  109. {
  110. $output['message'] = '文件格式不符合要求';
  111. return $output;
  112. }
  113. $config = Maze::load('image/upload-one', $image['key']);
  114. if(!is_array($config) || empty($config))
  115. {
  116. $output['message'] = '此配置不存在';
  117. return $output;
  118. }
  119. $size = $config['size'] > 0 ? 1024*$config['size'] : 30*1024*1024;
  120. if(($size < $image['file']['size']) && $size > 0)
  121. {
  122. $output['message'] = '文件不能超过'.$config['size'].'k';
  123. return $output;
  124. }
  125. $limit = getimagesize($image['file']['tmp_name']);
  126. if(($config['width'] && $config['width'] < $limit[0]) || ($config['height'] && $config['height'] < $limit[1]))
  127. {
  128. $output['message'] = '宽图片不能超过'.$config['width'].',高不能超过'.$config['height'];
  129. return $output;
  130. }
  131. $base = MAZE_PATH . 'data/upload/';
  132. $date = explode('-', date("Y-m-d"));
  133. $name = md5($image['file']['name'] . rand(0,100) . microtime());
  134. $path = $config['id'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  135. $ext = '.' . pathinfo($image['file']['name'], PATHINFO_EXTENSION);
  136. $file = Maze::path($base, $path . $name . $ext);
  137. # 不覆盖
  138. if(is_file($file) && $config['cover'] == 2)
  139. {
  140. $output['message'] = '文件已经存在';
  141. return $output;
  142. }
  143. # 不覆盖 生成新文件
  144. elseif(is_file($file) && $config['cover'] == 3)
  145. {
  146. $name = md5($this->data['tmp']['name'] . microtime() . mt_rand(0, 1000));
  147. $file = $base . $path . $name . $ext;
  148. }
  149. # 覆盖旧文件
  150. else
  151. {
  152. $pic = Maze::load('image/pic-info', array('where_name' => $name));
  153. }
  154. if(isset($pic) && $pic)
  155. {
  156. $param['set_name'] = $name;
  157. $param['set_file'] = $path;
  158. $param['set_ext'] = $ext;
  159. $param['set_width'] = $limit[0];
  160. $param['set_height'] = $limit[1];
  161. $param['set_size'] = $image['file']['size'];
  162. $pic = Maze::load('image/pic-update', $param);
  163. }
  164. else
  165. {
  166. $param['add_name'] = $name;
  167. $param['add_file'] = $path;
  168. $param['add_ext'] = $ext;
  169. $param['add_width'] = $limit[0];
  170. $param['add_height'] = $limit[1];
  171. $param['add_size'] = $image['file']['size'];
  172. $param['add_state'] = 1;
  173. //file_put_contents(MAZE_PATH . 'data/test', var_export($param,true));
  174. $pic = Maze::load('image/pic-insert', $param);
  175. }
  176. if(!copy($image['file']['tmp_name'], $file))
  177. {
  178. $output['message'] = '文件上传失败';
  179. return $output;
  180. }
  181. # 增加水印
  182. if($config['water'])
  183. {
  184. $handle[] = array('w', $water = array('water'=> $config['water'],'position'=> ($config['water_position'] ? $config['water_position'] : 1)));
  185. }
  186. if(isset($handle) && is_array($handle))
  187. {
  188. $this->handle = new Handle();
  189. foreach($handle as $k => $v)
  190. {
  191. $method = 'handle_' . $v[0];
  192. $this->$method($v[1], $file, $base, $path, $name, $ext);
  193. }
  194. }
  195. $output['status'] = 1;
  196. $output['url'] = Maze::$global['host']['image'] . $path . $name . $ext;
  197. return $output;
  198. }
  199. /**
  200. * water
  201. *
  202. * @return mixed
  203. */
  204. private function handle_w($water, $file, $base, $path, &$name, $ext)
  205. {
  206. $name .= '_w';
  207. $file = $this->handle->mark($file, $water, true, $base . $path . $name . $ext);
  208. }
  209. /**
  210. * thumb
  211. *
  212. * @return mixed
  213. */
  214. private function handle_t($id, $file, $base, $path, &$name, $ext)
  215. {
  216. $config = Maze::load('image/thumb-one', $id);
  217. if($config)
  218. {
  219. if(strpos($name, '_t') !== false)
  220. {
  221. $temp = explode('_t', $name);
  222. $name = $temp[0];
  223. }
  224. $name .= '_t' . $id;
  225. $size = $config['width'] . '_' . $config['height'] . '_2';
  226. $file = $this->handle->thumb($file, $size, true, $base . $path . $name . $ext);
  227. }
  228. }
  229. /**
  230. * crop
  231. *
  232. * @return mixed
  233. */
  234. private function handle_c($id, $base, $path, &$name, $ext)
  235. {
  236. $config = Maze::load('image/crop-one', $id);
  237. if($config)
  238. {
  239. $name .= '_c' . $id;
  240. $size = $config['width'] . '_' . $config['height'] . '_2';
  241. //false position
  242. $file = $this->handle->crop($file, $size, false, true, $base . $path . $name . $ext);
  243. echo $file;die;
  244. }
  245. }
  246. /**
  247. * @desc 根据文件名称判断文件格式
  248. * @param $filename 文件名称
  249. * return string
  250. **/
  251. private function _checktype($filename)
  252. {
  253. $file = fopen($filename,"rb");
  254. $bin = fread($file,2);
  255. fclose($file);
  256. $strInfo = @unpack("c2chars",$bin);
  257. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  258. $fileType = '';
  259. switch($typeCode)
  260. {
  261. case 7790:
  262. $fileType = 'exe';
  263. break;
  264. case 7784:
  265. $fileType = 'midi';
  266. break;
  267. case 8297:
  268. $fileType = 'rar';
  269. break;
  270. case 255216:
  271. $fileType = 'jpg';
  272. break;
  273. case 7173:
  274. $fileType = 'gif';
  275. break;
  276. case 13780:
  277. $fileType = 'png';
  278. break;
  279. case 6677:
  280. $fileType = 'bmp';
  281. break;
  282. case 6787:
  283. $fileType = 'swf';
  284. break;
  285. case 6063;
  286. $fileType = 'php|xml';
  287. break;
  288. case 6033:
  289. $fileType = 'html|htm|shtml';
  290. break;
  291. case 8075:
  292. $fileType = 'zip';
  293. break;
  294. case 6782:
  295. $fileType = 'txt';
  296. break;
  297. case 4742:
  298. $fileType = 'js';
  299. break;
  300. case 8273:
  301. $fileType = 'wav';
  302. break;
  303. case 7368:
  304. $fileType = 'mp3';
  305. break;
  306. default:
  307. $fileType = 'unknown'.$typeCode;
  308. break;
  309. }
  310. if($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40')
  311. {
  312. return 'jpg';
  313. }
  314. if($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80')
  315. {
  316. return 'png';
  317. }
  318. if($strInfo['chars1'] == '-48' && $strInfo['chars2'] == '-49')
  319. {
  320. return 'msi';
  321. }
  322. return $fileType;
  323. }
  324. }