cxgj/components/Utils.php
2023-11-27 09:45:13 +08:00

264 lines
8.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @author Any
* @description KISS
* @date 2020-11-1
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\components;
use Yii;
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
class Utils
{
/**
* 内容过滤防止xss的问题
* @param string $content 内容
*/
public static function htmlTagFilter($content)
{
$content = trim($content);
return HtmlPurifier::process($content);
}
/**
* 变量形式转换
* 下划线转驼峰
* 注:变量需要小写
*/
public static function underline2hump($str){
$str = preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){
return strtoupper($matches[2]);
},$str);
return $str;
}
/**
* 变量形式转换
* 驼峰转为下划线
*/
public static function hump2underline($str){
$str = preg_replace_callback('/([A-Z]{1})/',function($matches){
return '_'.strtolower($matches[0]);
},$str);
return $str;
}
/**
* 字符串脱敏
* @param string $content 需要脱敏字符串
* @param int $start 脱敏开始位置
* @param int $len 长度,如果为负值,从右向左偏移位
* @param string $char 脱敏替换字符
*/
public static function stringDesensitization($content,$start = 0, $len = 1, $char = '*')
{
$str_len = mb_strlen($content,'utf-8');
if($len < 0){
$end = $str_len + $len -1;
} else {
$end = $start + $len - 1;
}
$end = $end > $str_len ? $str_len : $end;
$str = '';
if($str_len <= 2){
$str = mb_substr($content, 0,1,'utf-8') . $char . mb_substr($content, 1, 1,'utf-8');
} else {
for($i = 0; $i < $str_len; $i++){
if($i >= $start && $i <= $end){
$str .= $char;
} else {
$str .= mb_substr($content, $i, 1,'utf-8');
}
}
}
return $str;
}
/**
* ip地址判断
* @param String $gonten 需要判断的字符串
* @return ture or false
*/
public static function isIp($gonten){
$ip=explode(".",$gonten);
for($i=0;$i<count($ip);$i++)
{
if($ip[$i]>255){
return false;
}
}
return ereg("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$",$gonten);
}
//字符串格式化
public static function stringFormat($content,$args)
{
$content = trim($content);
if(empty($content) || !is_array($args)){
return false;
}
preg_match_all('/{{}}/',$content,$matches);
$replace = '{{}}';
for ($i = 0; $i < count($matches[0]); $i++){
$pos = strpos($content, $replace);
if($pos !== false && array_key_exists($i, $args)){
$len = strlen($replace);
$content = substr_replace($content, $args[$i],$pos, $len);
}
}
return $content;
}
/**
* 距离格式化
* @param number $distance 距离
* @param boolean $km 是否为千米,默认为米
* return string
*/
public static function distanceFormat($distance, $km = false)
{
$distance = $km ? $distance * 1000 : $distance;
if ($distance > 1000) {
$distance = round($distance / 1000, 2) . 'km';
} else {
$distance .= 'm';
}
return $distance;
}
/**
* 根据起点坐标和终点坐标测距离
* @param [array] $from [起点坐标(经纬度),例如:array(118.012951,36.810024)]
* @param [array] $to [终点坐标(经纬度)]
* @param [bool] $km 是否以公里为单位 false:米 true:公里(千米)
* @param [int] $decimal 精度 保留小数位数
* @return [string] 距离数值
*/
public static function getDistance($from, $to, $km = true, $decimal = 2)
{
sort($from);
sort($to);
$EARTH_RADIUS = 6370.996; // 地球半径系数
$distance = $EARTH_RADIUS * 2 * asin(sqrt(pow(sin(($from[0] * pi() / 180 - $to[0] * pi() / 180) / 2), 2) + cos($from[0] * pi() / 180) * cos($to[0] * pi() / 180) * pow(sin(($from[1] * pi() / 180 - $to[1] * pi() / 180) / 2), 2))) * 1000;
if ($km) {
$distance = $distance / 1000;
}
return round($distance, $decimal);
}
/**
* 版本比较(主版本号.次版本号.修订版本号)
* @param string $version1 待比较版本号
* @param string $version2 待比较版本号
* return 0|1|-11=大于0=等于,-1=小于
*/
public static function compareVersion($version1, $version2)
{
$version1_arr = explode('.', $version1);
$version2_arr = explode('.', $version2);
$len = max([count($version1_arr), count($version2_arr)]);
while(count($version1_arr) < $len){
array_push($version1_arr,0);
}
while(count($version2_arr) < $len){
array_push($version2_arr,0);
}
for($i = 0 ; $i < $len; $i++){
$num1 = intval($version1_arr[$i]);
$num2 = intval($version2_arr[$i]);
if($num1 > $num2){
return 1;
} else if($num1 < $num2){
return -1;
}
}
return 0;
}
/**
* 获取视频信息
* @param string $file 视频文件路径
* @param string $ffmpeg_path ffmpeg路径
* return array
*/
public static function getVideoInfo($file, $ffmpeg_path = "/usr/local/bin/ffmpeg")
{
$wwwroot = Yii::getAlias('@webroot');
$cmd = "cd {$wwwroot} && {$ffmpeg_path} -i '{$file}' 2>&1";
ob_start();
passthru($cmd);
$info = ob_get_contents();
ob_end_clean();
$data = [];
if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
$data['duration'] = $match[1]; //播放时间
$arr_duration = explode(':', $match[1]);
$data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
$data['start'] = $match[2]; //开始时间
$data['bitrate'] = $match[3]; //码率(kb)
}
if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {
$data['vcodec'] = $match[1]; //视频编码格式
$data['vformat'] = $match[2]; //视频格式
$data['resolution'] = $match[3]; //视频分辨率
$arr_resolution = explode('x', $match[3]);
$data['width'] = $arr_resolution[0];
$data['height'] = $arr_resolution[1];
}
if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
$data['acodec'] = $match[1]; //音频编码
$data['asamplerate'] = $match[2]; //音频采样频率
}
if (isset($data['seconds']) && isset($data['start'])) {
$data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间
}
$data['size'] = filesize($file); //文件大小
return $data;
}
/**
* 提取视频封面
* @param string $file 视频文件路径
* @param string $output 提取图片保存路径
* @param string $ffmpeg_path ffmpeg路径
* return string|null
*/
public static function getVideoCover($file, $output, $time_offset = '00:00:1.18', $size = null, $ffmpeg_path = "/usr/local/bin/ffmpeg")
{
if(file_exists($output)){
return $output;
}
$wwwroot = Yii::getAlias('@webroot');
if($size == null){
$cmd = "cd {$wwwroot} && {$ffmpeg_path} -ss {$time_offset} -i '{$file}' -vframes 1 -f image2 -y {$output}";
} else {
$cmd = "cd {$wwwroot} && {$ffmpeg_path} -ss {$time_offset} -i '{$file}' -vframes 1 -s {$size} -f image2 -y {$output}";
}
ob_start();
passthru($cmd);
$info = ob_get_contents();
ob_end_clean();
if(file_exists($output)){
return $output;
}
return null;
}
/**
* 目录创建(递归创建)
*/
public static function mkDir($dir, $mode = 0777)
{
return is_dir($dir) or self::mkDir(dirname($dir), $mode) and mkdir($dir, $mode);
}
}