摘要: 今天刚好有个闲暇的时间整理一下近段时间以来写的几个有助于开发的小函数,所为函数不过是...
今天刚好有个闲暇的时间整理一下近段时间以来写的几个有助于开发的小函数,所为函数不过是没有放在一个php类里面而实现一定功能的可执行程序罢了,只要放在了类里面,那么函数不再是那个函数了,也就变成了方法。相应的再次访问该方法就需要用到self访问了。再具体一点的不用我指明了。
1.获取分层分级的所有元素进行序列化,适用于评论回复数据的展示
/**
* 分级排序
* @param $data
* @param int $pid
* @param string $field
* @param string $pk
* @param string $html
* @param int $level
* @param bool $clear
* @return array
*/
public static function sortListTier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1,
$clear = true)
{
static $list = [];
if ($clear) $list = [];
foreach ($data as $k => $res) {
if ($res[$field] == $pid) {
$res['html'] = str_repeat($html, $level);
$list[] = $res;
unset($data[$k]);
self::sortListTier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
}
}
return $list;
}
2.返回多维数组
/**
* 分级返回多维数组
* @param $data
* @param int $pid
* @param string $field
* @param string $pk
* @param int $level
* @return array
*/
public static function getChindNode($data, $pid = 0, $field = 'pid', $pk = 'id', $level = 1)
{
static $list = [];
foreach ($data as $k => $res) {
if ($res['pid'] == $pid) {
$list[] = $res;
unset($data[$k]);
self::getChindNode($data, $res['id'], $field, $pk, $level + 1);
}
}
return $list;
}
3.分级返回所有下级id
/**分级返回下级所有分类ID
* @param $data
* @param string $children
* @param string $field
* @param string $pk
* @return string
*/
public static function getChildrenPid($data, $pid, $field = 'pid', $pk = 'id')
{
static $pids = '';
foreach ($data as $k => $res) {
if ($res[$field] == $pid) {
$pids .= ',' . $res[$pk];
self::getChildrenPid($data, $res[$pk], $field, $pk);
}
}
return $pids;
}
4.关于身份证的查询
/**
* 身份证验证
* @param $card
* @return bool
*/
public static function setCard($card)
{
$city = [11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "内蒙古", 21 => "辽宁", 22 => "吉林",
23 => "黑龙江 ", 31 => "上海", 32 => "江苏", 33 => "浙江", 34 => "安徽", 35 => "福建", 36 => "江西", 37 => "山东",
41 => "河南", 42 => "湖北 ", 43 => "湖南", 44 => "广东", 45 => "广西", 46 => "海南", 50 => "重庆", 51 => "四川",
52 => "贵州", 53 => "云南", 54 => "西藏 ", 61 => "陕西", 62 => "甘肃", 63 => "青海", 64 => "宁夏", 65 => "新疆",
71 => "台湾", 81 => "香港", 82 => "澳门", 91 => "国外 "];
$tip = "";
$match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
$pass = true;
if (!$card || !preg_match($match, $card)) {
//身份证格式错误
$pass = false;
} else if (!$city[substr($card, 0, 2)]) {
//地址错误
$pass = false;
} else {
//18位身份证需要验证最后一位校验位
if (strlen($card) == 18) {
$card = str_split($card);
//∑(ai×Wi)(mod 11)
//加权因子
$factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
//校验位
$parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
$sum = 0;
$ai = 0;
$wi = 0;
for ($i = 0; $i < 17; $i++) {
$ai = $card[$i];
$wi = $factor[$i];
$sum += $ai * $wi;
}
$last = $parity[$sum % 11];
if ($parity[$sum % 11] != $card[17]) {
// $tip = "校验位错误";
$pass = false;
}
} else {
$pass = false;
}
}
if (!$pass) return false;/* 身份证格式错误*/
return true;/* 身份证格式正确*/
}
/**
* @param array $data
* @param $path
* @return array|bool|string
* @throws \Exception
*/
public static function setShareMarketingPoster($data = array(), $path)
{
$config = array(
'text' => array(
array(
'text' => $data['price'],//TODO 价格
'left' => 116,
'top' => 200,
'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf',
//字体文件
'fontSize' => 50, //字号
'fontColor' => '255,0,0', //字体颜色
'angle' => 0,
),
array(
'text' => $data['label'],//TODO 标签
'left' => 394,
'top' => 190,
'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf',
//字体文件
'fontSize' => 24, //字号
'fontColor' => '255,255,255', //字体颜色
'angle' => 0,
),
array(
'text' => $data['msg'],//TODO 简述
'left' => 80,
'top' => 270,
'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf',
//字体文件
'fontSize' => 22, //字号
'fontColor' => '40,40,40', //字体颜色
'angle' => 0,
)
),
'image' => array(
array(
'url' => $data['image'], //图片
'stream' => 0,
'left' => 120,
'top' => 340,
'right' => 0,
'bottom' => 0,
'width' => 450,
'height' => 450,
'opacity' => 100
),
array(
'url' => $data['url'], //二维码资源
'stream' => 0,
'left' => 260,
'top' => 890,
'right' => 0,
'bottom' => 0,
'width' => 160,
'height' => 160,
'opacity' => 100
)
),
'background' => app()->getRootPath() . '/public/static/poster/poster.jpg'
);
if (!file_exists($config['background'])) exception('缺少系统预设背景图片');
if (strlen($data['title']) < 36) {
$text = array(
'text' => $data['title'],//TODO 标题
'left' => 76,
'top' => 100,
'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf',
//字体文件
'fontSize' => 32, //字号
'fontColor' => '0,0,0', //字体颜色
'angle' => 0,
);
array_push($config['text'], $text);
} else {
$titleOne = array(
'text' => mb_substr($data['title'], 0, 12),//TODO 标题
'left' => 76,
'top' => 70,
'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf',
//字体文件
'fontSize' => 32, //字号
'fontColor' => '0,0,0', //字体颜色
'angle' => 0,
);
$titleTwo = array(
'text' => mb_substr($data['title'], 12, 12),//TODO 标题
'left' => 76,
'top' => 120,
'fontPath' => app()->getRootPath() . 'public/static/font/Alibaba-PuHuiTi-Regular.otf',
//字体文件
'fontSize' => 32, //字号
'fontColor' => '0,0,0', //字体颜色
'angle' => 0,
);
array_push($config['text'], $titleOne);
array_push($config['text'], $titleTwo);
}
return self::setSharePoster($config, $path);
}
5.TODO生成微信分享二维码
/**
* TODO 生成分享二维码图片
* @param array $config
* @param $path
* @return array|bool|string
* @throws \Exception
*/
public static function setSharePoster($config = array(), $path)
{
$imageDefault = array(
'left' => 0,
'top' => 0,
'right' => 0,
'bottom' => 0,
'width' => 100,
'height' => 100,
'opacity' => 100
);
$textDefault = array(
'text' => '',
'left' => 0,
'top' => 0,
'fontSize' => 32, //字号
'fontColor' => '255,255,255', //字体颜色
'angle' => 0,
);
$background = $config['background'];//海报最底层得背景
$backgroundInfo = getimagesize($background);
$background = imagecreatefromstring(file_get_contents($background));
$backgroundWidth = $backgroundInfo[0]; //背景宽度
$backgroundHeight = $backgroundInfo[1]; //背景高度
$imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
imagefill($imageRes, 0, 0, $color);
imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background),
imagesx($background), imagesy($background));
if (!empty($config['image'])) {
foreach ($config['image'] as $key => $val) {
$val = array_merge($imageDefault, $val);
$info = getimagesize($val['url']);
$function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
if ($val['stream']) {
$info = getimagesizefromstring($val['url']);
$function = 'imagecreatefromstring';
}
$res = $function($val['url']);
$resWidth = $info[0];
$resHeight = $info[1];
$canvas = imagecreatetruecolor($val['width'], $val['height']);
imagefill($canvas, 0, 0, $color);
imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth,
$resHeight);
$val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] :
$val['left'];
$val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] :
$val['top'];
imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'],
$val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
}
}
if (isset($config['text']) && !empty($config['text'])) {
foreach ($config['text'] as $key => $val) {
$val = array_merge($textDefault, $val);
list($R, $G, $B) = explode(',', $val['fontColor']);
$fontColor = imagecolorallocate($imageRes, $R, $G, $B);
$val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
$val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor,
$val['fontPath'], $val['text']);
}
}
ob_start();
imagejpeg($imageRes);
imagedestroy($imageRes);
$res = ob_get_contents();
ob_end_clean();
$key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
return UploadService::instance()->setUploadPath($path)->imageStream($key, $res);
}
6.检测远程文件是否存在
/**
* CURL 检测远程文件是否在
* @param $url
* @return bool
*/
public static function CurlFileExist($url)
{
$ch = curl_init();
try {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$contents = curl_exec($ch);
if (preg_match("/404/", $contents)) return false;
if (preg_match("/403/", $contents)) return false;
return true;
} catch (\Exception $e) {
return false;
}
}
7.获取图片转为base64地址
/**
* 获取图片转为base64
* @param string $avatar
* @return bool|string
*/
public static function setImageBase64($avatar = '', $timeout = 9)
{
try {
$url = parse_url($avatar);
$url = $url['host'];
$header = [
'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',
'Host:' . $url
];
$dir = pathinfo($url);
$host = $dir['dirname'];
$refer = $host . '/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_REFERER, $refer);
curl_setopt($curl, CURLOPT_URL, $avatar);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
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:image/jpeg;base64," . base64_encode($data);
else return false;
} catch (\Exception $e) {
return false;
}
}
8.时间戳人性化转化
/**
* 时间戳人性化转化
* @param $time
* @return string
*/
public static function timeTran($time)
{
$t = time() - $time;
$f = array(
'31536000' => '年',
'2592000' => '个月',
'604800' => '星期',
'86400' => '天',
'3600' => '小时',
'60' => '分钟',
'1' => '秒'
);
foreach ($f as $k => $v) {
if (0 != $c = floor($t / (int)$k)) {
return $c . $v . '前';
}
}
}
此篇文章由DurkBlue发布,转载请注明来处