获取当前网址
$_SERVER['REQUEST_URI'], ///test.html
向下取整
floor()
以相反的元素顺序返回数组
array_reverse()
$_SERVER[‘REQUEST_URI’]返回/后面的所有
例:http://www.test.com/index.php/dir/test — – — /index.php/dir/test
判断是否移动端
function isMobile()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobile_agents = ['iphone','android','phone','mobile','wap','netfront','java','opera mobi',
'opera mini','ucweb','windows ce','symbian','series','webos','sony','blackberry','dopod',
'nokia','samsung','palmsource','xda','pieplus','meizu','midp','cldc','motorola','foma',
'docomo','up.browser','up.link','blazer','helio','hosin','huawei','novarra','coolpad',
'techfaith','alcatel','amoi','ktouch','nexian','ericsson','philips','sagem','wellcom',
'bunjalloo','maui','smartphone','iemobile','spice','bird','zte-','longcos','pantech',
'gionee','portalmmm','jig browser','hiptop','benq','haier','^lct','320x320','240x320',
'176x220','windows phone','cect','compal','ctl','lg','nec','tcl','daxian','dbtel','eastcom',
'konka','kejian','lenovo','mot','soutec','sgh','sed','capitel','panasonic','sonyericsson',
'sharp','panda','zte','acer','acoon','acs-','abacho','ahong','airness','anywhereyougo.com',
'applewebkit/525','applewebkit/532','asus','audio','au-mic','avantogo','becker','bilbo',
'bleu','cdm-','danger','elaine','eric','etouch','fly ','fly_','fly-','go.web','goodaccess',
'gradiente','grundig','hedy','hitachi','htc','hutchison','inno','ipad','ipaq','ipod',
'jbrowser','kddi','kgt','kwc','lg ','lg2','lg3','lg4','lg5','lg7','lg8','lg9','lg-','lge-',
'lge9','maemo','mercator','meridian','micromax','mini','mitsu','mmm','mmp','mobi','mot-',
'moto','nec-','newgen','nf-browser','nintendo','nitro','nook','obigo','palm','pg-',
'playstation','pocket','pt-','qc-','qtek','rover','sama','samu','sanyo','sch-','scooter',
'sec-','sendo','sgh-','siemens','sie-','softbank','sprint','spv','tablet','talkabout',
'tcl-','teleca','telit','tianyu','tim-','toshiba','tsm','utec','utstar','verykool','virgin',
'vk-','voda','voxtel','vx','wellco','wig browser','wii','wireless','xde','pad','gt-p1000'];
foreach ($mobile_agents as $device) {
if (stripos($user_agent, $device) !== false) {
return true;
}
}
return false;
}
随机字符串
/**
* 随机字符串
* @param int $len 位数
* @return bool|string
*/
protected function getRandStr($len = 32)
{
$str = "132sdadsafhfhgfgh3112421kkkkfdsdsfdsgasdsadsa21421321";
return substr(str_shuffle($str),0, $len);
}
php 图片处理
下载网络图片
/**
* 下载图片
* @param $url
* @param string $absolute_path
*/
private function download($url, $absolute_path = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // 请求地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 不直接输出信息
// 可以访问https请求
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$file = curl_exec($ch);
curl_close($ch);
$resource = fopen($absolute_path, 'a');
fwrite($resource, $file);
fclose($resource);
}
圆角处理
/**
* 图片圆角处理
*
* @param $src_img
* @param $width
* @param $height
* @param int $radius
* @return resource
*/
private function radius_img($src_img, $width, $height, $radius = 30) {
$w = &$width;
$h = &$height;
$img = imagecreatetruecolor($w, $h);
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $radius; //圆 角半径
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
//不在四角的范围内,直接画
imagesetpixel($img, $x, $y, $rgbColor);
} else {
//在四角的范围内选择画
//上左
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//上右
$y_x = $w - $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//下左
$y_x = $r; //圆心X坐标
$y_y = $h - $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//下右
$y_x = $w - $r; //圆心X坐标
$y_y = $h - $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
}
return $img;
}
裁剪和缩放图片
/**
* 裁剪图片和缩放图片
* @param $img 图片
* @param $old_width 裁剪后的宽
* @param $old_height 裁剪后的高
* @param $width 要裁剪的宽
* @param $height 要裁剪的高
* @param $x 裁剪的左上角的坐标x轴
* @param $y 裁剪的左上角的坐标y轴
* @return false|resource
*/
private function cropImage($img, $out_width, $out_height, $width, $height, $x, $y)
{
$new_image = imagecreatetruecolor($out_width, $out_height);
//背景,写入图片,位置,截取写入图片,写入图片的大小,写入图片的截取大小
imagecopyresized($new_image, $img, 0 ,0, $x, $y , $out_width, $out_height, $width, $height);//拷贝图片
return $new_image;
}
写入文字
//设置水印字体颜色
$color = imagecolorallocatealpha($bg,0, 0, 0, 1);
imagettftext($bg, 40, 0, 80, 450, $color, $font_path, $this->to_entities('测试'));
写入文字汉字需要处理
/**
* @param $string
* @return string
*/
private function to_entities($string)
{
$len = strlen($string);
$buf = "";
for ($i = 0; $i < $len; $i++) {
if (ord($string[$i]) <= 127) {
$buf .= $string[$i];
} else if (ord($string[$i]) < 192) {
//unexpected 2nd, 3rd or 4th byte
$buf .= "�";
} else if (ord($string[$i]) < 224) {
//first byte of 2-byte seq
$buf .= sprintf("&#%d;",
((ord($string[$i + 0]) & 31) << 6) +
(ord($string[$i + 1]) & 63)
);
$i += 1;
} else if (ord($string[$i]) < 240) {
//first byte of 3-byte seq
$buf .= sprintf("&#%d;",
((ord($string[$i + 0]) & 15) << 12) +
((ord($string[$i + 1]) & 63) << 6) +
(ord($string[$i + 2]) & 63)
);
$i += 2;
} else {
//first byte of 4-byte seq
$buf .= sprintf("&#%d;",
((ord($string[$i + 0]) & 7) << 18) +
((ord($string[$i + 1]) & 63) << 12) +
((ord($string[$i + 2]) & 63) << 6) +
(ord($string[$i + 3]) & 63)
);
$i += 3;
}
}
return $buf;
}
写入图片
$bgImg = "card.png";
$bg = imagecreatefrompng($bgImg);
$bgImgInfo = @getimagesize($bgImg);
//背景,写入图片,位置,截取写入图片,写入图片的大小,写入图片的截取大小
imagecopyresized($bg, $code, 428 ,540, 0, 0 , 200, 200, $codeImgInfo[0], $codeImgInfo[1]);//拷贝图片
图片写入
imagepng($bg, "a.png", 9);//写入图像
imagedestroy($bg);//摧毁图像
二维数组转树形结构, 要层级高的排在前面
/**
* 树形结构数组处理
* @param $data 二维数组
* @param $parentField 父级id的字段
* @param $name 名称字段
* @return array
*/
public function tree($data, $parentField, $name)
{
$tree = [];
$maps = array_column($data, NUll, 'id');
foreach ($maps as $k => &$v) {
// 判断是否存在parent
$parentId = $v[$parentField];
$v['title'] = $v[$name];
$v = ['title' => $v[$name], 'id' => $v['id'], 'field' => $name];
if ($parentId == 0) {
$tree[] = &$v;
} else {
if (isset($maps[$parentId])) {
$maps[$parentId]['children'][] = &$v;
}
}
}
return $tree;
}
统计字符串出现的次数substr_count
回调方法使用use
$a = 1;
test(function() use $a {
print_r($a)
})
提高php7性能,Opcache
https://www.laruence.com/2015/12/04/3086.html
目录分隔符
DIRECTORY_SEPARATOR
array_unshift导致数据键值重置,数组合并+