thefunpower / helper
helper
Requires
- iio/libmergepdf: ^3.1
- imangazaliev/didom: ^2.0
- league/csv: ^9.0
- mpdf/mpdf: ^8.1
- nicolab/php-ftp-client: ^2.0
- overtrue/socialite: ^4.9
- phpoffice/phpspreadsheet: *
- picqer/php-barcode-generator: ^2
- predis/predis: ^2.1
- pusher/pusher-php-server: ^7.2
- scssphp/scssphp: ^1
- spatie/array-to-xml: *
- symfony/lock: ^6
- thefunpower/rpc_php: ^2
- dev-main
- v2.2.91
- v2.2.90
- v2.2.89
- v2.2.88
- v2.2.87
- v2.2.86
- v2.2.85
- v2.2.84
- v2.2.83
- v2.2.82
- v2.2.81
- v2.2.80
- v2.2.79
- v2.2.78
- v2.2.77
- v2.2.76
- v2.2.75
- v2.2.73
- v2.2.72
- v2.2.71
- v2.2.70
- v2.2.69
- v2.2.68
- v2.2.67
- v2.2.66
- v2.2.65
- v2.2.64
- v2.2.63
- v2.2.62
- v2.2.61
- v2.2.60
- v2.2.59
- v2.2.58
- v2.2.57
- v2.2.56
- v2.2.54
- v2.2.53
- v2.2.52
- v2.2.51
- v2.2.50
- v2.2.47
- v2.2.46
- v2.2.45
- v2.2.44
- v2.2.43
- v2.2.42
- v2.2.41
- v2.2.40
- v2.2.39
- v2.2.38
- v2.2.37
- v2.2.36
- v2.2.35
- v2.2.34
- v2.2.33
- v2.2.32
- v2.2.31
- v2.2.30
- v2.2.29
- v2.2.28
- v2.2.27
- v2.2.26
- v2.2.25
- v2.2.24
- v2.2.23
- v2.2.22
- v2.2.21
- v2.2.20
- v2.2.19
- v2.2.18
- v2.2.17
- v2.2.16
- v2.2.15
- v2.2.14
- v2.2.13
- v2.2.12
- v2.2.11
- v2.2.10
- v2.2.9
- v2.2.8
- v2.2.7
- v2.2.6
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
This package is auto-updated.
Last update: 2024-11-03 08:48:37 UTC
README
在composer.json中添加
"thefunpower/helper": "dev-main"
助手工具类或函数
需要定义PATH
目录,项目的根目录
define("PATH",__DIR__.'/');
需要定义WWW_PATH
目录,网站访问的目录,有时PATH与WWW_PATH是一样的
define("WWW_PATH",__DIR__.'/');
确保有data
uploads
两个目录且可写.
data在根目录 uploads在网站访问的目录
Predis Publish Subscribe
连接
predis($host,$port,$auth);
发布消息
redis_pub("demo","welcome man");
redis_pub("demo",['title'=>'yourname']);
取订阅消息
redis_sub("demo",function($channel,$message){
echo "channel ".$channel."\n";
print_r($message);
});
Predis GEO
连接
predis($host,$port,$auth);
获取
$s = predis_geo_pos('places',[
'上海外滩','北京天安门'
]);
pr($s) ;
添加
predis_add_geo('places',[
[
'lat'=>'116.397128',
'lng'=>'39.916527',
'title'=>'北京天安门'
],
[
'lat'=>'121.473701',
'lng'=>'31.230416',
'title'=>'上海外滩'
],
[
'lat'=>'121.45668',
'lng'=>'31.21706',
'title'=>'襄阳公园'
],
]);
附近分页
pr(predis_get_pager('places', 121.45668, 31.21706));
RPC
服务端
class ServerGetUser{
public function getInfo($name = 'abc'){
return ['welcome'=>$name,'token'=>rpc_token()];
}
}
rpc_server("ServerGetUser");
客户端
$client = rpc_client("http://127.0.0.1:5000/rpc.php");
$info = $client->getInfo("test");
print_r($info);
Ftp
php.ini中开启ftp
扩展
把本地文件同步到FTP上。
如果FTP上目录文件已存在,将会被替换。
use helper_v3\Ftp;
$ftp = Ftp::start([
'host' =>'IP地址',
'user' =>'帐号',
'pwd' =>'密码',
'port' =>'端口,默认21',
]);
//上传到根目录
Ftp::put_all(__DIR__.'/uploads');
//或上传到指定目录
//Ftp::put_all(__DIR__.'/uploads','uploads');
Ftp::end();
更多方法 https://github.com/Nicolab/php-ftp-client
PDF字体
免费字体
阿里妈妈方圆体 alifanyuan
阿里妈妈数黑体 alishuhei
阿里巴巴普惠体 puhuiti
阿里巴巴普惠体细 puhuitithin
google字体 notosanssc
默认使用 notosanssc。
helper_v3\Pdf::init([
'fontDir'=>[''],
'fontdata'=>[
'simhei'=> [
'R' => 'simhei.ttf',
'I' => 'simhei.ttf',
],
],
'default_font'=>'simhei'
]);
安装依赖
yum install pdftk pdftk-java poppler-utils perl-Image-ExifTool.noarch ImageMagick ImageMagick-devel ghostscript -y
生成PDF
https://mpdf.github.io/installation-setup/installation-v7-x.html
use helper_v3\Pdf;
$mpdf = Pdf::init();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();
合并PDF
$input = [
PATH.'uploads/1.pdf',
PATH.'uploads/2.pdf',
];
$new_file = '/完整路径/1.pdf';
echo Pdf::merger($input,$new_name);
exit;
合并PDF,包含图片
Pdf::merger_with_image($files, $output);
PDF提取图片
Pdf::pdf_to_image($file,$saveToDir)
取PDF信息
Pdf::get_info($file);
返回
Array
(
[header] => Array
(
[ModDate] => D
[Creator] => Microsoft® PowerPoint® 2019
[CreationDate] => D
[Producer] => Microsoft® PowerPoint® 2019
[Author] => Microsoft Office User
[Title] => PowerPoint 演示文稿
)
文档长宽
[dimensions] => Array
(
[0] => 960
[1] => 540
)
2是横版,1是竖版
[dimensions_type] => 2
)
取PDF页数
Pdf::get_pages($file);
设置PDF信息
Pdf::set_info($file,$output,$arr = []);
其中arr
支持title
author
keywords
生成PDF table
$html = '
<style>
table{
width: 100%;
text-align:left;
margin: 0 auto;
border: 1px solid #000000;
border-collapse: collapse;
}
th,td {
border: 1px solid #000000;
text-align: center;
}
</style>
<table cellspacing="0" cellpadding="0" border="0" >
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>';
$mpdf = Pdf::init();
$mpdf->shrink_tables_to_fit = 1;
$mpdf->WriteHTML($html);
$mpdf->Output();
HTML转PDF
安装依赖
yum install xorg-x11-server-Xvfb wkhtmltopdf fontconfig freetype wqy-zenhei-fonts wqy-microhei-fonts
PHP中调用
html_to_pdf($input_html_file,$output_pdf_file,$return_cmd = false,$exec = false)
如遇条形码可用 php-barcode-generator
composer require picqer/php-barcode-generator
Xls
composer require phpoffice/phpspreadsheet
当前使用 "phpoffice/phpspreadsheet": "^1.20"
生成xls
use helper_v3\Xls;
$all = db_get("catalog_product",'*');
foreach($all as $v){
$title = $v['title'];
$desc = $v['desc'];
$values[] = [
'title'=>$title,
'desc'=>$desc,
];
}
Xls::create([
'title'=>'编号',
'desc'=>'规格',
], $values, 'product', FALSE);
第一个worksheet
Xls::$label = $txt_month.'专票';
Xls::$sheet_width = [
'A' => "15",
'B' => "36",
'C' => "30",
'D' => "10",
'E' => "10",
'F' => "10",
];
更多worksheet
Xls::$works = [
[
'title' => $title,
'label' => $txt_month.'普票',
'data' => $new_data,
'width' => Xls::$sheet_width,
]
];
合并
Xls::$merge = [
'A18:E22'
];
Xls::create($title, $values, $name, FALSE);
消息订阅
依赖
yarn add ioredis
yarn add ws
1.生成server.js
echo create_node_ws_server($ws_port=3006,$topic=['demo'],$redis_host='127.0.0.1',$port='6379',$auth='');
复制代码至server.js
中
启动server
node server.js
2.HTML添加监听
依赖 reconnecting-websocket.js
https://github.com/joewalnes/reconnecting-websocket
<script>
<?php
$func = "
data = JSON.parse(data);
console.log(data);
";
echo get_ws_js($func,'ws://127.0.0.1:3006');
?>
</script>
其中ws://127.0.0.1:3006
如果是 wss 则wss://yourdomain/wss
3.php发送消息
redis_pub("demo",['title'=>'yourname']);
如使用wss则需配置Nginx转发
location /wss {
proxy_pass http://127.0.0.1:3006;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
rewrite /wss/(.*) /$1 break;
proxy_redirect off;
}
测试
redis_sub("demo",function($channel,$message){
echo "channel ".$channel."\n";
print_r($message);
});
pusher
PUSHER_APP_KEY =
PUSHER_APP_SECRET =
PUSHER_APP_ID =
PUSHER_APP_CLUSTER =
前端需要加载JS
<script src="https://js.pusher.com/8.0.1/pusher.min.js"></script>
<script type="text/javascript">
var pusher = new Pusher("<?=get_config("PUSHER_APP_KEY")?>", {
cluster: "<?=get_config("PUSHER_APP_CLUSTER")?>",
});
var channel = pusher.subscribe("netteadmin");
channel.bind("notice", (data) => {
console.log(data);
});
</script>
发送消息
helper_v3\Pusher::sender($channel,$event,$data = []);
或使用
send_pusher($data = [],$channel='netteadmin',$event='notice');
xcookie 加密
//设置
xcookie("ss",1);
xcookie("ss",['title'=>'tt']);
//读取
pr(xcookie("ss"));
//删除
xcookie_delete("ss");
redis锁
global $redis_lock;
//锁前缀
global $lock_key;
$redis_lock = [
'host'=>'',
'port'=>'',
'auth'=>'',
];
lock_call('k',function(){
},second);
gz压缩数据
$s = gz_encode(['a'=>"test"]);
echo $s;
echo "解压后<br>";
print_r(gz_decode($s));
SCSS
scss链接
<link rel="stylesheet" href="<?=scss("app.scss",true)?>" />
也可以直接调用
<style>
<?php
echo scss("
\$color: #abc;
div { color: lighten(\$color, 20%); }
");
<?php }?>
</style>
scss文件语法,参考 http://www.uinio.com/Web/Scss/
$color: red;
.navigation {
ul {
line-height: 20px;
color: blue;
a {
color: $color;
}
}
}
.footer {
.copyright {
color: silver;
}
}
贝塞尔
$blob = line_bezier([
'front_border'=>'#000',
'fill_color'=>'red',
'background_color '=>'#fff',
'stroke_opacity'=>1,
'stroke_width'=>1,
'data'=>[
[
['x' => 10.0 * 5, 'y' => 10.0 * 5],
['x' => 30.0 * 5, 'y' => 90.0 * 5],
['x' => 25.0 * 5, 'y' => 10.0 * 5],
['x' => 50.0 * 5, 'y' => 50.0 * 5],
],
[
['x' => 50.0 * 5, 'y' => 50.0 * 5],
['x' => 75.0 * 5, 'y' => 90.0 * 5],
['x' => 70.0 * 5, 'y' => 10.0 * 5],
['x' => 90.0 * 5, 'y' => 40.0 * 5],
],
'translate'=>[0,200],
[
['x' => 10 * 5, 'y' => 10 * 5],
['x' => 30 * 5, 'y' => 90 * 5],
['x' => 25 * 5, 'y' => 10 * 5],
['x' => 50 * 5, 'y' => 50 * 5],
],
[
['x' => 50 * 5, 'y' => 50 * 5],
['x' => 80 * 5, 'y' => 50 * 5],
['x' => 70 * 5, 'y' => 10 * 5],
['x' => 90 * 5, 'y' => 40 * 5],
],
]
],'base64');
//base64时
echo "<img src='data:image/png;base64,".$blob."' />";exit;
//blob时
header("Content-Type: image/png");
echo $blob;exit;
获取本地音视频时长
需手动安装getid3
composer require james-heinrich/getid3
使用
get_video_time($video_local_path)
BLOCK
1.写CSS文件
<?php helper_v3\Block::start('css');?>
#app{
padding-left: 0px !important;
padding-right: 0px !important;
}
<?php helper_v3\Block::end();?>
2.输出
<?php
helper_v3\Block::output();
?>
天地图
tianditu.gov.cn
set_config("tianditu","服务端key");
pr(helper_v3\Map::get_lat('上海市襄阳公园'));
pr(helper_v3\Map::get_address(31.218970,121.452340));
输出
Array
(
[lat] => 31.218970
[lng] => 121.452340
)
Array
(
[address] => 上海市徐汇区湖南路街道淮海中路1008号
[parse] => Array
(
[nation] => 中国
[province] => 上海市
[county] => 徐汇区
[address] => 淮海中路1008号
)
)