DebrisHelper.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. use yii\helpers\HtmlPurifier;
  5. use yii\helpers\Json;
  6. use Zhuzhichao\IpLocationZh\Ip;
  7. /**
  8. * Class DebrisHelper
  9. * @package common\helpers
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class DebrisHelper
  13. {
  14. /**
  15. * @throws \yii\base\InvalidConfigException
  16. */
  17. public static function getUrl()
  18. {
  19. $url = explode('?', Yii::$app->request->getUrl())[0];
  20. $matching = '/' . Yii::$app->id . '/';
  21. if (substr($url, 0, strlen($matching)) == $matching) {
  22. $url = substr($url, strlen($matching), strlen($url));
  23. }
  24. if (substr($url, 0, 1) === '/') {
  25. $url = substr($url, 1, strlen($url));
  26. }
  27. return $url;
  28. }
  29. /**
  30. * @param $value
  31. * @return mixed
  32. */
  33. public static function htmlEncode($value)
  34. {
  35. try {
  36. if (empty($value)) {
  37. return $value;
  38. }
  39. if (!is_array($value)) {
  40. $value = Json::decode($value);
  41. }
  42. $array = [];
  43. foreach ($value as $key => &$item) {
  44. if (!is_array($item)) {
  45. $array[$key] = Html::encode($item);
  46. } else {
  47. $array[$key] = self::htmlEncode($item);
  48. }
  49. }
  50. return $array;
  51. } catch (\Exception $e) {
  52. return $value;
  53. }
  54. }
  55. /**
  56. * 获取分页跳转
  57. *
  58. * @return array
  59. */
  60. public static function getPageSkipUrl()
  61. {
  62. $defautlUrl = Yii::$app->request->getHostInfo() . Yii::$app->request->url;
  63. $urlArr = explode('?', $defautlUrl);
  64. $defautlUrl = $urlArr[0];
  65. $getQueryParam = urldecode($urlArr[1] ?? '');
  66. $getQueryParamArr = explode('&', $getQueryParam);
  67. // 查询字符串是否有page
  68. foreach ($getQueryParamArr as $key => $value) {
  69. if (StringHelper::strExists($value, 'page=')) {
  70. unset($getQueryParamArr[$key]);
  71. }
  72. if (StringHelper::strExists($value, 'per-page=')) {
  73. unset($getQueryParamArr[$key]);
  74. }
  75. }
  76. $connector = !empty($getQueryParamArr) ? '?' : '';
  77. $fullUrl = $defautlUrl . $connector;
  78. $pageConnector = '?';
  79. if (!empty($getQueryParamArr)) {
  80. $fullUrl .= implode('&', $getQueryParamArr);
  81. $pageConnector = '&';
  82. }
  83. return [HtmlPurifier::process($fullUrl), $pageConnector];
  84. }
  85. /**
  86. * @param $ip
  87. * @return string
  88. */
  89. public static function long2ip($ip)
  90. {
  91. try {
  92. if (!is_string($ip)) {
  93. return $ip;
  94. }
  95. return long2ip($ip);
  96. } catch (\Exception $e) {
  97. return $ip;
  98. }
  99. }
  100. /**
  101. * @param $ip
  102. * @return string
  103. */
  104. public static function analysisIp($ip, $long = false)
  105. {
  106. if (empty($ip)) {
  107. return false;
  108. }
  109. if ('127.0.0.1' == $ip) {
  110. return '本地';
  111. }
  112. if ($long === true) {
  113. $ip = self::long2ip($ip);
  114. if (((int)$ip) > 1000) {
  115. return '无法解析';
  116. }
  117. }
  118. $ipData = Ip::find($ip);
  119. $str = '';
  120. !empty($ipData[0]) && $str .= $ipData[0];
  121. !empty($ipData[1]) && $str .= ' · ' . $ipData[1];
  122. !empty($ipData[2]) && $str .= ' · ' . $ipData[2];
  123. return $str;
  124. }
  125. /**
  126. * 调用这个方法前面干了什么
  127. *
  128. * @param bool $reverse
  129. * @return array
  130. */
  131. public static function debug($reverse = false)
  132. {
  133. $debug = debug_backtrace();
  134. $data = [];
  135. foreach ($debug as $e) {
  136. $function = isset($e['function']) ? $e['function'] : 'null function';
  137. $class = isset($e['class']) ? $e['class'] : 'null class';
  138. $file = isset($e['file']) ? $e['file'] : 'null file';
  139. $line = isset($e['line']) ? $e['line'] : 'null';
  140. $data[] = $file . '(' . $line . '),' . $class . '::' . $function . '()';
  141. }
  142. return $reverse == true ? array_reverse($data) : $data;
  143. }
  144. /**
  145. * 根据两点间的经纬度计算距离
  146. *
  147. * @param $lat1
  148. * @param $lng1
  149. * @param $lat2
  150. * @param $lng2
  151. * @return float
  152. */
  153. public static function getDistance($lat1, $lng1, $lat2, $lng2)
  154. {
  155. if (empty($lat1) || empty($lng1) || empty($lat2) || empty($lng2)) {
  156. return 0;
  157. }
  158. $earthRadius = 6367000; // 地球的近似半径(米)
  159. $lat1 = ($lat1 * pi()) / 180;
  160. $lng1 = ($lng1 * pi()) / 180;
  161. $lat2 = ($lat2 * pi()) / 180;
  162. $lng2 = ($lng2 * pi()) / 180;
  163. $calcLongitude = $lng2 - $lng1;
  164. $calcLatitude = $lat2 - $lat1;
  165. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
  166. $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  167. $calculatedDistance = $earthRadius * $stepTwo;
  168. return round($calculatedDistance);
  169. }
  170. /**
  171. * 获取水印坐标
  172. *
  173. * @param $imgUrl
  174. * @param $watermarkImgUrl
  175. * @param $point
  176. * @return array|bool
  177. */
  178. public static function getWatermarkLocation($imgUrl, $watermarkImgUrl, $point)
  179. {
  180. if (empty($imgUrl) || empty($watermarkImgUrl)) {
  181. return false;
  182. }
  183. if (!file_exists($watermarkImgUrl) || !file_exists($imgUrl)) {
  184. return false;
  185. }
  186. $imgSize = getimagesize($imgUrl);
  187. $watermarkImgSize = getimagesize($watermarkImgUrl);
  188. if (empty($imgSize) || empty($watermarkImgSize)) {
  189. return false;
  190. }
  191. $imgWidth = $imgSize[0];
  192. $imgHeight = $imgSize[1];
  193. $imgMime = $imgSize['mime'];
  194. $watermarkImgWidth = $watermarkImgSize[0];
  195. $watermarkImgHeight = $watermarkImgSize[1];
  196. $watermarkImgMime = $watermarkImgSize['mime'];
  197. switch ($point) {
  198. case 1 : // 左上角
  199. $porintLeft = 20;
  200. $pointTop = 20;
  201. break;
  202. case 2 : // 上中部
  203. $porintLeft = floor(($imgWidth - $watermarkImgWidth) / 2);
  204. $pointTop = 20;
  205. break;
  206. case 3 : // 右上部
  207. $porintLeft = $imgWidth - $watermarkImgWidth - 20;
  208. $pointTop = 20;
  209. break;
  210. case 4 : // 左中部
  211. $porintLeft = 20;
  212. $pointTop = floor(($imgHeight - $watermarkImgHeight) / 2);
  213. break;
  214. case 5 : // 正中部
  215. $porintLeft = floor(($imgWidth - $watermarkImgWidth) / 2);
  216. $pointTop = floor(($imgHeight - $watermarkImgHeight) / 2);
  217. break;
  218. case 6 : // 右中部
  219. $porintLeft = $imgWidth - $watermarkImgWidth - 20;
  220. $pointTop = floor(($imgHeight - $watermarkImgHeight) / 2);
  221. break;
  222. case 7 : // 左下部
  223. $porintLeft = 20;
  224. $pointTop = $imgHeight - $watermarkImgHeight - 20;
  225. break;
  226. case 8 : // 中下部
  227. $porintLeft = floor(($imgWidth - $watermarkImgWidth) / 2);
  228. $pointTop = $imgHeight - $watermarkImgHeight - 20;
  229. break;
  230. case 9 : // 右下部
  231. $porintLeft = $imgWidth - $watermarkImgWidth - 20;
  232. $pointTop = $imgHeight - $watermarkImgHeight - 20;
  233. break;
  234. default :
  235. return [0, 0];
  236. break;
  237. }
  238. // 太小就不生成水印坐标
  239. if (($imgWidth - $porintLeft) < $watermarkImgWidth || ($imgHeight - $pointTop) < $watermarkImgHeight) {
  240. return false;
  241. }
  242. return [$porintLeft, $pointTop];
  243. }
  244. }
粤ICP备19079148号