方法一: 将指定颜色变浅
public static function adjustBrightness ( $hex , $steps ) {
$steps = max ( - 255 , min ( 255 , $steps ) ) ;
$hex = str_replace ( '#' , '' , $hex ) ;
if ( strlen ( $hex ) == 3 ) {
$hex = str_repeat ( substr ( $hex , 0 , 1 ) , 2 ) . str_repeat ( substr ( $hex , 1 , 1 ) , 2 ) . str_repeat ( substr ( $hex , 2 , 1 ) , 2 ) ;
}
$color_parts = str_split ( $hex , 2 ) ;
$return = '#' ;
foreach ( $color_parts as $color ) {
$color = hexdec ( $color ) ;
$color = max ( 0 , min ( 255 , $color + $steps ) ) ;
$return .= str_pad ( dechex ( $color ) , 2 , '0' , STR_PAD_LEFT ) ;
}
return $return ;
}
方法二:数组转树形
public static function list_to_tree ( $list , $root = 0 , $pk = 'id' , $pid = 'parentid' , $child = '_child' ) {
$tree = array ( ) ;
if ( is_array ( $list ) ) {
$refer = array ( ) ;
foreach ( $list as $key => $data ) {
$refer [ $data [ $pk ] ] = & $list [ $key ] ;
}
foreach ( $list as $key => $data ) {
$parentId = 0 ;
if ( isset ( $data [ $pid ] ) ) {
$parentId = $data [ $pid ] ;
}
if ( ( string ) $root == $parentId ) {
$tree [ ] = & $list [ $key ] ;
} else {
if ( isset ( $refer [ $parentId ] ) ) {
$parent = & $refer [ $parentId ] ;
$parent [ $child ] [ ] = & $list [ $key ] ;
}
}
}
}
return $tree ;
}
方法三:去除多维数组空值
public static function filter_array ( $arr , $values = [ '' , null , false , 0 , '0' , [ ] ] ) {
foreach ( $arr as $k => $v ) {
if ( is_array ( $v ) && count ( $v ) > 0 ) {
$arr [ $k ] = self :: filter_array ( $v , $values ) ;
}
foreach ( $values as $value ) {
if ( $v === $value ) {
unset ( $arr [ $k ] ) ;
break ;
}
}
}
return $arr ;
}
方法四:PHP数组分页
public static function arrayPage ( $arr , $rowsPerPage , $url , $curPage = 1 , $param = 'curpage' )
{
$totalPage = count ( $arr ) ;
$begin = ( $curPage - 1 ) * $rowsPerPage ;
$ret = array_slice ( $arr , $begin , $rowsPerPage ) ;
$totalPage = ceil ( $totalPage / $rowsPerPage ) ;
$pageNumString = '' ;
if ( $curPage <= 5 ) {
$begin = 1 ;
$end = $totalPage >= 10 ? 10 : $totalPage ;
} else {
$end = $curPage + 5 > $totalPage ? $totalPage : $curPage + 5 ;
$begin = $end - 9 <= 1 ? 1 : $end - 9 ;
}
$prev = $curPage - 1 <= 1 ? 1 : $curPage - 1 ;
$pageNumString .= "<li><a href='$url ?$param =1'>首页</a></li>" ;
$pageNumString .= "<li><a href='$url ?$param =$prev '>«</a></li>" ;
for ( $i = $begin ; $i <= $end ; $i ++ ) {
if ( $curPage == $i ) {
$pageNumString .= "<li class='active'><a href='$url ?$param =$i '>$i </a></li>" ;
} else {
$pageNumString .= "<li><a href='$url ?$param =$i '>$i </a></li>" ;
}
}
$next = $curPage + 1 >= $totalPage ? $totalPage : $curPage + 1 ;
$pageNumString .= "<li><a href='$url ?$param =$next '>»</a></li>" ;
$pageNumString .= "<li><a href='$url ?$param =$totalPage '>尾页</a></li>" ;
return [ 'ret' => $ret , 'pageNumString' => $pageNumString ] ;
}
方法五:将Base64图片转换为本地图片并保存
public static function base64_image_content ( $base64_image_content , $path )
{
if ( preg_match ( '/^(data:\s*image\/(\w+);base64,)/' , $base64_image_content , $result ) ) {
$type = "jpg" ;
$new_file1 = $path . date ( 'Ymd' ) . DS ;
if ( ! file_exists ( $new_file1 ) ) {
mkdir ( $new_file1 , 0755 , true ) ;
}
$new_file = $new_file1 . time ( ) . ".{ $type } " ;
$dist_img = $new_file1 . time ( ) . "_yasuo.{ $type } " ;
if ( file_put_contents ( $new_file , base64_decode ( str_replace ( $result [ 1 ] , '' , $base64_image_content ) ) ) ) {
$dist = self :: img2thumb ( $new_file , $dist_img , 150 , 150 , 0 , 0.4 ) ;
if ( $dist ) {
return [ "src_img" => $new_file , "dist_img" => $dist_img ] ;
} else {
return false ;
}
} else {
return false ;
}
} else {
return false ;
}
}
public static function img2thumb ( $src_img , $dst_img , $width = 75 , $height = 75 , $cut = 0 , $proportion = 0 )
{
if ( ! is_file ( $src_img ) || filesize ( $src_img ) <= 0 ) {
return false ;
}
$ot = pathinfo ( $dst_img , PATHINFO_EXTENSION ) ;
$otfunc = 'image' . ( $ot == 'jpg' ? 'jpeg' : $ot ) ;
if ( ! function_exists ( $otfunc ) ) {
Log :: error ( "undefined function: { $otfunc } " , func_get_args ( ) ) ;
return false ;
}
$srcinfo = @getimagesize ( $src_img ) ;
$src_w = $srcinfo [ 0 ] ;
$src_h = $srcinfo [ 1 ] ;
$type = strtolower ( substr ( image_type_to_extension ( $srcinfo [ 2 ] ) , 1 ) ) ;
$createfun = 'imagecreatefrom' . ( $type == 'jpg' ? 'jpeg' : $type ) ;
$dst_h = $height ;
$dst_w = $width ;
$x = $y = 0 ;
if ( ! function_exists ( $createfun ) ) {
Log :: error ( "undefined function: { $createfun } " , func_get_args ( ) ) ;
return false ;
}
if ( ( $width > $src_w && $height > $src_h ) || ( $height > $src_h && $width == 0 ) || ( $width > $src_w && $height == 0 ) ) {
$proportion = 1 ;
}
if ( $width > $src_w ) {
$dst_w = $width = $src_w ;
}
if ( $height > $src_h ) {
$dst_h = $height = $src_h ;
}
if ( ! $width && ! $height && ! $proportion ) {
return false ;
}
if ( ! $proportion ) {
if ( $cut == 0 ) {
if ( $dst_w && $dst_h ) {
if ( $dst_w / $src_w > $dst_h / $src_h ) {
$dst_w = $src_w * ( $dst_h / $src_h ) ;
$x = 0 - ( $dst_w - $width ) / 2 ;
} else {
$dst_h = $src_h * ( $dst_w / $src_w ) ;
$y = 0 - ( $dst_h - $height ) / 2 ;
}
} else {
if ( $dst_w xor $dst_h ) {
if ( $dst_w && ! $dst_h ) {
$propor = $dst_w / $src_w ;
$height = $dst_h = $src_h * $propor ;
} else {
if ( ! $dst_w && $dst_h ) {
$propor = $dst_h / $src_h ;
$width = $dst_w = $src_w * $propor ;
}
}
}
}
} else {
if ( ! $dst_h ) {
$height = $dst_h = $dst_w ;
}
if ( ! $dst_w ) {
$width = $dst_w = $dst_h ;
}
$propor = min ( max ( $dst_w / $src_w , $dst_h / $src_h ) , 1 ) ;
$dst_w = ( int ) round ( $src_w * $propor ) ;
$dst_h = ( int ) round ( $src_h * $propor ) ;
$x = ( $width - $dst_w ) / 2 ;
$y = ( $height - $dst_h ) / 2 ;
}
} else {
$proportion = min ( $proportion , 1 ) ;
$height = $dst_h = $src_h * $proportion ;
$width = $dst_w = $src_w * $proportion ;
}
$src = @$createfun ( $src_img ) ;
if ( ! $src ) {
Log :: error ( "img2thumb failed" , func_get_args ( ) ) ;
return false ;
}
$dst = imagecreatetruecolor ( $width ? $width : $dst_w , $height ? $height : $dst_h ) ;
$white = imagecolorallocate ( $dst , 255 , 255 , 255 ) ;
imagefill ( $dst , 0 , 0 , $white ) ;
if ( function_exists ( 'imagecopyresampled' ) ) {
imagecopyresampled ( $dst , $src , $x , $y , 0 , 0 , $dst_w , $dst_h , $src_w , $src_h ) ;
} else {
imagecopyresized ( $dst , $src , $x , $y , 0 , 0 , $dst_w , $dst_h , $src_w , $src_h ) ;
}
$otfunc ( $dst , $dst_img ) ;
imagedestroy ( $dst ) ;
imagedestroy ( $src ) ;
return true ;
}
方法六:CURL 请求
public static function curl_post ( $host , $bodys )
{
$headers = array ( ) ;
$bodys = http_build_query ( $bodys ) ;
$url = $host ;
array_push ( $headers , "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8" ) ;
$curl = curl_init ( ) ;
curl_setopt ( $curl , CURLOPT_CUSTOMREQUEST , "POST" ) ;
curl_setopt ( $curl , CURLOPT_URL , $url ) ;
curl_setopt ( $curl , CURLOPT_HTTPHEADER , $headers ) ;
curl_setopt ( $curl , CURLOPT_FAILONERROR , false ) ;
curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , true ) ;
curl_setopt ( $curl , CURLOPT_HEADER , false ) ;
if ( 1 == strpos ( "$" . $host , "https://" ) ) {
curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , false ) ;
curl_setopt ( $curl , CURLOPT_SSL_VERIFYHOST , false ) ;
}
curl_setopt ( $curl , CURLOPT_POSTFIELDS , $bodys ) ;
$content = curl_exec ( $curl ) ;
curl_close ( $curl ) ;
return $content ;
}
public static function https_request ( $url = '' , $param = [ ] , $contentType = '' )
{
$ch = curl_init ( ) ;
curl_setopt ( $ch , CURLOPT_URL , $url ) ;
$param = $contentType == 'json' ? json_encode ( $param , true ) : $param ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , false ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , false ) ;
if ( $param ) {
curl_setopt ( $ch , CURLOPT_POST , 1 ) ;
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $param ) ;
}
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true ) ;
$output = curl_exec ( $ch ) ;
curl_close ( $ch ) ;
return $output !== false ? $output : false ;
}
public static function curl_get ( $url )
{
if ( $ch = curl_init ( ) ) {
curl_setopt ( $ch , CURLOPT_URL , $url ) ;
curl_setopt ( $ch , CURLOPT_TIMEOUT , 30 ) ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 ) ;
curl_setopt ( $ch , CURLOPT_POST , 1 ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , FALSE ) ;
$content = curl_exec ( $ch ) ;
curl_close ( $ch ) ;
return $content ;
} else {
echo "系统CURL初始化失败!" ;
exit ;
}
}