2847c5f51e84f114b02d6ddb472e8660ff1f1797.svn-base 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php namespace Maze\Plad;
  2. class Upload
  3. {
  4. /**
  5. * base path
  6. *
  7. * @var string
  8. */
  9. const path = 'data';
  10. /**
  11. * config
  12. *
  13. * @var array
  14. */
  15. private $config = array();
  16. /**
  17. * data
  18. *
  19. * @var array
  20. */
  21. private $data = array();
  22. /**
  23. * __construct
  24. *
  25. * @return mixed
  26. */
  27. public function __construct()
  28. {}
  29. /**
  30. * save
  31. *
  32. * @return mixed
  33. */
  34. public function save($config)
  35. {
  36. $this->config = $config;
  37. $this->make();
  38. return $this->copy();
  39. }
  40. /**
  41. * data
  42. *
  43. * @return array
  44. */
  45. public function data()
  46. {
  47. return $this->data;
  48. }
  49. /**
  50. * create file name
  51. *
  52. * @return int
  53. */
  54. private function file()
  55. {
  56. //return md5(microtime()+rand()*100);
  57. $ext = '.jpg';
  58. if($this->data['tmp']['type'] == 'image/gif')
  59. {
  60. $ext = '.gif';
  61. }
  62. if(empty($this->config['filename']))
  63. {
  64. $filename = md5($this->data['tmp']['name']);
  65. $this->data['filename'] = $filename . $ext;
  66. }
  67. else
  68. {
  69. $this->data['filename'] = $this->config['filename'];
  70. }
  71. }
  72. /**
  73. * create path
  74. *
  75. * @return mixed
  76. */
  77. private function create($path)
  78. {
  79. if(is_array($path))
  80. {
  81. foreach($path as $v)
  82. {
  83. $this->create($v);
  84. }
  85. }
  86. else
  87. {
  88. if(isset($this->config['id']) && $this->config['id'] > 0)
  89. {
  90. $id = ceil($this->config['id']/1000);
  91. $filepath = \Helper::path(MAZE_PATH . self::path, $path . $id . '/');
  92. }
  93. else
  94. {
  95. $filepath = \Helper::path(MAZE_PATH . self::path, $path . date("Y") . '/' . date("m") . '/' . date("d") . '/');
  96. }
  97. $this->data[$path . '_file'] = $filepath . $this->data['filename'];
  98. }
  99. }
  100. /**
  101. * make file
  102. *
  103. * @return mixed
  104. */
  105. private function make()
  106. {
  107. $this->checkConfig();
  108. $this->file();
  109. $this->create(array('source', 'view'));
  110. $this->data['tmp'] = $this->post($this->config['name']);
  111. }
  112. /**
  113. * copy file
  114. *
  115. * @return mixed
  116. */
  117. private function copy()
  118. {
  119. if(isset($this->config['filesize']) && $this->config['filesize'] > 0 && $this->data['tmp']['size'] > $this->config['filesize'])
  120. {
  121. return $this->error('file size error -1');
  122. }
  123. if(isset($this->config['filelimit']) && strstr($this->config['filelimit'], '*'))
  124. {
  125. $imgstream = file_get_contents($this->data['tmp']['tmp_name']);
  126. $im = imagecreatefromstring($imgstream);
  127. $width = imagesx($im);
  128. $height = imagesy($im);
  129. @imagedestroy($im);
  130. $attribute = explode(',', $this->config['filelimit']);
  131. $array = explode('*', $attribute[0]);
  132. if($width > $array[0])
  133. {
  134. return $this->error('file max width error -2');
  135. }
  136. if($height > $array[1])
  137. {
  138. return $this->error('file max height error -3');
  139. }
  140. if(isset($attribute[1]))
  141. {
  142. $array = explode('*', $attribute[1]);
  143. if($width < $array[0])
  144. {
  145. return $this->error('file min width error -4');
  146. }
  147. if($height < $array[1])
  148. {
  149. return $this->error('file min height error -5');
  150. }
  151. }
  152. }
  153. if($this->data['type'] && !strstr($this->data['type'], $this->data['tmp']['type']))
  154. {
  155. return $this->error('upload type error -6');
  156. }
  157. if(!copy($this->data['tmp']['tmp_name'], $this->data['file']))
  158. {
  159. return $this->error('upload error -7');
  160. }
  161. else
  162. {
  163. # 复制一份用来给用户看的,我们保留一份吧
  164. copy($this->data['file'], $this->data['view_file']);
  165. //@unlink($this->data['tmp']['tmp_name']);
  166. $this->data['name'] = $this->data['tmp']['name'];
  167. $this->data['type'] = $this->data['tmp']['type'];
  168. if(isset($this->config['width']) && $this->config['width'])
  169. {
  170. $imgstream = file_get_contents($this->data['file']);
  171. $im = imagecreatefromstring($imgstream);
  172. $this->data['width'] = imagesx($im);
  173. $this->data['height'] = imagesy($im);
  174. @imagedestroy($im);
  175. }
  176. $img = false;
  177. //图片压缩
  178. if(isset($this->config['compress']))
  179. {
  180. $img = \Helper::core('img');
  181. $img->compress($this->data['file'], $this->config['compress']);
  182. }
  183. //添加水印
  184. if(isset($this->config['mark']))
  185. {
  186. if(!$img)
  187. {
  188. $img = \Helper::core('img');
  189. }
  190. $img->mark($this->data['file'], $this->config['mark']);
  191. }
  192. //建立小图
  193. if(isset($this->config['thumb']))
  194. {
  195. if(!$img)
  196. {
  197. $img = \Helper::core('img');
  198. }
  199. $img->thumb($this->data['file'], $this->config['thumb']);
  200. }
  201. //建立小图
  202. if(isset($this->config['crop']))
  203. {
  204. if(!$img)
  205. {
  206. $img = \Helper::core('img');
  207. }
  208. $img->crop($this->data['file'], $this->config['crop']);
  209. }
  210. return $this->data;
  211. }
  212. }
  213. /**
  214. * @desc 检测是否设置了配置
  215. * @param *
  216. * @author leo(suwi.bin)
  217. * @date 2012-03-23
  218. */
  219. private function checkConfig()
  220. {
  221. if(!$this->config)
  222. {
  223. $this->error('config error');
  224. }
  225. if(!isset($this->config['name']))
  226. {
  227. $this->error('name error');
  228. }
  229. if(!isset($this->config['filetype']))
  230. {
  231. $this->config['filetype'] = 'file';
  232. }
  233. if(isset($this->config['filepath']) && $this->config['filepath'])
  234. {
  235. $this->config['filepath'] .= '/';
  236. }
  237. $this->data['type'] = false;
  238. switch($this->config['filetype'])
  239. {
  240. case 'file':
  241. $this->data['type'] = 'image/png,image/x-png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/bmp,application/javascript,text/css,application/octet-stream';
  242. break;
  243. case 'img':
  244. $this->data['type'] = 'image/png,image/jpg,image/x-png,image/jpeg,image/pjpeg,image/gif,image/bmp,application/octet-stream';
  245. break;
  246. case 'excel':
  247. $this->data['type'] = 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  248. break;
  249. case 'stream':
  250. $this->data['type'] = 'application/octet-stream';
  251. break;
  252. }
  253. }
  254. /**
  255. * @desc 获取post数据
  256. * @param *
  257. * @author leo(suwi.bin)
  258. * @date 2012-03-23
  259. */
  260. private function post($name)
  261. {
  262. # 判断是否网络文件
  263. if(strpos($name, 'http://') !== false)
  264. {
  265. $state = false;
  266. if(strpos($name, '.jpg') !== false)
  267. {
  268. $state = true;
  269. $data['type'] = 'image/jpeg';
  270. }
  271. elseif(strpos($name, '.gif') !== false)
  272. {
  273. $state = true;
  274. $data['type'] = 'image/gif';
  275. $this->_data['filename'] = str_replace('.jpg', '.gif', $this->_data['filename']);
  276. }
  277. else
  278. {
  279. $data['type'] = '';
  280. }
  281. if($state == true)
  282. {
  283. $data['name'] = md5($name);
  284. $data['tmp_name'] = $name;
  285. }
  286. return $data;
  287. }
  288. else
  289. {
  290. return $this->data['post'][$name] = \Input::get($name);
  291. }
  292. return false;
  293. }
  294. /**
  295. * @desc 匹配错误
  296. * @param *
  297. * @author leo(suwi.bin)
  298. * @date 2012-03-23
  299. */
  300. private function error($string, $type = 1)
  301. {
  302. $errstr = '' ;
  303. $errstr .= "Upload Error:" . $string . "\n";
  304. return $errstr;
  305. }
  306. }