upfile.fun.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. function check_upimage($file = 'filename')
  3. {
  4. global $mymps_global;
  5. $size = $mymps_global['cfg_upimg_size'] * 1024;
  6. $upimg_allow = explode(',', $mymps_global['cfg_upimg_type']);
  7. if ($size < $_FILES[$file]['size']) {
  8. write_msg('上传文件应小于' . $mymps_global['cfg_upimg_size'] . 'KB');
  9. exit();
  10. }
  11. if (!in_array(FileExt($_FILES[$file]['name']), $upimg_allow)) {
  12. write_msg('系统只允许上传' . $mymps_global['cfg_upimg_type'] . '格式的图片!');
  13. }
  14. if (!preg_match('/^image\\//i', $_FILES[$file]['type'])) {
  15. write_msg('很抱歉,系统无法识别您上传的文件的格式,请换一张图片上传!');
  16. }
  17. return true;
  18. }
  19. function mymps_check_upimage($file = 'filename')
  20. {
  21. if (is_array($_FILES)) {
  22. for ($i = 0; $i < count($_FILES); $i++) {
  23. if ($_FILES[$file . $i]['name']) {
  24. check_upimage($file . $i);
  25. }
  26. }
  27. }
  28. }
  29. function upload_img_num($file = 'filename')
  30. {
  31. if (is_array($_FILES)) {
  32. $num = 0;
  33. for ($i = 0; $i < count($_FILES); $i++) {
  34. $num = $_FILES[$file . $i]['error'] != 4 ? $num + 1 : $num;
  35. }
  36. return $num;
  37. }
  38. else {
  39. return 0;
  40. }
  41. }
  42. function start_upload($file_name, $destination_folder, $watermark = 0, $limit_width = '', $limit_height = '', $edit_filename = '', $edit_pre_filename = '')
  43. {
  44. global $mymps_global;
  45. global $timestamp;
  46. !is_uploaded_file($_FILES[$file_name]['tmp_name']) && write_msg('请重新选择您要上传的图片!');
  47. $file = $_FILES[$file_name];
  48. @createdir(MYMPS_UPLOAD . $destination_folder);
  49. $file_name = $file['tmp_name'];
  50. $pinfo = pathinfo($file['name']);
  51. $ftype = $pinfo['extension'];
  52. $fname = $pinfo[basename];
  53. if (empty($edit_filename) && empty($edit_pre_filename)) {
  54. $destination_file = $timestamp . random() . '.' . $ftype;
  55. $destination = MYMPS_UPLOAD . $destination_folder . $destination_file;
  56. $small_destination = MYMPS_UPLOAD . $destination_folder . 'pre_' . $destination_file;
  57. }
  58. else {
  59. $destination = MYMPS_ROOT . $edit_filename;
  60. $small_destination = MYMPS_ROOT . $edit_pre_filename;
  61. $forbidarray = array(MYMPS_ROOT . '/images/logo.gif', MYMPS_ROOT . '/images/nopic.gif', MYMPS_ROOT . '/images/nophoto.jpg', MYMPS_ROOT . '/images/noavatar.gif', MYMPS_ROOT . '/images/noavatar_small.gif');
  62. if (!in_array($destination, $forbidarray) && $destination != MYMPS_ROOT) {
  63. @unlink($destination);
  64. }
  65. if (!in_array($small_destination, $forbidarray) && $destination != MYMPS_ROOT) {
  66. @unlink($small_destination);
  67. }
  68. unset($forbidarray);
  69. }
  70. if (file_exists($destination)) {
  71. write_msg('同名图片已存在,请重新选择您要上传的图片!');
  72. }
  73. if (!move_uploaded_file($file_name, $destination)) {
  74. write_msg('图片上传失败,请重新选择您要上传的图片!');
  75. }
  76. if (strstr($destination_folder, '/information/') && '409600' < $file['size']) {
  77. ImageResize($destination, 600, 600);
  78. }
  79. if ($watermark == 1 && function_exists('gd_info')) {
  80. WaterImg($destination);
  81. }
  82. if (!empty($limit_width) && !empty($limit_height)) {
  83. ImageResize($destination, $limit_width, $limit_height, $small_destination);
  84. $mymps_image = array();
  85. $mymps_image[0] = $edit_filename ? $edit_filename : '/attachment' . $destination_folder . $destination_file;
  86. $mymps_image[1] = $proportion != 1 ? ($edit_pre_filename ? $edit_pre_filename : '/attachment' . $destination_folder . 'pre_' . $destination_file) : $mymps_image[0];
  87. }
  88. else {
  89. $mymps_image = $edit_filename ? $edit_filename : '/attachment' . $destination_folder . $destination_file;
  90. }
  91. return $mymps_image;
  92. }
  93. function ImageResize($srcFile, $toW, $toH, $toFile = '')
  94. {
  95. global $cfg_photo_type;
  96. global $cfg_jpeg_query;
  97. if (empty($cfg_jpeg_query)) {
  98. $cfg_jpeg_query = 85;
  99. }
  100. if ($toFile == '') {
  101. $toFile = $srcFile;
  102. }
  103. $info = '';
  104. $srcInfo = GetImageSize($srcFile, $info);
  105. switch ($srcInfo[2]) {
  106. case 1:
  107. if (!$cfg_photo_type['gif']) {
  108. return false;
  109. }
  110. $im = imagecreatefromgif($srcFile);
  111. break;
  112. case 2:
  113. if (!$cfg_photo_type['jpeg']) {
  114. return false;
  115. }
  116. $im = imagecreatefromjpeg($srcFile);
  117. break;
  118. case 3:
  119. if (!$cfg_photo_type['png']) {
  120. return false;
  121. }
  122. $im = imagecreatefrompng($srcFile);
  123. break;
  124. case 6:
  125. if (!$cfg_photo_type['bmp']) {
  126. return false;
  127. }
  128. $im = imagecreatefromwbmp($srcFile);
  129. break;
  130. }
  131. $srcW = ImageSX($im);
  132. $srcH = ImageSY($im);
  133. $toWH = $toW / $toH;
  134. $srcWH = $srcW / $srcH;
  135. if ($toWH <= $srcWH) {
  136. $ftoW = $toW;
  137. $ftoH = $ftoW * ($srcH / $srcW);
  138. }
  139. else {
  140. $ftoH = $toH;
  141. $ftoW = $ftoH * ($srcW / $srcH);
  142. }
  143. if ($toW < $srcW || $toH < $srcH) {
  144. if (function_exists('imagecreatetruecolor')) {
  145. @$ni = imagecreatetruecolor($ftoW, $ftoH);
  146. if ($ni) {
  147. imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  148. }
  149. else {
  150. $ni = imagecreate($ftoW, $ftoH);
  151. imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  152. }
  153. }
  154. else {
  155. $ni = imagecreate($ftoW, $ftoH);
  156. imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
  157. }
  158. switch ($srcInfo[2]) {
  159. case 1:
  160. imagegif($ni, $toFile);
  161. break;
  162. case 2:
  163. imagejpeg($ni, $toFile, $cfg_jpeg_query);
  164. break;
  165. case 3:
  166. imagepng($ni, $toFile);
  167. break;
  168. case 6:
  169. imagebmp($ni, $toFile);
  170. break;
  171. default:
  172. return false;
  173. }
  174. imagedestroy($ni);
  175. }
  176. else {
  177. copy($srcFile, $toFile);
  178. }
  179. imagedestroy($im);
  180. return true;
  181. }
  182. function gdversion()
  183. {
  184. static $gd_version_number;
  185. if ($gd_version_number === NULL) {
  186. ob_start();
  187. phpinfo(8);
  188. $module_info = ob_get_contents();
  189. ob_end_clean();
  190. if (preg_match('/\\bgd\\s+version\\b[^\\d
  191. ]+?([\\d\\.]+)/i', $module_info, $matches)) {
  192. $gdversion_h = $matches[1];
  193. }
  194. else {
  195. $gdversion_h = 0;
  196. }
  197. }
  198. return $gdversion_h;
  199. }
  200. function WaterImg($srcFile, $fromGo = 'up')
  201. {
  202. global $mymps_global;
  203. include MYMPS_DATA . '/watermark.inc.php';
  204. if ($photo_markup != '1') {
  205. return NULL;
  206. }
  207. $info = '';
  208. $srcInfo = GetImageSize($srcFile, $info);
  209. $srcFile_w = $srcInfo[0];
  210. $srcFile_h = $srcInfo[1];
  211. if ($srcFile_w < $photo_wwidth || $srcFile_h < $photo_wheight) {
  212. return NULL;
  213. }
  214. if ($fromGo == 'up' && $photo_markup == '0') {
  215. return NULL;
  216. }
  217. if ($fromGo == 'down' && $photo_markdown == '0') {
  218. return NULL;
  219. }
  220. $trueMarkimg = MYMPS_ROOT . $photo_markimg;
  221. if (!file_exists($trueMarkimg) || empty($photo_markimg)) {
  222. $trueMarkimg = '';
  223. }
  224. ImgWaterMark($srcFile, $photo_waterpos, $trueMarkimg, $photo_watertext, $photo_fontsize, $photo_fontcolor, $photo_diaphaneity);
  225. }
  226. function ImgWaterMark($srcFile, $w_pos = 0, $w_img = '', $w_text = '', $w_font = 5, $w_color = '#FF0000', $w_pct)
  227. {
  228. $font_type = MYMPS_DATA . '/ttf/number.ttf';
  229. if (empty($srcFile) || !file_exists($srcFile)) {
  230. return NULL;
  231. }
  232. $info = '';
  233. $srcInfo = getimagesize($srcFile, $info);
  234. $srcFile_w = $srcInfo[0];
  235. $srcFile_h = $srcInfo[1];
  236. switch ($srcInfo[2]) {
  237. case 1:
  238. if (!function_exists('imagecreatefromgif')) {
  239. return NULL;
  240. }
  241. $srcFile_img = imagecreatefromgif($srcFile);
  242. break;
  243. case 2:
  244. if (!function_exists('imagecreatefromjpeg')) {
  245. return NULL;
  246. }
  247. $srcFile_img = imagecreatefromjpeg($srcFile);
  248. break;
  249. case 3:
  250. if (!function_exists('imagecreatefrompng')) {
  251. return NULL;
  252. }
  253. $srcFile_img = imagecreatefrompng($srcFile);
  254. break;
  255. case 6:
  256. if (!function_exists('imagewbmp')) {
  257. return NULL;
  258. }
  259. $srcFile_img = imagecreatefromwbmp($srcFile);
  260. break;
  261. default:
  262. return NULL;
  263. }
  264. if (!empty($w_img) && file_exists($w_img)) {
  265. $ifWaterImage = 1;
  266. $info = '';
  267. $water_info = getimagesize($w_img, $info);
  268. $width = $water_info[0];
  269. $height = $water_info[1];
  270. switch ($water_info[2]) {
  271. case 1:
  272. if (!function_exists('imagecreatefromgif')) {
  273. return NULL;
  274. }
  275. $water_img = imagecreatefromgif($w_img);
  276. break;
  277. case 2:
  278. if (!function_exists('imagecreatefromjpeg')) {
  279. return NULL;
  280. }
  281. $water_img = imagecreatefromjpeg($w_img);
  282. break;
  283. case 3:
  284. if (!function_exists('imagecreatefrompng')) {
  285. return NULL;
  286. }
  287. $water_img = imagecreatefrompng($w_img);
  288. break;
  289. case 6:
  290. if (!function_exists('imagecreatefromwbmp')) {
  291. return NULL;
  292. }
  293. $srcFile_img = imagecreatefromwbmp($w_img);
  294. break;
  295. default:
  296. return NULL;
  297. }
  298. }
  299. else {
  300. $ifWaterImage = 0;
  301. $ifttf = 1;
  302. @$temp = imagettfbbox($w_font, 0, $font_type, $w_text);
  303. $width = $temp[2] - $temp[6];
  304. $height = $temp[3] - $temp[7];
  305. unset($temp);
  306. if (empty($width) && empty($height)) {
  307. $width = strlen($w_text) * 10;
  308. $height = 20;
  309. $ifttf = 0;
  310. }
  311. }
  312. if ($w_pos == 0) {
  313. $wX = rand(0, $srcFile_w - $width);
  314. $wY = rand(0, $srcFile_h - $height);
  315. }
  316. else if ($w_pos == 1) {
  317. $wX = 5;
  318. if ($ifttf == 1) {
  319. $wY = $height + 5;
  320. }
  321. else {
  322. $wY = 5;
  323. }
  324. }
  325. else if ($w_pos == 2) {
  326. $wX = 5;
  327. $wY = ($srcFile_h - $height) / 2;
  328. }
  329. else if ($w_pos == 3) {
  330. $wX = 5;
  331. $wY = $srcFile_h - $height - 5;
  332. }
  333. else if ($w_pos == 4) {
  334. $wX = ($srcFile_w - $width) / 2;
  335. if ($ifttf == 1) {
  336. $wY = $height + 5;
  337. }
  338. else {
  339. $wY = 5;
  340. }
  341. }
  342. else if ($w_pos == 5) {
  343. $wX = ($srcFile_w - $width) / 2;
  344. $wY = ($srcFile_h - $height) / 2;
  345. }
  346. else if ($w_pos == 6) {
  347. $wX = ($srcFile_w - $width) / 2;
  348. $wY = $srcFile_h - $height - 5;
  349. }
  350. else if ($w_pos == 7) {
  351. $wX = $srcFile_w - $width - 5;
  352. if ($ifttf == 1) {
  353. $wY = $height + 5;
  354. }
  355. else {
  356. $wY = 5;
  357. }
  358. }
  359. else if ($w_pos == 8) {
  360. $wX = $srcFile_w - $width - 5;
  361. $wY = ($srcFile_h - $height) / 2;
  362. }
  363. else if ($w_pos == 9) {
  364. $wX = $srcFile_w - $width - 5;
  365. $wY = $srcFile_h - $height - 5;
  366. }
  367. else {
  368. $wX = ($srcFile_w - $width) / 2;
  369. $wY = ($srcFile_h - $height) / 2;
  370. }
  371. imagealphablending($srcFile_img, true);
  372. if ($ifWaterImage) {
  373. imagecopymerge($srcFile_img, $water_img, $wX, $wY, 0, 0, $width, $height, $w_pct);
  374. }
  375. else {
  376. if (!empty($w_color) && strlen($w_color) == 7) {
  377. $R = hexdec(substr($w_color, 1, 2));
  378. $G = hexdec(substr($w_color, 3, 2));
  379. $B = hexdec(substr($w_color, 5));
  380. }
  381. else {
  382. return NULL;
  383. }
  384. if ($ifttf == 1) {
  385. imagettftext($srcFile_img, $w_font, 0, $wX, $wY, imagecolorallocate($srcFile_img, $R, $G, $B), $font_type, $w_text);
  386. }
  387. else {
  388. imagestring($srcFile_img, $w_font, $wX, $wY, $w_text, imagecolorallocate($srcFile_img, $R, $G, $B));
  389. }
  390. }
  391. switch ($srcInfo[2]) {
  392. case 1:
  393. if (function_exists('imagegif')) {
  394. imagegif($srcFile_img, $srcFile);
  395. }
  396. break;
  397. case 2:
  398. if (function_exists('imagejpeg')) {
  399. imagejpeg($srcFile_img, $srcFile);
  400. }
  401. break;
  402. case 3:
  403. if (function_exists('imagepng')) {
  404. imagepng($srcFile_img, $srcFile);
  405. }
  406. break;
  407. case 6:
  408. if (function_exists('imagewbmp')) {
  409. imagewbmp($srcFile_img, $srcFile);
  410. }
  411. break;
  412. default:
  413. return NULL;
  414. }
  415. if (isset($water_info)) {
  416. unset($water_info);
  417. }
  418. if (isset($water_img)) {
  419. imagedestroy($water_img);
  420. }
  421. unset($srcInfo);
  422. imagedestroy($srcFile_img);
  423. }
  424. !defined('IN_MYMPS') && exit('FORBIDDEN');
  425. $cfg_photo_type['gif'] = false;
  426. $cfg_photo_type['jpeg'] = false;
  427. $cfg_photo_type['png'] = false;
  428. $cfg_photo_type['wbmp'] = false;
  429. $cfg_photo_typenames = array();
  430. $cfg_photo_support = '';
  431. if (function_exists('imagecreatefromgif') && function_exists('imagegif')) {
  432. $cfg_photo_type['gif'] = true;
  433. $cfg_photo_typenames[] = 'image/gif';
  434. $cfg_photo_support .= 'GIF ';
  435. }
  436. if (function_exists('imagecreatefromjpeg') && function_exists('imagejpeg')) {
  437. $cfg_photo_type['jpeg'] = true;
  438. $cfg_photo_typenames[] = 'image/pjpeg';
  439. $cfg_photo_typenames[] = 'image/jpeg';
  440. $cfg_photo_support .= 'JPEG ';
  441. }
  442. if (function_exists('imagecreatefrompng') && function_exists('imagepng')) {
  443. $cfg_photo_type['png'] = true;
  444. $cfg_photo_typenames[] = 'image/png';
  445. $cfg_photo_typenames[] = 'image/x-png';
  446. $cfg_photo_support .= 'PNG ';
  447. }
  448. if (function_exists('imagecreatefromwbmp') && function_exists('imagewbmp')) {
  449. $cfg_photo_type['wbmp'] = true;
  450. $cfg_photo_typenames[] = 'image/wbmp';
  451. $cfg_photo_support .= 'WBMP ';
  452. }
  453. ?>