file.php 18 KB

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