PHP code example of zxf / tools
1. Go to this page and download the library: Download zxf/tools library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zxf / tools example snippets
use zxf\WeChat\xxx;
use zxf\Tools\Cache;
$cache = Cache::instance([
'cache_dir' => "./cache", // 缓存地址
'type' => 'random', // 缓存方式 key: 直接使用key存储,random:对key加密存储
'mode' => '1', //缓存模式 1:serialize ;2:保存为可执行php文件
]);
//设置存放缓存文件夹路径
$cache->setCacheDir('./cache');
//设置缓存模式
$cache->setMode(1);
//模式1 缓存存储方式
//a:2:{s:11:"expiry_time";i:6475431070;s:4:"data";a:1:{s:3:"key";s:5:"value";}}
//模式2 缓存存储方式
//
// return array(
// 'data'=>'xxx'
//);
// 获取缓存
$cache->get('name');
// 设置缓存
$cache->set('name', '张三');
// 设置缓存有效期,第三个参数为int 类型表示缓存多少秒,为string 类型时候的缓存时间为 strtotime 函数支持的字符串,例如:"+1 day"
$cache->set('status', '1',55); // 缓存55秒
$cache->set('status', '1','+5 hours'); // 缓存5小时
// 清空所有缓存
$cache->flush();
// 删除一条缓存
$cache->delete('name');
// 判断某条缓存是否存在
$cache->has('name');
use zxf\BarCode\BarCodeGenerate;
$barcode = new BarCodeGenerate();
$image = $barcode
->width((int)$bar_width) // 条码宽度,单位为像素
->height((int)$thickness) // 条码高度,单位为像素
->padding(8) // 条码安全区,单位为像素
->text($label, (int)$fontSize) // 设置底部文本
->content($text,$textSize??10,$fontPath??'') // 设置条码内容
->format($codeType) // 设置条码格式
;
// 直接输出到浏览器
$barcode->toBrowser();
// 保存到文件
$filePath = $barcode->toFile('/your/path/barcode.png');
// 返回图片资源
$barcode->toImg();
// 返回base64图片资源
$barcode->toBase64();
php
\zxf\Http\Curl::instance()->setParams(['path'=>'pages/index/index'])->post($url,'json');
php
use zxf\Login\QqOauth;
/**
* QQ 互联登录
*/
class Connect extends Controller
{
/**
* qq登录
*
* 可以在url 中传入 参数 callback_url 用来做通知回调 ; 例如 xxx.com/callback/tencent/login?callback_url=http%3A%2F%2Fwww.a.com%2Fa%2Fb%2Fc%3Fd%3D123
* callback_url 参数说明 传入前需要做 urlencode($callback_url) 操作
* callback_url 回调地址要求允许跨域或者 csrf
*/
public function login()
{
$jump_url = request()->get('callback_url', '');
$jumpUrl = $jump_url ? urldecode($jump_url) : '';
$qq = new QqOauth(config('callback.qq'));
// $url = $qq->authorization(); // 不传值方式
$url = $qq->authorization($jumpUrl); // 传入的数据 $jumpUrl 将会在 qq_callback 回调中返回得到
// 重定向到外部地址
return redirect()->away($url);
}
public function notify()
{
$auth = new QqOauth(config('callback.qq'));
$userInfo = $auth->getUserInfo('');
$callbackUrl = $auth->getStateParam();
// 记录用户信息
$loginUserInfo = UserServices::instance()->fastLogin('qq', $userInfo);
if ($callbackUrl) {
return buildRequestFormAndSend($callbackUrl, $loginUserInfo);
} else {
dump($loginUserInfo);
}
}
}
php
use zxf\Login\WeiboOauth;
/**
* 新浪微博登录
*/
class Sina extends Controller
{
/**
* 微博登录
*
* 可以在url 中传入 参数 callback_url 用来做通知回调 ; 例如 xxx.com/callback/weibo/login?callback_url=http%3A%2F%2Fwww.a.com%2Fa%2Fb%2Fc%3Fd%3D123
* callback_url 参数说明 传入前需要做 urlencode($callback_url) 操作
* callback_url 回调地址要求允许跨域或者 csrf
*/
public function login()
{
$jump_url = request()->get('callback_url', '');
$jumpUrl = $jump_url ? urldecode($jump_url) : '';
$weibo = new WeiboOauth(config('callback.sina'));
// $url = $qq->authorization(); // 不传值方式
$weibo->authorization($jumpUrl); // 传入的数据 $jumpUrl 将会在 qq_callback 回调中返回得到
}
public function notify()
{
$auth = new WeiboOauth(config('callback.sina'));
$userInfo = $auth->getUserInfo('');
$callbackUrl = $auth->getStateParam();
// 记录用户信息
$loginUserInfo = UserServices::instance()->fastLogin('sina', $userInfo);
if ($callbackUrl) {
return buildRequestFormAndSend($callbackUrl, $loginUserInfo);
} else {
dump($userInfo);
}
}
}
$minifier = new \zxf\Min\JS('var a = "hello";',$jsFilePath,...); // 实例化 并混合自动引入 任意个 需要压缩的 js 文件路径 和 代码段
$res = $minifier->minify();
php
use zxf\QrCode\QRCodeGenerate;
$levelMap = [
'high' => EccLevel::H,
'quartile' => EccLevel::Q,
'medium' => EccLevel::M,
'low' => EccLevel::L,
];
$qrcode = new QRCodeGenerate([
// 'version' => $level == 'high' ? min(max(strlen($text) / 10, 10), 35) : 2,
// 'version' => \zxf\QrCode\Common\Version::AUTO,
'eccLevel' => !empty($logoPath) ? EccLevel::H : $levelMap[$level],
'scale' => (int)($input['scale'] ?: 2), // 每个模块的像素大小
]);
$handle = $qrcode
->content($text)
->withText($label ?? '', $fontPath??'', $fontSize??10) // 可选
->withLogo($logoPath) // 可选
;
// 把图片直接输出到浏览器上
$handle->toBrowser();
// 生成图片保存到文件
$handle->toFile('/your/path/to/qrcode.png');
// 生成base64图片字符串
$base64 = $handle->toBase64();
echo '<img src="' . $base64 . '">';
php
/**
* 功能:图片压缩类(可改变图片大小和压缩质量以及保留宽高压缩)
*
* @DateTime 2019-03-08
*
* 调用示例:
* # 实例化对象
* $Compressor = new \zxf\tools\Compressor();
* OR
* $Compressor = \zxf\tools\Compressor::instance();
*
* # 使用原始尺寸 压缩图片大小并输出到浏览器
* $result = $Compressor->set('001.jpg')->proportion(1)->get();
* # 仅压缩
* $result = $Compressor->set('001.jpg')->compress(5)->get();
* # 仅改变尺寸并保存到指定位置
* $result = $Compressor->set('001.jpg', './resizeOnly.jpg')->resize(500, 500)->get();
* # 压缩且改变尺寸并保存到指定位置
* $result = $Compressor->set('001.jpg', './resizeAndCompress.png')->resize(0, 500)->compress(5)->get();
* # 压缩且按照比例压缩
* $result = $Compressor->set('001.jpg', './resizeAndCompress.png')->proportion(0.5)->compress(5)->get();
* return $result;
* 参数说明:
* set(原图路径,保存后的路径); // 如果要直接输出到浏览器则只传第一个参数即可
* resize(设置宽度,设置高度);//如果有一个参数为0,则保持宽高比例
* proportion(压缩比例);//0.1~1 根据比例压缩
* compress(压缩级别);//0~9,压缩级别,级别越高就图片越小也就越模糊
* get();//获取生成后的结果
* 提示:
* proportion 方法 回去调用 resize 方法,因此他们两个方法只需要选择调用一个即可
*/
php
use zxf\tools\TextToImg;
/**
* 文字生成图片
*
* // 创建一个实例
* $TextToImg = new TextToImg(1200, 800);
*
* $TextToImg->setFontFile('./arial.ttf'); // 设置自定义字体路径
* $TextToImg->setFontStyle('foxi'); // 选择本库中支持的一种字体
* $TextToImg->setText('这是<br>一段<br>测试文字'); // 设置文字内容,支持使用 <br> 换行
* $TextToImg->setColor('FF00FF'); // 设置文字颜色
* $TextToImg->setBgColor('00FF00'); // 设置图片背景色
* $TextToImg->setAngle(90);// 设置文字旋转
* $TextToImg->setSize(20);// 设置文字固定字号为20【提示:本库默认会自动计算字体大小,如果设置该属性就使用传入的固定值】
* $TextToImg->render();// 显示图片到浏览器
* $TextToImg->render('test.png');// 将图片保存至本地
*/
php
use zxf\sms\Sms;
$accessKeyId = "阿里云或者腾讯云 appid";
$accessKeySecret = "阿里云或者腾讯云 secret";
// 可发送多个手机号,变量为数组即可,如:[11111111111, 22222222222]
$mobile = '18***888';
$template = '您申请的短信模板';
$sign = '您申请的短信签名';
// 短信模板中用到的 参数 模板变量为键值对数组
$params = [
"code" => rand(1000, 9999),
"title" => '您的标题',
"content" => '您的内容',
];
// 初始化 短信服务(阿里云短信或者腾讯云短信)
$smsObj = Sms::instance($accessKeyId, $accessKeySecret,'ali或者tencent');
// 若使用的是 腾讯云短信 需要 设置 appid 参数; 阿里云则不用
// $smsObj = $smsObj->setAppid($appid);
// 发起请求
// 需要注意,设置配置不分先后顺序,send后也不会清空配置
$result = $aliyunSms->setMobile($mobile)->setParams($params)->setTemplate($template)->setSign($sign)->send();
/**
* 返回值为bool,你可获得阿里云响应做出你业务内的处理
*
* status bool 此变量是此包用来判断是否发送成功
* code string 阿里云短信响应代码
* message string 阿里云短信响应信息
*/
if (!$result) {
$response = $aliyunSms->getResponse();
// 做出处理
}
// 结构:
$arr = array(
array('id'=>'1','pid'=>0,'name'=>'一级栏目一'),
array('id'=>'2','pid'=>0,'name'=>'一级栏目二', 'weight' => 101),
array('id'=>'3','pid'=>1,'name'=>'二级栏目一', 'weight' => 1),
array('id'=>'4','pid'=>1,'name'=>'二级栏目二', 'weight' => 2),
array('id'=>'5','pid'=>2,'name'=>'二级栏目三'),
array('id'=>'6','pid'=>3,'name'=>'三级栏目一'),
array('id'=>'7','pid'=>3,'name'=>'三级栏目二')
)
// 用法:
// 使用默认配置 初始化数据
$tree = zxf\tools\Tree::instance($data);
// OR
$tree = zxf\tools\Tree::instance()->setData($data);
// 自定义id、pid、children配置
zxf\tools\Tree::instance($data)->setId('id')->setPid('pid')->setChildlist('children')->getTree();
// 自定义权重字段和排序方式
zxf\tools\Tree::instance($data)->setWeight('weight')->setSortType('desc')->getTree();
// 自定义根节点id,默认为0
zxf\tools\Tree::instance($data)->setRootId(1)->getTree();
// 接口:
// 获取结构树
$nodes = $tree->getTree();
// 获取所有子节点的主键(包含自己)
$nodes = $tree->getChildrenAndMeIds(1);
// 获取所有子节点列表(包含自己)
$nodes = $tree->getChildrenAndMeNodes(1);
// 获取所有子节点的主键(不包含自己)
$nodes = $tree->getChildrenIds(1);
// 获取所有子节点列表(不包含自己)
$nodes = $tree->getChildrenNodes(1);
// 获取所有父节点主键(包含自己)
$nodes = $tree->getParentAndMeIds(5);
// 获取所有父节点列表(包含自己)
$nodes = $tree->getParentAndMeNodes(5);
// 获取所有父节点主键(不包含自己)
$nodes = $tree->getParentIds(5);
// 获取所有父节点列表(不包含自己)
$nodes = $tree->getParentNodes(5);
// 获取所有根节点主键
$roots = $tree->getRootsIds();
// 重新初始化数据
$tree->reset();
// 添加新节点
$tree->addNode(['id' => 7, 'pid' => 0, 'name' => 'New Node']);
// 删除节点
$tree->removeNode(7);
// 更新节点
$tree->updateNode(2, ['name' => 'Updated Node']);