DebrisHelper.php 7.4 KB

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