1 Star 1 Fork 0

张先生/common_func

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
common_func.php 3.14 KB
Copy Edit Raw Blame History
张先生 authored 2020-04-07 11:22 +08:00 . add common_func.php.
<?php
/**@formatCategory 无线级分类处理
* @param array $data 数据源
* @param string $fieldName 字段名
* @return array
* @author 张先生
* @date 2020-03-27
*/
if(!function_exists('formatCategory')){
function formatCategory(array $data , string $idName = "id", string $fieldName = 'pid', $childrenKey = 'child')
{
$items = [];
foreach ($data as $item) {
$items[$item[$idName]] = $item;
}
$result = array();
foreach($items as $item){
if(isset($items[$item[$fieldName]])){
$items[$item[$fieldName]][$childrenKey][] = &$items[$item[$idName]];
}else{
$result[] = &$items[$item[$idName]];
}
}
return $result;
}
}
/**
* @function toMap 数组重新组装
* @param array $data 数据
* @param string $field key
* @param string $value value default null
* @author 张先生
* @date 2020-04-01
* @return array
*/
if(!function_exists('toMap')){
function toMap(array $data, $field = 'id', $value = ''){
$result = array();
if(empty($data)){
return $result;
}
//开始处理数据
foreach ($data as $item) {
$val = $item;
if(!empty($value)){
$val = $item[$value];
}
$result[$item[$field]] = $val;
}
return $result;
}
}
/**
* @function getUniqueListByArray 从数组中获取某个字段的值,重新拼装成新的一维数组
* @param array $data 数据
* @param string $field key
* @author 张先生
* @date 2020-04-01
* @return array
*/
if(!function_exists('getUniqueListByArray')){
function getUniqueListByArray(array $data, $field = 'id'){
return array_unique(array_values(array_column($data, $field)));
}
}
/**
* @function getCode 生成验证码 返回base64图片
* @author 张先生
* @date 2020-04-01
* @return string
*/
// 获取验证码(参数:验证码个数,验证码宽度,验证码高度)
if(!function_exists('getCode')) {
function getBase64Code(string $code, $width = 100, $height = 30)
{
$image = imagecreate($width, $height); // 赋值宽度,高度
imagecolorallocate($image, 255, 255, 255); // 设定图片背景颜色
// 生成干扰像素
for ($i = 0; $i < 80; $i++) {
$dis_color = imagecolorallocate($image, rand(0, 2555), rand(0, 255), rand(0, 255));
imagesetpixel($image, rand(1, $width), rand(1, $height), $dis_color);
}
// 打印字符到图像
$num = strlen($code);
for ($i = 0; $i < $num; $i++) {
$char_color = imagecolorallocate($image, rand(0, 2555), rand(0, 255), rand(0, 255));
imagechar($image, 100, ($width / $num) * $i, rand(0, 5), $code[$i], $char_color);
}
ob_start();
imagepng($image);
$imageData = ob_get_contents();
imagedestroy($image);//释放资源
ob_end_clean();
return "data:image/png;base64," . base64_encode($imageData);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/shmilyle/common_func.git
git@gitee.com:shmilyle/common_func.git
shmilyle
common_func
common_func
master

Search