Utils.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /* file:常用函数、工具类函数封装的集合
  3. Created by wanghong<1204772286@qq.com>
  4. Date: 2021-02-24 */
  5. namespace utils;
  6. class Utils{
  7. /*
  8. * 文件根据类型进行分类
  9. $ext -string 文件后缀
  10. return -int
  11. * */
  12. public static function getFileType($ext){
  13. $ext=strtolower($ext);
  14. $image=['jpg','jpeg','png','bmp','gif'];
  15. $radio=['mp3','wav','wmv','amr'];
  16. $video=['mp4','3gp','avi','m2v','mkv','mov'];
  17. $doc=['ppt','pptx','doc','docx','xls','xlsx','pdf','txt','md'];
  18. if(in_array($ext,$doc)){
  19. $fileType=1;
  20. }elseif(in_array($ext,$image)){
  21. $fileType=2;
  22. }elseif(in_array($ext,$radio)){
  23. $fileType=3;
  24. }elseif(in_array($ext,$video)){
  25. $fileType=4;
  26. }else{
  27. $fileType=9;
  28. }
  29. return $fileType;
  30. }
  31. /*
  32. 获取文件的大小
  33. */
  34. public static function get_file_size($size, $limit = 0)
  35. {
  36. $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  37. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  38. return round($size, 2) . $units[$i + $limit];
  39. }
  40. /*
  41. h5ip网址缩短
  42. $link -string
  43. return string
  44. */
  45. public static function h5ipUrl($link)
  46. {
  47. $url = "http://h5ip.cn/index/api?format=json&url=" . $link;
  48. $data = json_decode(curl_request($url), true);
  49. if ($data['code'] == 0) {
  50. return $data['short_url'];
  51. } else {
  52. return $link;
  53. }
  54. }
  55. /*
  56. linux系统探测
  57. */
  58. public static function sys_linux()
  59. {
  60. // CPU
  61. if (false === ($str = @file("/proc/cpuinfo"))) return false;
  62. $str = implode("", $str);
  63. @preg_match_all("/model\s+name\s{0,}\:+\s{0,}([\w\s\)\(\@.-]+)([\r\n]+)/s", $str, $model);
  64. @preg_match_all("/cpu\s+MHz\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $mhz);
  65. @preg_match_all("/cache\s+size\s{0,}\:+\s{0,}([\d\.]+\s{0,}[A-Z]+[\r\n]+)/", $str, $cache);
  66. @preg_match_all("/bogomips\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $bogomips);
  67. if (false !== is_array($model[1])) {
  68. $res['cpu']['num'] = sizeof($model[1]);
  69. for ($i = 0; $i < $res['cpu']['num']; $i++) {
  70. $res['cpu']['model'][] = $model[1][$i] . '&nbsp;(' . $mhz[1][$i] . ')';
  71. $res['cpu']['mhz'][] = $mhz[1][$i];
  72. $res['cpu']['cache'][] = $cache[1][$i];
  73. $res['cpu']['bogomips'][] = $bogomips[1][$i];
  74. }
  75. if ($res['cpu']['num'] == 1)
  76. $x1 = '';
  77. else
  78. $x1 = ' ×' . $res['cpu']['num'];
  79. $mhz[1][0] = ' | 频率:' . $mhz[1][0];
  80. $cache[1][0] = ' | 二级缓存:' . $cache[1][0];
  81. $bogomips[1][0] = ' | Bogomips:' . $bogomips[1][0];
  82. $res['cpu']['model'][] = $model[1][0] . $mhz[1][0] . $cache[1][0] . $bogomips[1][0] . $x1;
  83. if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("<br />", $res['cpu']['model']);
  84. if (false !== is_array($res['cpu']['mhz'])) $res['cpu']['mhz'] = implode("<br />", $res['cpu']['mhz']);
  85. if (false !== is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("<br />", $res['cpu']['cache']);
  86. if (false !== is_array($res['cpu']['bogomips'])) $res['cpu']['bogomips'] = implode("<br />", $res['cpu']['bogomips']);
  87. }
  88. // NETWORK
  89. // UPTIME
  90. if (false === ($str = @file("/proc/uptime"))) return false;
  91. $str = explode(" ", implode("", $str));
  92. $str = trim($str[0]);
  93. $min = $str / 60;
  94. $hours = $min / 60;
  95. $days = floor($hours / 24);
  96. $hours = floor($hours - ($days * 24));
  97. $min = floor($min - ($days * 60 * 24) - ($hours * 60));
  98. if ($days !== 0) $res['uptime'] = $days . "天";
  99. if ($hours !== 0) $res['uptime'] .= $hours . "小时";
  100. $res['uptime'] .= $min . "分钟";
  101. // MEMORY
  102. if (false === ($str = @file("/proc/meminfo"))) return false;
  103. $str = implode("", $str);
  104. preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);
  105. preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
  106. $res['memTotal'] = round($buf[1][0] / 1024, 2);
  107. $res['memFree'] = round($buf[2][0] / 1024, 2);
  108. $res['memBuffers'] = round($buffers[1][0] / 1024, 2);
  109. $res['memCached'] = round($buf[3][0] / 1024, 2);
  110. $res['memUsed'] = $res['memTotal'] - $res['memFree'];
  111. $res['memPercent'] = (floatval($res['memTotal']) != 0) ? round($res['memUsed'] / $res['memTotal'] * 100, 2) : 0;
  112. $res['memRealUsed'] = $res['memTotal'] - $res['memFree'] - $res['memCached'] - $res['memBuffers']; //真实内存使用
  113. $res['memRealFree'] = $res['memTotal'] - $res['memRealUsed']; //真实空闲
  114. $res['memRealPercent'] = (floatval($res['memTotal']) != 0) ? round($res['memRealUsed'] / $res['memTotal'] * 100, 2) : 0; //真实内存使用率
  115. $res['memCachedPercent'] = (floatval($res['memCached']) != 0) ? round($res['memCached'] / $res['memTotal'] * 100, 2) : 0; //Cached内存使用率
  116. $res['swapTotal'] = round($buf[4][0] / 1024, 2);
  117. $res['swapFree'] = round($buf[5][0] / 1024, 2);
  118. $res['swapUsed'] = round($res['swapTotal'] - $res['swapFree'], 2);
  119. $res['swapPercent'] = (floatval($res['swapTotal']) != 0) ? round($res['swapUsed'] / $res['swapTotal'] * 100, 2) : 0;
  120. // LOAD AVG
  121. if (false === ($str = @file("/proc/loadavg"))) return false;
  122. $str = explode(" ", implode("", $str));
  123. $str = array_chunk($str, 4);
  124. $res['loadAvg'] = implode(" ", $str[0]);
  125. return $res;
  126. }
  127. /**
  128. * 将数组按字母A-Z排序
  129. * @return [type] [description]
  130. */
  131. public static function chartSort($array, $field,$isGroup=true,$chart='chart')
  132. {
  133. $newArray = [];
  134. foreach ($array as $k => &$v) {
  135. $v[$chart] = self::getFirstChart($v[$field]);
  136. $newArray[] = $v;
  137. }
  138. $data = [];
  139. if($isGroup){
  140. foreach ($newArray as $k => $v) {
  141. if (array_key_exists($v[$chart], $data)) {
  142. $data[$v[$chart]][] = $v;
  143. } else {
  144. $data[$v[$chart]] = [];
  145. $data[$v[$chart]][] = $v;
  146. }
  147. }
  148. ksort($data);
  149. }else{
  150. return $newArray;
  151. }
  152. return $data;
  153. }
  154. /**
  155. * 返回取汉字的第一个字的首字母
  156. * @param [type] $str [string]
  157. * @return [type] [strind]
  158. */
  159. public static function getFirstChart($str)
  160. {
  161. $str = str_replace(' ', '', $str);
  162. if (empty($str)) {
  163. return '#';
  164. }
  165. $char = ord($str[0]);
  166. if ($char >= ord('A') && $char <= ord('z')) {
  167. return strtoupper($str[0]);
  168. }
  169. $s1 = iconv('UTF-8', 'gb2312//IGNORE', $str);
  170. $s2 = iconv('gb2312', 'UTF-8//IGNORE', $s1);
  171. $s = $s2 == $str ? $s1 : $str;
  172. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  173. if ($asc >= -20319 && $asc <= -20284) return 'A';
  174. if ($asc >= -20283 && $asc <= -19776) return 'B';
  175. if ($asc >= -19775 && $asc <= -19219) return 'C';
  176. if ($asc >= -19218 && $asc <= -18711) return 'D';
  177. if ($asc >= -18710 && $asc <= -18527) return 'E';
  178. if ($asc >= -18526 && $asc <= -18240) return 'F';
  179. if ($asc >= -18239 && $asc <= -17923) return 'G';
  180. if ($asc >= -17922 && $asc <= -17418) return 'H';
  181. if ($asc >= -17417 && $asc <= -16475) return 'J';
  182. if ($asc >= -16474 && $asc <= -16213) return 'K';
  183. if ($asc >= -16212 && $asc <= -15641) return 'L';
  184. if ($asc >= -15640 && $asc <= -15166) return 'M';
  185. if ($asc >= -15165 && $asc <= -14923) return 'N';
  186. if ($asc >= -14922 && $asc <= -14915) return 'O';
  187. if ($asc >= -14914 && $asc <= -14631) return 'P';
  188. if ($asc >= -14630 && $asc <= -14150) return 'Q';
  189. if ($asc >= -14149 && $asc <= -14091) return 'R';
  190. if ($asc >= -14090 && $asc <= -13319) return 'S';
  191. if ($asc >= -13318 && $asc <= -12839) return 'T';
  192. if ($asc >= -12838 && $asc <= -12557) return 'W';
  193. if ($asc >= -12556 && $asc <= -11848) return 'X';
  194. if ($asc >= -11847 && $asc <= -11056) return 'Y';
  195. if ($asc >= -11055 && $asc <= -10247) return 'Z';
  196. return "#";
  197. }
  198. /**
  199. * 人民币转大写
  200. * @param
  201. */
  202. function cny($ns)
  203. {
  204. static $cnums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"),
  205. $cnyunits = array("圆", "角", "分"),
  206. $grees = array("拾", "佰", "仟", "万", "拾", "佰", "仟", "亿");
  207. list($ns1, $ns2) = explode(".", $ns, 2);
  208. $ns2 = array_filter(array($ns2[1], $ns2[0]));
  209. $ret = array_merge($ns2, array(implode("", _cny_map_unit(str_split($ns1), $grees)), ""));
  210. $ret = implode("", array_reverse(_cny_map_unit($ret, $cnyunits)));
  211. return str_replace(array_keys($cnums), $cnums, $ret);
  212. }
  213. function _cny_map_unit($list, $units)
  214. {
  215. $ul = count($units);
  216. $xs = array();
  217. foreach (array_reverse($list) as $x) {
  218. $l = count($xs);
  219. if ($x != "0" || !($l % 4)) {
  220. $n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]);
  221. } else {
  222. $n = is_numeric($xs[0][0]) ? $x : '';
  223. }
  224. array_unshift($xs, $n);
  225. }
  226. return $xs;
  227. }
  228. /**
  229. * 解析获取php.ini 的upload_max_filesize(单位:byte)
  230. * @param $dec int 小数位数
  231. * @return float (单位:byte)
  232. * */
  233. public static function get_upload_max_filesize_byte($dec = 2)
  234. {
  235. $max_size = ini_get('upload_max_filesize');
  236. preg_match('/(^[0-9\.]+)(\w+)/', $max_size, $info);
  237. $size = $info[1];
  238. $suffix = strtoupper($info[2]);
  239. $a = array_flip(array("B", "KB", "MB", "GB", "TB", "PB"));
  240. $b = array_flip(array("B", "K", "M", "G", "T", "P"));
  241. $pos = $a[$suffix] && $a[$suffix] !== 0 ? $a[$suffix] : $b[$suffix];
  242. return round($size * pow(1024, $pos), $dec);
  243. }
  244. /**
  245. * 十六进制 转 RGB
  246. */
  247. public static function hex2rgb($hexColor)
  248. {
  249. $color = str_replace('#', '', $hexColor);
  250. if (strlen($color) > 3) {
  251. $rgb = array(
  252. 'r' => hexdec(substr($color, 0, 2)),
  253. 'g' => hexdec(substr($color, 2, 2)),
  254. 'b' => hexdec(substr($color, 4, 2))
  255. );
  256. } else {
  257. $color = $hexColor;
  258. $r = substr($color, 0, 1) . substr($color, 0, 1);
  259. $g = substr($color, 1, 1) . substr($color, 1, 1);
  260. $b = substr($color, 2, 1) . substr($color, 2, 1);
  261. $rgb = array(
  262. 'r' => hexdec($r),
  263. 'g' => hexdec($g),
  264. 'b' => hexdec($b)
  265. );
  266. }
  267. return $rgb;
  268. }
  269. /**
  270. * RGB转 十六进制
  271. * @param $rgb RGB颜色的字符串 如:rgb(255,255,255);
  272. * @return string 十六进制颜色值 如:#FFFFFF
  273. */
  274. public static function RGBToHex($rgb){
  275. $regexp = "/^rgb\(([0-9]{0,3})\,\s*([0-9]{0,3})\,\s*([0-9]{0,3})\)/";
  276. $re = preg_match($regexp, $rgb, $match);
  277. $re = array_shift($match);
  278. $hexColor = "#";
  279. $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
  280. for ($i = 0; $i < 3; $i++) {
  281. $r = null;
  282. $c = $match[$i];
  283. $hexAr = array();
  284. while ($c > 16) {
  285. $r = $c % 16;
  286. $c = ($c / 16) >> 0;
  287. array_push($hexAr, $hex[$r]);
  288. }
  289. array_push($hexAr, $hex[$c]);
  290. $ret = array_reverse($hexAr);
  291. $item = implode('', $ret);
  292. $item = str_pad($item, 2, '0', STR_PAD_LEFT);
  293. $hexColor .= $item;
  294. }
  295. return $hexColor;
  296. }
  297. }