//转大写数字 最大99
function daxie($number){
$number=substr($number,0,2);
$arr=array("零","一","二","三","四","五","六","七","八","九");
if(strlen($number)==1){
$result=$arr[$number];
}else{
if($number==10){
$result="十";
}else{
if($number<20){
$result="十";
}else{
$result=$arr[substr($number,0,1)]."十";
}
if(substr($number,1,1)!="0"){
$result.=$arr[substr($number,1,1)];
}
}
}
return $result;
}
/**
* 生成不重复的随机数
* @param int $start 需要生成的数字开始范围
* @param int $end 结束范围
* @param int $length 需要生成的随机数个数
* @return array 生成的随机数
*/
function get_rand_number($start=1,$end=10,$length=4){
$connt=0;
$temp=array();
while($connt<$length){
$temp[]=mt_rand($start,$end);
$data=array_unique($temp);
$connt=count($data);
}
sort($data);
return $data;
}
//汉字转数组
function ch2arr($str)
{
$length = mb_strlen($str, 'utf-8');
$array = [];
for ($i=0; $i<$length; $i++)
$array[] = mb_substr($str, $i, 1, 'utf-8');
return $array;
}
//获取全年每月的头尾日期
function getOneYearAllMonth($year = null)
{
if ($year == null) {
$year = date('Y');
}
//首先判断闰年
if($year%400 == 0 || ($year%4 == 0 && $year%100 !== 0)){
$rday = 29;
}else{
$rday = 28;
}
for ($i=1; $i<=12;$i++){
if($i==2){
$days = $rday;
}else{
//判断是大月(31),还是小月(30)
$days = (($i - 1)%7%2) ? 30 : 31;
}
if($i < 10)
{
$mon = "0".$i;
}
else
{
$mon = $i;
}
$mouth[] = array(
'month' => $i,
'start' => $year.$mon."01",
'end' => $year.$mon.$days,
);
//echo $year."年".$i."月有:".$days."天"."<br>";
}
return $mouth;
}
/**
* 判断某年的某月有多少天
* @return [type] [description]
*/
function daysInmonth($year='',$month='')
{
if(empty($year))
{
$year = date('Y');
}
if(empty($month))
{
$month = date('m');
}
if (in_array($month, array(1, 3, 5, 7, 8, '01', '03', '05', '07', '08', 10, 12)))
{
$text = '31'; //月大
}
elseif ($month == 2 || $month == '02')
{
//判断是否是闰年
if ( ($year % 400 == 0) || ( ($year % 4 == 0) && ($year % 100 !== 0) ) )
{
$text = '29'; //闰年2月
}
else
{
$text = '28'; //平年2月
}
}
else
{
$text = '30'; //月小
}
return intval($text);
}
/**
* [yuan_img 编辑图片为圆形] 剪切头像为圆形
* @param [string] $imgpath [头像保存之后的图片名]
*/
function yuan_img($imgpath) {
$ext = pathinfo($imgpath);
$src_img = null;
switch ($ext['extension']) {
case 'jpg':
$src_img = imagecreatefromjpeg($imgpath);
break;
case 'png':
$src_img = imagecreatefromjpeg($imgpath);
break;
}
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圆半径
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
return $img;
}
//随机字符串
private function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//http
function httpGet($url)
{
$curl = curl_init (); // 启动一个CURL会话
curl_setopt ( $curl, CURLOPT_URL, $url );
curl_setopt ( $curl, CURLOPT_HEADER, 0 );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false ); // 跳过证书检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false ); // 从证书中检查SSL加密算法是否存在
$tmpInfo = curl_exec ( $curl ); // 返回api的json对象
// 关闭URL请求
curl_close ( $curl );
return $tmpInfo; // 返回json对象
}
/**
* 文字居中
* @param $text
*/
public static function middle($width,$text,$fontSize) {
$len = mb_strlen($text,"UTF-8");
$fix = ($len != 1) ? $len*5 : 4; //比例要与图片实际宽度对应
return ceil(($width-$len*$fontSize)/2) - $fix;
}