111
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

603 rivejä
18 KiB

  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. * 删除目录及目录下所有文件或删除指定文件
  101. *
  102. * @param str $path
  103. * 待删除目录路径
  104. * @param int $delDir
  105. * 是否删除目录,true删除目录,false则只删除文件保留目录
  106. * @return bool 返回删除状态
  107. */
  108. function path_delete($path, $delDir = false)
  109. {
  110. $result = true; // 对于空目录直接返回true状态
  111. if (! file_exists($path)) {
  112. return $result;
  113. }
  114. if (is_dir($path)) {
  115. if (! ! $dirs = scandir($path)) {
  116. foreach ($dirs as $value) {
  117. if ($value != "." && $value != "..") {
  118. $dir = $path . '/' . $value;
  119. $result = is_dir($dir) ? path_delete($dir, $delDir) : unlink($dir);
  120. }
  121. }
  122. if ($result && $delDir) {
  123. return rmdir($path);
  124. } else {
  125. return $result;
  126. }
  127. } else {
  128. return false;
  129. }
  130. } else {
  131. return unlink($path);
  132. }
  133. }
  134. // 判断文件是否是图片
  135. function is_image($path)
  136. {
  137. $types = '.gif|.jpeg|.png|.bmp'; // 定义检查的图片类型
  138. if (file_exists($path)) {
  139. $info = getimagesize($path);
  140. $ext = image_type_to_extension($info['2']);
  141. if (stripos($types, $ext) !== false)
  142. return true;
  143. }
  144. return false;
  145. }
  146. /**
  147. * 文件上传
  148. *
  149. * @param string $input_name表单名称
  150. * @param string $file_ext允许的扩展名
  151. * @param number $max_width最大宽度
  152. * @param number $max_height最大高度
  153. * @return string 返回成功上传文件的路径数组
  154. */
  155. function upload($input_name, $file_ext = null, $max_width = null, $max_height = null, $watermark = false)
  156. {
  157. // 未选择文件返回空
  158. if (! isset($_FILES[$input_name])) {
  159. return '文件超过PHP环境允许的大小!';
  160. } else {
  161. $files = $_FILES[$input_name];
  162. }
  163. // 定义允许上传的扩展
  164. if (! $file_ext) {
  165. $array_ext_allow = Config::get('upload.format', true);
  166. } else {
  167. $array_ext_allow = explode(',', $file_ext);
  168. }
  169. // 未直接传递函数参数,且具有地址参数,则打水印
  170. if (! $watermark && get('watermark', 'int')) {
  171. $watermark = true;
  172. }
  173. $array_save_file = array();
  174. if (is_array($files['tmp_name'])) { // 多文件情况
  175. $file_count = count($files['tmp_name']);
  176. for ($i = 0; $i < $file_count; $i ++) {
  177. if (! $files['error'][$i]) {
  178. $upfile = handle_upload($files['name'][$i], $files['tmp_name'][$i], $array_ext_allow, $max_width, $max_height, $watermark);
  179. if (strrpos($upfile, '/') > 0) {
  180. $array_save_file[] = $upfile;
  181. } else {
  182. $err = $upfile;
  183. }
  184. } else {
  185. $err = '错误代码' . $files['error'][$i];
  186. }
  187. }
  188. } else { // 单文件情况
  189. if (! $files['error']) {
  190. $upfile = handle_upload($files['name'], $files['tmp_name'], $array_ext_allow, $max_width, $max_height, $watermark);
  191. if (strrpos($upfile, '/') > 0) {
  192. $array_save_file[] = $upfile;
  193. } else {
  194. $err = $upfile;
  195. }
  196. } else {
  197. $err = '错误代码' . $files['error'];
  198. }
  199. }
  200. if (isset($err)) {
  201. return $err;
  202. } else {
  203. return $array_save_file;
  204. }
  205. }
  206. // 处理并移动上传文件
  207. function handle_upload($file, $temp, $array_ext_allow, $max_width, $max_height, $watermark)
  208. {
  209. // 定义主存储路径
  210. $save_path = DOC_PATH . STATIC_DIR . '/upload';
  211. $file = explode('.', $file); // 分离文件名及扩展
  212. $file_ext = strtolower(end($file)); // 获取扩展
  213. if (! in_array($file_ext, $array_ext_allow)) {
  214. return $file_ext . '格式的文件不允许上传!';
  215. }
  216. $image = array(
  217. 'png',
  218. 'jpg',
  219. 'gif',
  220. 'bmp'
  221. );
  222. $file = array(
  223. 'ppt',
  224. 'pptx',
  225. 'xls',
  226. 'xlsx',
  227. 'doc',
  228. 'docx',
  229. 'pdf',
  230. 'txt'
  231. );
  232. if (in_array($file_ext, $image)) {
  233. $file_type = 'image';
  234. } elseif (in_array($file_ext, $file)) {
  235. $file_type = 'file';
  236. } else {
  237. $file_type = 'other';
  238. }
  239. // 检查文件存储路径
  240. if (! check_dir($save_path . '/' . $file_type . '/' . date('Ymd'), true)) {
  241. return '存储目录创建失败!';
  242. }
  243. $file_path = $save_path . '/' . $file_type . '/' . date('Ymd') . '/' . time() . mt_rand(100000, 999999) . '.' . $file_ext;
  244. if (! move_uploaded_file($temp, $file_path)) { // 从缓存中转存
  245. return '从缓存中转存失败!';
  246. }
  247. $save_file = str_replace(ROOT_PATH, '', $file_path); // 获取文件站点路径
  248. // 如果是图片
  249. if (is_image($file_path)) {
  250. // 进行等比例缩放
  251. if (($reset = resize_img($file_path, $file_path, $max_width, $max_height)) !== true) {
  252. return $reset;
  253. }
  254. // 图片打水印
  255. if ($watermark) {
  256. watermark_img($file_path);
  257. }
  258. }
  259. return $save_file;
  260. }
  261. /**
  262. * *
  263. * 等比缩放图片
  264. *
  265. * @param string $src_image源图片路径
  266. * @param string $out_image输出图像路径
  267. * @param number $max_width最大宽
  268. * @param number $max_height最大高
  269. * @param number $img_quality图片质量
  270. * @return boolean 返回是否成功
  271. */
  272. function resize_img($src_image, $out_image = null, $max_width = null, $max_height = null, $img_quality = 90)
  273. {
  274. // 输出地址
  275. if (! $out_image)
  276. $out_image = $src_image;
  277. // 读取配置文件设置
  278. if (! $max_width)
  279. $max_width = Config::get('upload.max_width') ?: 999999999;
  280. if (! $max_height)
  281. $max_height = Config::get('upload.max_height') ?: 999999999;
  282. // 获取图片属性
  283. list ($width, $height, $type, $attr) = getimagesize($src_image);
  284. // 检查输出目录
  285. check_dir(dirname($out_image), true);
  286. // 无需缩放的图片
  287. if ($width <= $max_width && $height <= $max_height) {
  288. if ($src_image != $out_image) { // 存储地址不一致时进行拷贝
  289. if (! copy($src_image, $out_image)) {
  290. return '缩放图片时拷贝到目的地址失败!';
  291. }
  292. }
  293. return true;
  294. }
  295. // 求缩放比例
  296. if ($max_width && $max_height) {
  297. $scale = min($max_width / $width, $max_height / $height);
  298. } elseif ($max_width) {
  299. $scale = $max_width / $width;
  300. } elseif ($max_height) {
  301. $scale = $max_height / $height;
  302. }
  303. if ($scale < 1) {
  304. switch ($type) {
  305. case 1:
  306. $img = imagecreatefromgif($src_image);
  307. break;
  308. case 2:
  309. $img = imagecreatefromjpeg($src_image);
  310. break;
  311. case 3:
  312. $img = imagecreatefrompng($src_image);
  313. break;
  314. }
  315. $new_width = floor($scale * $width);
  316. $new_height = floor($scale * $height);
  317. $new_img = imagecreatetruecolor($new_width, $new_height); // 创建画布
  318. // 创建透明画布,避免黑色
  319. if ($type == 1 || $type == 3) {
  320. $color = imagecolorallocate($new_img, 255, 255, 255);
  321. imagefill($new_img, 0, 0, $color);
  322. imagecolortransparent($new_img, $color);
  323. }
  324. imagecopyresized($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  325. switch ($type) {
  326. case 1:
  327. imagegif($new_img, $out_image, $img_quality);
  328. break;
  329. case 2:
  330. imagejpeg($new_img, $out_image, $img_quality);
  331. break;
  332. case 3:
  333. imagepng($new_img, $out_image, $img_quality / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9
  334. break;
  335. default:
  336. imagejpeg($new_img, $out_image, $img_quality);
  337. }
  338. imagedestroy($new_img);
  339. imagedestroy($img);
  340. }
  341. return true;
  342. }
  343. // 剪切图片
  344. function cut_img($src_image, $out_image = null, $new_width = null, $new_height = null, $img_quality = 90)
  345. {
  346. // 输出地址
  347. if (! $out_image)
  348. $out_image = $src_image;
  349. // 读取配置文件设置
  350. if (! $new_width && ! $new_height)
  351. return;
  352. // 获取图片属性
  353. list ($width, $height, $type, $attr) = getimagesize($src_image);
  354. switch ($type) {
  355. case 1:
  356. $img = imagecreatefromgif($src_image);
  357. break;
  358. case 2:
  359. $img = imagecreatefromjpeg($src_image);
  360. break;
  361. case 3:
  362. $img = imagecreatefrompng($src_image);
  363. break;
  364. }
  365. // 不限定是等比例缩放
  366. if (! $new_width) {
  367. $new_width = floor($width * ($new_height / $height));
  368. }
  369. if (! $new_height) {
  370. $new_height = floor($height * ($new_width / $width));
  371. }
  372. // 计算裁剪是变大缩小方式
  373. if ($width >= $new_width && $height >= $new_height) { // 长宽均满足
  374. $cut_width = $new_width;
  375. $cut_height = $new_height;
  376. } else { // 有一边不满足
  377. $scale1 = $width / $new_width;
  378. $scale2 = $height / $new_height;
  379. if ($scale1 < $scale2) { // 变化越多的一边取全值,其余一边等比例缩放
  380. $cut_width = $width;
  381. $cut_height = floor($height * ($width / $new_width));
  382. } else {
  383. $cut_width = floor($new_width * ($height / $new_height));
  384. $cut_height = $height;
  385. }
  386. }
  387. // 创建画布
  388. $new_img = imagecreatetruecolor($new_width, $new_height);
  389. // 创建透明画布,避免黑色
  390. if ($type == 1 || $type == 3) {
  391. $color = imagecolorallocate($new_img, 255, 255, 255);
  392. imagefill($new_img, 0, 0, $color);
  393. imagecolortransparent($new_img, $color);
  394. }
  395. imagecopyresized($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $cut_width, $cut_height);
  396. check_dir(dirname($out_image), true); // 检查输出目录
  397. switch ($type) {
  398. case 1:
  399. imagegif($new_img, $out_image, $img_quality);
  400. break;
  401. case 2:
  402. imagejpeg($new_img, $out_image, $img_quality);
  403. break;
  404. case 3:
  405. imagepng($new_img, $out_image, $img_quality / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9
  406. break;
  407. default:
  408. imagejpeg($new_img, $out_image, $img_quality);
  409. }
  410. imagedestroy($new_img);
  411. imagedestroy($img);
  412. return true;
  413. }
  414. // 图片水印
  415. function watermark_img($src_image, $out_image = null, $position = null, $watermark_image = null, $watermark_text = '', $watermark_text_size = null, $watermark_text_color = null)
  416. {
  417. cache_config(); // 避免直接调用时未加载水印配置问题
  418. if (! Config::get('watermark_open')) {
  419. return;
  420. }
  421. // 输出地址
  422. if (! $out_image)
  423. $out_image = $src_image;
  424. // 如果不存在文字及图片则直接返回
  425. if (! $watermark_text) {
  426. $watermark_text = Config::get('watermark_text') ?: 'PbootCMS';
  427. }
  428. $watermark_image = $watermark_image ?: Config::get('watermark_pic');
  429. if (! $watermark_text && ! $watermark_image) {
  430. return;
  431. }
  432. // 获取图片属性
  433. list ($width1, $height1, $type1, $attr1) = getimagesize($src_image);
  434. switch ($type1) {
  435. case 1:
  436. $img1 = imagecreatefromgif($src_image);
  437. break;
  438. case 2:
  439. $img1 = imagecreatefromjpeg($src_image);
  440. break;
  441. case 3:
  442. $img1 = imagecreatefrompng($src_image);
  443. break;
  444. }
  445. if ($watermark_image) {
  446. $watermark_image = ROOT_PATH . $watermark_image;
  447. // 获取水印图片
  448. list ($width2, $height2, $type2, $attr2) = getimagesize($watermark_image);
  449. switch ($type2) {
  450. case 1:
  451. $img2 = imagecreatefromgif($watermark_image);
  452. break;
  453. case 2:
  454. $img2 = imagecreatefromjpeg($watermark_image);
  455. break;
  456. case 3:
  457. $img2 = imagecreatefrompng($watermark_image);
  458. break;
  459. }
  460. } else {
  461. if (! $watermark_text_size) {
  462. $watermark_text_size = Config::get('watermark_text_size') ?: 16;
  463. }
  464. if (! $watermark_text_color) {
  465. $watermark_text_color = Config::get('watermark_text_color') ?: '100,100,100';
  466. }
  467. $colors = explode(',', $watermark_text_color);
  468. if (Config::get('watermark_text_font')) {
  469. $font = ROOT_PATH . Config::get('watermark_text_font');
  470. } else {
  471. return;
  472. }
  473. // 手动创建水印图像
  474. $fontsize = $watermark_text_size;
  475. $width2 = mb_strlen($watermark_text, 'UTF-8') * ($fontsize + 10) + 20;
  476. $height2 = $fontsize + 10;
  477. $img2 = imagecreatetruecolor($width2, $height2);
  478. $color = imagecolorallocate($img2, 255, 255, 255);
  479. imagefill($img2, 0, 0, $color);
  480. imagecolortransparent($img2, $color); // 创建透明图
  481. $textcolor = imagecolorallocate($img2, $colors[0], $colors[1], $colors[2]);
  482. imagettftext($img2, $fontsize, 0, 5, $fontsize + 5, $textcolor, $font, $watermark_text);
  483. }
  484. // 现对图片太大时,自动缩放水印
  485. if ($width1 < $width2 * 3 || $height1 < $height2) {
  486. $scale = min(($width1 / 3) / $width2, ($height1 / 2) / $height2); // 求缩放比例
  487. $new_width = floor($scale * $width2);
  488. $new_height = floor($scale * $height2);
  489. } else {
  490. $new_width = $width2;
  491. $new_height = $height2;
  492. }
  493. // 水印位置
  494. if (! $position) {
  495. $position = Config::get('watermark_position') ?: 4;
  496. }
  497. switch ($position) {
  498. case '1':
  499. $x = 15;
  500. $y = 15;
  501. break;
  502. case '2':
  503. $x = $width1 - $new_width - 15;
  504. $y = 20;
  505. break;
  506. case '3':
  507. $x = 20;
  508. $y = $height1 - $new_height - 15;
  509. break;
  510. case '5':
  511. $x = ($width1 - $new_width) / 2;
  512. $y = ($height1 - $new_height) / 2;
  513. break;
  514. default:
  515. $x = $width1 - $new_width - 15;
  516. $y = $height1 - $new_height - 15;
  517. break;
  518. }
  519. // 创建透明画布,避免黑色
  520. if ($type1 == 1 || $type1 == 3) {
  521. $out = imagecreatetruecolor($width1, $height1);
  522. $color = imagecolorallocate($out, 255, 255, 255);
  523. imagefill($out, 0, 0, $color);
  524. imagecolortransparent($out, $color);
  525. imagecopy($out, $img1, 0, 0, 0, 0, $width1, $height1);
  526. } else {
  527. $out = $img1;
  528. }
  529. // 打上水印
  530. imagecopyresized($out, $img2, $x, $y - 10, 0, 0, $new_width, $new_height, $width2, $height2);
  531. check_dir(dirname($out_image), true); // 检查输出目录
  532. // 输出图片
  533. switch ($type1) {
  534. case 1:
  535. imagegif($out, $out_image, 90);
  536. break;
  537. case 2:
  538. imagejpeg($out, $out_image, 90);
  539. break;
  540. case 3:
  541. imagepng($out, $out_image, 90 / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9
  542. break;
  543. default:
  544. imagejpeg($out, $out_image, 90);
  545. }
  546. imagedestroy($img1);
  547. imagedestroy($img2);
  548. return true;
  549. }