PHP常用自定义方法
# 获取远程图片内容
/**
* @param $url
* @return mixed|string
*/
function getUrlImgString($url){
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($code == 200){
return $data;
}
return '';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 写入日志文件
/**
* 写入日志文件
* @param string|array $values 写入内容
* @param string $dir 写入路径
* @return bool|int
*/
function write_log($values, $dir)
{
if (is_array($values))
$values = print_r($values, true);
// 日志内容
$content = '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . $values . PHP_EOL . PHP_EOL;
try {
// 文件路径
$filePath = $dir . '/logs/';
// 路径不存在则创建
!is_dir($filePath) && mkdir($filePath, 0755, true);
// 写入文件
return file_put_contents($filePath . date('Ymd') . '.log', $content, FILE_APPEND);
} catch (\Exception $e) {
return false;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 根据两个经纬度计算两点之间的距离
/**
* 求两个已知经纬度之间的距离,单位为千米
* @param lng1,lng2 经度
* @param lat1,lat2 纬度
* @return float 距离,单位千米
**/
private function _distance($lng1, $lat1, $lng2, $lat2)
{
// 将角度转为弧度
$radLat1 = deg2rad($lat1);
$radLat2 = deg2rad($lat2);
$radLng1 = deg2rad($lng1);
$radLng2 = deg2rad($lng2);
$a = $radLat1 - $radLat2; // 两纬度之差,纬度<90
$b = $radLng1 - $radLng2; // 两经度之差纬度<180
$s = 2 * asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2))) * 6378.137;
return $s;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 异步执行cmd控制台命令
$cmd = "php ".ROOT_PATH."think 命令";
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
} else {
exec($cmd . " > /dev/null &");
}
1
2
3
4
5
6
2
3
4
5
6
# 将一个字节码字符串转换成浮点数
$src = '000090c0';
$arr = array_map('hexdec', str_split($src, 2));
$float = unpack('f', pack("C*", ...$arr));
var_dump($float[1]);
1
2
3
4
2
3
4
# 返回当前的毫秒时间戳
//返回当前的毫秒时间戳
function msectime() {
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 将图片翻转90°后保存
$url = $attachment->url; // 本地图片地址
$rotate = 90; // 逆时针旋转90°
$image = imagecreatefromstring(file_get_contents($url));
$rotate_image = imagerotate($image, $rotate, 0);
// 有的右边会出现黑线,直接才减掉
$cropped_image = imagecrop($rotate_image, ['x'=>0,'y'=>0, 'width' => $attachment->imageheight - 1,'height' => $attachment->imagewidth]);
imagejpeg($cropped_image, $url);
1
2
3
4
5
6
7
2
3
4
5
6
7
上次更新: 2023/09/22, 16:54:32