心理咨询网
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年8月3日
  7. * 数据处理函数库
  8. */
  9. use core\basic\Config;
  10. // 检测目录是否存在
  11. function check_dir($path, $create = false)
  12. {
  13. if (is_dir($path)) {
  14. return true;
  15. } elseif ($create) {
  16. return create_dir($path);
  17. }
  18. }
  19. // 创建目录
  20. function create_dir($path)
  21. {
  22. if (! file_exists($path)) {
  23. if (mkdir($path, 0777, true)) {
  24. return true;
  25. }
  26. }
  27. return false;
  28. }
  29. // 检查文件是否存在
  30. function check_file($path, $create = false, $content = null)
  31. {
  32. if (file_exists($path)) {
  33. return true;
  34. } elseif ($create) {
  35. return create_file($path, $content);
  36. }
  37. }
  38. // 创建文件
  39. function create_file($path, $content = null, $over = false)
  40. {
  41. if (file_exists($path) && ! $over) {
  42. return false;
  43. } elseif (file_exists($path)) {
  44. @unlink($path);
  45. }
  46. check_dir(dirname($path), true);
  47. $handle = fopen($path, 'w') or error('创建文件失败,请检查目录权限!');
  48. fwrite($handle, $content);
  49. return fclose($handle);
  50. }
  51. // 目录文件夹列表
  52. function dir_list($path)
  53. {
  54. $list = array();
  55. if (! is_dir($path) || ! $filename = scandir($path)) {
  56. return $list;
  57. }
  58. $files = count($filename);
  59. for ($i = 0; $i < $files; $i ++) {
  60. $dir = $path . '/' . $filename[$i];
  61. if (is_dir($dir) && $filename[$i] != '.' && $filename[$i] != '..') {
  62. $list[] = $filename[$i];
  63. }
  64. }
  65. return $list;
  66. }
  67. // 目录文件列表
  68. function file_list($path)
  69. {
  70. $list = array();
  71. if (! is_dir($path) || ! $filename = scandir($path)) {
  72. return $list;
  73. }
  74. $files = count($filename);
  75. for ($i = 0; $i < $files; $i ++) {
  76. $dir = $path . '/' . $filename[$i];
  77. if (is_file($dir)) {
  78. $list[] = $filename[$i];
  79. }
  80. }
  81. return $list;
  82. }
  83. // 目录下文件及文件夹列表
  84. function path_list($path)
  85. {
  86. $list = array();
  87. if (! is_dir($path) || ! $filename = scandir($path)) {
  88. return $list;
  89. }
  90. $files = count($filename);
  91. for ($i = 0; $i < $files; $i ++) {
  92. $dir = $path . '/' . $filename[$i];
  93. if (is_file($dir) || (is_dir($dir) && $filename[$i] != '.' && $filename[$i] != '..')) {
  94. $list[] = $filename[$i];
  95. }
  96. }
  97. return $list;
  98. }
  99. //获取目录和子目录下所有文件
  100. function get_dir($path): array
  101. {
  102. $files = array();
  103. if (is_dir($path)) {
  104. if ($handle = opendir($path)) {
  105. while (($file = readdir($handle)) !== false) {
  106. if ($file != "." && $file != ".." && $file != "file") {
  107. if (is_dir($path . "/" . $file)) {
  108. $files[$file] = get_dir($path . "/" . $file);
  109. } else {
  110. $files[] = $path . "/" . $file;
  111. }
  112. }
  113. }
  114. closedir($handle);
  115. return $files;
  116. }
  117. }
  118. return $files;
  119. }
  120. /**
  121. * 删除目录及目录下所有文件或删除指定文件
  122. *
  123. * @param str $path
  124. * 待删除目录路径
  125. * @param int $delDir
  126. * 是否删除目录,true删除目录,false则只删除文件保留目录
  127. * @return bool 返回删除状态
  128. */
  129. function path_delete($path, $delDir = false, $exFile = array())
  130. {
  131. $result = true; // 对于空目录直接返回true状态
  132. if (! file_exists($path)) {
  133. return $result;
  134. }
  135. if (is_dir($path)) {
  136. if (! ! $dirs = scandir($path)) {
  137. foreach ($dirs as $value) {
  138. if ($value != "." && $value != ".." && ! in_array($value, $exFile)) {
  139. $dir = $path . '/' . $value;
  140. $result = is_dir($dir) ? path_delete($dir, $delDir, $exFile) : unlink($dir);
  141. }
  142. }
  143. if ($result && $delDir) {
  144. return rmdir($path);
  145. } else {
  146. return $result;
  147. }
  148. } else {
  149. return false;
  150. }
  151. } else {
  152. return unlink($path);
  153. }
  154. }
  155. // 拷贝文件夹
  156. function dir_copy($src, $des, $son = 1)
  157. {
  158. if (! is_dir($src)) {
  159. return false;
  160. }
  161. if (! is_dir($des)) {
  162. create_dir($des);
  163. }
  164. $handle = dir($src);
  165. while (! ! $path = $handle->read()) {
  166. if (($path != ".") && ($path != "..")) {
  167. if (is_dir($src . "/" . $path)) {
  168. if ($son)
  169. dir_copy($src . "/" . $path, $des . "/" . $path, $son);
  170. } else {
  171. copy($src . "/" . $path, $des . "/" . $path);
  172. }
  173. }
  174. }
  175. return true;
  176. }
  177. // 判断文件是否是图片
  178. function is_image($path)
  179. {
  180. $types = '.gif|.jpeg|.png|.bmp'; // 定义检查的图片类型
  181. if (file_exists($path)) {
  182. $info = getimagesize($path);
  183. $ext = image_type_to_extension($info['2']);
  184. if (stripos($types, $ext) !== false)
  185. return true;
  186. }
  187. return false;
  188. }
  189. /**
  190. * 文件上传
  191. *
  192. * @param string $input_name表单名称
  193. * @param string $file_ext允许的扩展名
  194. * @param number $max_width最大宽度
  195. * @param number $max_height最大高度
  196. * @return string 返回成功上传文件的路径数组
  197. */
  198. function upload($input_name, $file_ext = null, $max_width = null, $max_height = null, $watermark = false)
  199. {
  200. // 未选择文件返回空
  201. if (! isset($_FILES[$input_name])) {
  202. return '文件超过PHP环境允许的大小!';
  203. } else {
  204. $files = $_FILES[$input_name];
  205. }
  206. // 定义允许上传的扩展
  207. if (! $file_ext) {
  208. $array_ext_allow = Config::get('upload.format', true);
  209. } else {
  210. $array_ext_allow = explode(',', $file_ext);
  211. }
  212. // 未直接传递函数参数,且具有地址参数,则打水印
  213. if (! $watermark && get('watermark', 'int')) {
  214. $watermark = true;
  215. }
  216. $array_save_file = array();
  217. if (is_array($files['tmp_name'])) { // 多文件情况
  218. $file_count = count($files['tmp_name']);
  219. for ($i = 0; $i < $file_count; $i ++) {
  220. if (! $files['error'][$i]) {
  221. $upfile = handle_upload($files['name'][$i], $files['tmp_name'][$i], $array_ext_allow, $max_width, $max_height, $watermark);
  222. if (strrpos($upfile, '/') > 0) {
  223. $array_save_file[] = $upfile;
  224. } else {
  225. $err = $upfile;
  226. }
  227. } else {
  228. $err = '错误代码' . $files['error'][$i];
  229. }
  230. }
  231. } else { // 单文件情况
  232. if (! $files['error']) {
  233. $upfile = handle_upload($files['name'], $files['tmp_name'], $array_ext_allow, $max_width, $max_height, $watermark);
  234. if (strrpos($upfile, '/') > 0) {
  235. $array_save_file[] = $upfile;
  236. } else {
  237. $err = $upfile;
  238. }
  239. } else {
  240. $err = '错误代码' . $files['error'];
  241. }
  242. }
  243. if (isset($err)) {
  244. return $err;
  245. } else {
  246. return $array_save_file;
  247. }
  248. }
  249. // 处理并移动上传文件
  250. function handle_upload($file, $temp, $array_ext_allow, $max_width, $max_height, $watermark)
  251. {
  252. // 定义主存储路径
  253. $save_path = DOC_PATH . STATIC_DIR . '/upload';
  254. $file = explode('.', $file); // 分离文件名及扩展
  255. $file_ext = strtolower(end($file)); // 获取扩展
  256. if (! in_array($file_ext, $array_ext_allow)) {
  257. return $file_ext . '格式的文件不允许上传!';
  258. }
  259. // 文件扩展黑名单
  260. $black = array(
  261. 'php',
  262. 'jsp',
  263. 'asp',
  264. 'vb',
  265. 'exe',
  266. 'sh',
  267. 'cmd',
  268. 'bat',
  269. 'vbs',
  270. 'phtml',
  271. 'class',
  272. 'php2',
  273. 'php3',
  274. 'php4',
  275. 'php5'
  276. );
  277. if (in_array($file_ext, $black)) {
  278. return $file_ext . '格式的文件不允许上传!';
  279. }
  280. $image = array(
  281. 'png',
  282. 'jpg',
  283. 'gif',
  284. 'bmp'
  285. );
  286. $file = array(
  287. 'ppt',
  288. 'pptx',
  289. 'xls',
  290. 'xlsx',
  291. 'doc',
  292. 'docx',
  293. 'pdf',
  294. 'txt'
  295. );
  296. if (in_array($file_ext, $image)) {
  297. $file_type = 'image';
  298. } elseif (in_array($file_ext, $file)) {
  299. $file_type = 'file';
  300. } else {
  301. $file_type = 'other';
  302. }
  303. // 检查文件存储路径
  304. if (! check_dir($save_path . '/' . $file_type . '/' . date('Ymd'), true)) {
  305. return '存储目录创建失败!';
  306. }
  307. $file_path = $save_path . '/' . $file_type . '/' . date('Ymd') . '/' . time() . mt_rand(100000, 999999) . '.' . $file_ext;
  308. if (! move_uploaded_file($temp, $file_path)) { // 从缓存中转存
  309. return '从缓存中转存失败!';
  310. }
  311. $save_file = str_replace(ROOT_PATH, '', $file_path); // 获取文件站点路径
  312. // 如果是图片
  313. if (is_image($file_path)) {
  314. // 进行等比例缩放
  315. if (($reset = resize_img($file_path, $file_path, $max_width, $max_height)) !== true) {
  316. return $reset;
  317. }
  318. // 图片打水印
  319. if ($watermark) {
  320. watermark_img($file_path);
  321. }
  322. }
  323. return $save_file;
  324. }
  325. /**
  326. * *
  327. * 等比缩放图片
  328. *
  329. * @param string $src_image源图片路径
  330. * @param string $out_image输出图像路径
  331. * @param number $max_width最大宽
  332. * @param number $max_height最大高
  333. * @param number $img_quality图片质量
  334. * @return boolean 返回是否成功
  335. */
  336. function resize_img($src_image, $out_image = null, $max_width = null, $max_height = null, $img_quality = 90)
  337. {
  338. // 输出地址
  339. if (! $out_image)
  340. $out_image = $src_image;
  341. // 读取配置文件设置
  342. if (! $max_width)
  343. $max_width = Config::get('upload.max_width') ?: 999999999;
  344. if (! $max_height)
  345. $max_height = Config::get('upload.max_height') ?: 999999999;
  346. // 获取图片属性
  347. list ($width, $height, $type, $attr) = getimagesize($src_image);
  348. // 检查输出目录
  349. check_dir(dirname($out_image), true);
  350. // 无需缩放的图片
  351. if ($width <= $max_width && $height <= $max_height) {
  352. if ($src_image != $out_image) { // 存储地址不一致时进行拷贝
  353. if (! copy($src_image, $out_image)) {
  354. return '缩放图片时拷贝到目的地址失败!';
  355. }
  356. }
  357. return true;
  358. }
  359. // 求缩放比例
  360. if ($max_width && $max_height) {
  361. $scale = min($max_width / $width, $max_height / $height);
  362. } elseif ($max_width) {
  363. $scale = $max_width / $width;
  364. } elseif ($max_height) {
  365. $scale = $max_height / $height;
  366. }
  367. if ($scale < 1) {
  368. switch ($type) {
  369. case 1:
  370. $img = imagecreatefromgif($src_image);
  371. break;
  372. case 2:
  373. $img = imagecreatefromjpeg($src_image);
  374. break;
  375. case 3:
  376. $img = imagecreatefrompng($src_image);
  377. break;
  378. }
  379. $new_width = floor($scale * $width);
  380. $new_height = floor($scale * $height);
  381. $new_img = imagecreatetruecolor($new_width, $new_height); // 创建画布
  382. // 创建透明画布,避免黑色
  383. if ($type == 1 || $type == 3) {
  384. $color = imagecolorallocate($new_img, 255, 255, 255);
  385. imagefill($new_img, 0, 0, $color);
  386. imagecolortransparent($new_img, $color);
  387. }
  388. imagecopyresized($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  389. switch ($type) {
  390. case 1:
  391. imagegif($new_img, $out_image, $img_quality);
  392. break;
  393. case 2:
  394. imagejpeg($new_img, $out_image, $img_quality);
  395. break;
  396. case 3:
  397. imagepng($new_img, $out_image, $img_quality / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9
  398. break;
  399. default:
  400. imagejpeg($new_img, $out_image, $img_quality);
  401. }
  402. imagedestroy($new_img);
  403. imagedestroy($img);
  404. }
  405. return true;
  406. }
  407. // 剪切图片
  408. function cut_img($src_image, $out_image = null, $new_width = null, $new_height = null, $img_quality = 90)
  409. {
  410. // 输出地址
  411. if (! $out_image)
  412. $out_image = $src_image;
  413. // 读取配置文件设置
  414. if (! $new_width && ! $new_height)
  415. return;
  416. // 获取图片属性
  417. list ($width, $height, $type, $attr) = getimagesize($src_image);
  418. switch ($type) {
  419. case 1:
  420. $img = imagecreatefromgif($src_image);
  421. break;
  422. case 2:
  423. $img = imagecreatefromjpeg($src_image);
  424. break;
  425. case 3:
  426. $img = imagecreatefrompng($src_image);
  427. break;
  428. }
  429. // 不限定是等比例缩放
  430. if (! $new_width) {
  431. $new_width = floor($width * ($new_height / $height));
  432. }
  433. if (! $new_height) {
  434. $new_height = floor($height * ($new_width / $width));
  435. }
  436. // 计算裁剪是变大缩小方式
  437. if ($width >= $new_width && $height >= $new_height) { // 长宽均满足
  438. $cut_width = $new_width;
  439. $cut_height = $new_height;
  440. } else { // 有一边不满足
  441. $scale1 = $width / $new_width;
  442. $scale2 = $height / $new_height;
  443. if ($scale1 < $scale2) { // 变化越多的一边取全值,其余一边等比例缩放
  444. $cut_width = $width;
  445. $cut_height = floor($height * ($width / $new_width));
  446. } else {
  447. $cut_width = floor($new_width * ($height / $new_height));
  448. $cut_height = $height;
  449. }
  450. }
  451. // 创建画布
  452. $new_img = imagecreatetruecolor($new_width, $new_height);
  453. // 创建透明画布,避免黑色
  454. if ($type == 1 || $type == 3) {
  455. $color = imagecolorallocate($new_img, 255, 255, 255);
  456. imagefill($new_img, 0, 0, $color);
  457. imagecolortransparent($new_img, $color);
  458. }
  459. imagecopyresized($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $cut_width, $cut_height);
  460. check_dir(dirname($out_image), true); // 检查输出目录
  461. switch ($type) {
  462. case 1:
  463. imagegif($new_img, $out_image, $img_quality);
  464. break;
  465. case 2:
  466. imagejpeg($new_img, $out_image, $img_quality);
  467. break;
  468. case 3:
  469. imagepng($new_img, $out_image, $img_quality / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9
  470. break;
  471. default:
  472. imagejpeg($new_img, $out_image, $img_quality);
  473. }
  474. imagedestroy($new_img);
  475. imagedestroy($img);
  476. return true;
  477. }
  478. // 图片水印
  479. function watermark_img($src_image, $out_image = null, $position = null, $watermark_image = null, $watermark_text = '', $watermark_text_size = null, $watermark_text_color = null)
  480. {
  481. if (! Config::get('watermark_open')) {
  482. return;
  483. }
  484. // 输出地址
  485. if (! $out_image)
  486. $out_image = $src_image;
  487. // 如果不存在文字及图片则直接返回
  488. if (! $watermark_text) {
  489. $watermark_text = Config::get('watermark_text') ?: 'PbootCMS';
  490. }
  491. $watermark_image = $watermark_image ?: Config::get('watermark_pic');
  492. if (! $watermark_text && ! $watermark_image) {
  493. return;
  494. }
  495. // 获取图片属性
  496. list ($width1, $height1, $type1, $attr1) = getimagesize($src_image);
  497. switch ($type1) {
  498. case 1:
  499. $img1 = imagecreatefromgif($src_image);
  500. break;
  501. case 2:
  502. $img1 = imagecreatefromjpeg($src_image);
  503. break;
  504. case 3:
  505. $img1 = imagecreatefrompng($src_image);
  506. break;
  507. }
  508. if ($watermark_image) {
  509. $watermark_image = ROOT_PATH . $watermark_image;
  510. // 获取水印图片
  511. list ($width2, $height2, $type2, $attr2) = getimagesize($watermark_image);
  512. switch ($type2) {
  513. case 1:
  514. $img2 = imagecreatefromgif($watermark_image);
  515. break;
  516. case 2:
  517. $img2 = imagecreatefromjpeg($watermark_image);
  518. break;
  519. case 3:
  520. $img2 = imagecreatefrompng($watermark_image);
  521. break;
  522. }
  523. } else {
  524. if (! $watermark_text_size) {
  525. $watermark_text_size = Config::get('watermark_text_size') ?: 16;
  526. }
  527. if (! $watermark_text_color) {
  528. $watermark_text_color = Config::get('watermark_text_color') ?: '100,100,100';
  529. }
  530. $colors = explode(',', $watermark_text_color);
  531. if (Config::get('watermark_text_font')) {
  532. $font = ROOT_PATH . Config::get('watermark_text_font');
  533. } else {
  534. return;
  535. }
  536. // 手动创建水印图像
  537. $fontsize = $watermark_text_size;
  538. $width2 = mb_strlen($watermark_text, 'UTF-8') * ($fontsize + 10) + 20;
  539. $height2 = $fontsize + 10;
  540. $img2 = imagecreatetruecolor($width2, $height2);
  541. $color = imagecolorallocate($img2, 255, 255, 255);
  542. imagefill($img2, 0, 0, $color);
  543. imagecolortransparent($img2, $color); // 创建透明图
  544. $textcolor = imagecolorallocate($img2, $colors[0], $colors[1], $colors[2]);
  545. imagettftext($img2, $fontsize, 0, 5, $fontsize + 5, $textcolor, $font, $watermark_text);
  546. }
  547. // 现对图片太大时,自动缩放水印
  548. if ($width1 < $width2 * 3 || $height1 < $height2) {
  549. $scale = min(($width1 / 3) / $width2, ($height1 / 2) / $height2); // 求缩放比例
  550. $new_width = floor($scale * $width2);
  551. $new_height = floor($scale * $height2);
  552. } else {
  553. $new_width = $width2;
  554. $new_height = $height2;
  555. }
  556. // 水印位置
  557. if (! $position) {
  558. $position = Config::get('watermark_position') ?: 4;
  559. }
  560. switch ($position) {
  561. case '1':
  562. $x = 15;
  563. $y = 15;
  564. break;
  565. case '2':
  566. $x = $width1 - $new_width - 15;
  567. $y = 20;
  568. break;
  569. case '3':
  570. $x = 20;
  571. $y = $height1 - $new_height - 15;
  572. break;
  573. case '5':
  574. $x = ($width1 - $new_width) / 2;
  575. $y = ($height1 - $new_height) / 2;
  576. break;
  577. default:
  578. $x = $width1 - $new_width - 15;
  579. $y = $height1 - $new_height - 15;
  580. break;
  581. }
  582. // 创建透明画布,避免黑色
  583. if ($type1 == 1 || $type1 == 3) {
  584. $out = imagecreatetruecolor($width1, $height1);
  585. $color = imagecolorallocate($out, 255, 255, 255);
  586. imagefill($out, 0, 0, $color);
  587. imagecolortransparent($out, $color);
  588. imagecopy($out, $img1, 0, 0, 0, 0, $width1, $height1);
  589. } else {
  590. $out = $img1;
  591. }
  592. // 打上水印
  593. imagecopyresized($out, $img2, $x, $y - 10, 0, 0, $new_width, $new_height, $width2, $height2);
  594. check_dir(dirname($out_image), true); // 检查输出目录
  595. // 输出图片
  596. switch ($type1) {
  597. case 1:
  598. imagegif($out, $out_image, 90);
  599. break;
  600. case 2:
  601. imagejpeg($out, $out_image, 90);
  602. break;
  603. case 3:
  604. imagepng($out, $out_image, 90 / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9
  605. break;
  606. default:
  607. imagejpeg($out, $out_image, 90);
  608. }
  609. imagedestroy($img1);
  610. imagedestroy($img2);
  611. return true;
  612. }