php公共方法集合

function v($msg)
{
    echo '
';
    var_dump($msg);
    exit;
}

function Pe($msg)
{
    if (is_array($msg)) {
        if (count($msg) < 2) {
            echo json_encode($msg, JSON_UNESCAPED_UNICODE);
        } else {
            foreach ($msg as $key => $value) {
                if (is_array($value)) {
                    $msg[$key] = json_encode($value, JSON_UNESCAPED_UNICODE);
                }
            }
            echo implode(' *** ', $msg);
        }
    } else {
        echo $msg;
    }
    echo '

'; } /** * 缩略图 * @param type $imagePath */ function PimageSL($imagePath, $xx = 100, $yy = 100) { $filePath = base_path() . "/public"; $rootPath = $filePath . $imagePath; if (file_exists($rootPath) && $imagePath) { $s_image = $imagePath . "_w{$xx}_h{$yy}.jpg"; if (!file_exists($filePath . $s_image)) { //获取图片信息 list($imgw, $imgh, $imgt, $attr) = getimagesize($rootPath); if ($imgw && $imgh) { if ($imgw > 0 && $imgh > 0) { //计算缩小的比例,目标最长边缩至150 $percent = $imgw > $imgh ? ($xx / $imgw) : ($yy / $imgh); //以最长边作为缩放参考 if ($percent < 1) { //计算缩略图的新尺寸 $new_width = floor($imgw * $percent); $new_height = floor($imgh * $percent); } else { //如果原图尺寸小于 150x150 直接输出原图尺寸 $new_width = $imgw; $new_height = $imgh; } $thumb = imagecreatetruecolor($new_width, $new_height); //读取图片 switch ($imgt) { //判断格式,图像类型,但缩略图输出的都是jpg..参考下文 case 1: $orgimg = imagecreatefromgif($rootPath); break; case 2: $orgimg = imagecreatefromjpeg($rootPath); break; case 3: $orgimg = imagecreatefrompng($rootPath); break; } //imagecopyresampled(缩略图片资源, 源图片资源, dx, dy, sx,sy, 新图像宽度, 新图像高度, 源图像宽度, 源图像高度); imagecopyresampled($thumb, $orgimg, 0, 0, 0, 0, $new_width, $new_height, $imgw, $imgh); //缩放核心函数 imagejpeg($thumb, $filePath . $s_image, 90); //输出图像 //销毁资源 imagedestroy($thumb); imagedestroy($orgimg); } } } return $s_image; } else { return $imagePath; } } /** * 图片缩放 * @param type $imagePath * @param type $width * @param type $height * @param type $pz */ function PimageCut($imagePath, $width = 100, $height = 100, $pz = 90) { //系统根目录 $filePath = base_path() . "/public"; $oldImagePath = $filePath . $imagePath; if (file_exists($oldImagePath) && $imagePath) { //图片新名字 $newImageName = "{$imagePath}_{$width}_{$height}.jpg"; //图片新的根路径 $newFilePath = $filePath . $newImageName; if (!file_exists($newFilePath)) { //获取图片信息 list($imgW, $imgH, $imgType, $attr) = getimagesize($oldImagePath); if ($imgW && $imgH) { //读取图片 switch ($imgType) { //判断格式,图像类型,但缩略图输出的都是jpg..参考下文 case 1: $oldImageObj = imagecreatefromgif($oldImagePath); break; case 2: $oldImageObj = imagecreatefromjpeg($oldImagePath); break; case 3: $oldImageObj = imagecreatefrompng($oldImagePath); break; } //计算缩小的比例,目标最长边缩至150 $percent = $imgW > $imgH ? ($width / $imgW) : ($height / $imgH); //以最长边作为缩放参考 if ($percent < 1) { //计算缩略图的新尺寸 $new_width = floor($imgW * $percent); $new_height = floor($imgH * $percent); $newImageObj = imagecreatetruecolor($new_width, $new_height); //imagecopyresampled(缩略图片资源, 源图片资源, dx, dy, sx,sy, 新图像宽度, 新图像高度, 源图像宽度, 源图像高度); imagecopyresampled($newImageObj, $oldImageObj, 0, 0, 0, 0, $new_width, $new_height, $imgW, $imgH); //缩放核心函数 imagedestroy($oldImageObj); $start_x = $new_width > $width ? (($new_width - $width) / 2) : 0; $start_y = $new_width > $width ? (($new_height - $height) / 2) : 0; } else { $newImageObj = $oldImageObj; $start_x = $imgW > $width ? (($imgW - $width) / 2) : 0; $start_y = $imgH > $width ? (($imgH - $height) / 2) : 0; } $imageCutObj = imagecreatetruecolor($width, $height); imagecopy($imageCutObj, $newImageObj, $start_x, $start_y, 0, 0, $width, $height); //保存图像 imagejpeg($imageCutObj, $newFilePath, $pz); //销毁资源 imagedestroy($newImageObj); imagedestroy($imageCutObj); } } echo $newImageName; } else { echo $imagePath; } } /** * 发布时间方案确认 * @param type $param * @return string */ function PtimeCode($param, $type = 0) { if ($type == 1) { $param = strtotime($param); } $now_time = time(); if ($now_time - $param < 60 * 3) { $putTime = '刚刚'; } else if ($now_time - $param < 60 * 60) { $putTime = ceil(($now_time - $param) / 60) . '分钟前'; } else if ($now_time - $param < 60 * 60 * 24) { $putTime = ceil(($now_time - $param) / 60 / 60) . '小时前'; } else if ($now_time - $param < 60 * 60 * 24 * 7) { $putTime = ceil(($now_time - $param) / 60 / 60 / 24) . '天前'; } else { // $putTime = ceil(($now_time - $param) / 60 / 60 / 24 / 30) . '月前'; $putTime = date('Y-m-d', $param); } return $putTime; } /** * ****************** * 1、写入内容到文件,追加内容到文件 * 2、打开并读取文件内容 * ******************* */ function saveLog($msg, $case = 'default') { $path = base_path() . "/storage/mylogs/{$case}/"; if (!is_dir($path)) { mkdir($path, 0777, true); } $filename = $path . date('Ymd') . '.txt'; $content = date("Y-m-d H:i:s") . " ==> " . $msg . "\r\n\r\n"; file_put_contents($filename, $content, FILE_APPEND); } /** * 公用的方法 返回json数据,进行信息的提示 * @param $code 状态码 * @param string $message 提示信息 * @param array $data 返回数据 * 1 */ function showMsg($code, $message = '', $data = array()) { $result = array( 'code' => $code, 'msg' => $message, 'data' => $data, ); exit(json_encode($result)); } //加密函数 function lockToken($txt = '', $key = 'bfyj') { if ($txt) { $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; $nh = rand(0, 64); $ch = $chars[$nh]; $mdKey = md5($key . $ch); $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7); $txt = base64_encode($txt); $tmp = ''; $i = 0; $j = 0; $k = 0; for ($i = 0; $i < strlen($txt); $i++) { $k = $k == strlen($mdKey) ? 0 : $k; $j = ($nh + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64; $tmp .= $chars[$j]; } return urlencode($ch . $tmp); } else { return false; } } //解密函数 function unlockToken($txt = '', $key = 'bfyj') { if ($txt) { $txt = urldecode($txt); $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; $ch = $txt[0]; $nh = strpos($chars, $ch); $mdKey = md5($key . $ch); $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7); $txt = substr($txt, 1); $tmp = ''; $i = 0; $j = 0; $k = 0; for ($i = 0; $i < strlen($txt); $i++) { $k = $k == strlen($mdKey) ? 0 : $k; $j = strpos($chars, $txt[$i]) - $nh - ord($mdKey[$k++]); while ($j < 0) { $j += 64; } $tmp .= $chars[$j]; } return base64_decode($tmp); } else { return false; } } /** * @Author Afree * @DateTime 2018-04-18 * @copyright [获取设计订单号] * @license [license] * @version [version] * @param [type] $user_id [description] * @return [type] [description] */ function pbGetOrderSN($user_id) { $user_uid = 10000000 + $user_id; $nowTime = time(); $date = date('Ymd', $nowTime); $timestr = substr($nowTime, -4); $rand = rand(10, 99); $userstr = substr($user_uid, -4); return $date . $timestr . $rand . $userstr; } /** * @Author Afree * @DateTime 2018-04-18 * @copyright [获取工厂传单订单号] * @license [license] * @version [version] * @param [type] $user_id [description] * @return [type] [description] */ function pbGetFactorySN($user_id) { $user_uid = 10000000 + $user_id; $nowTime = time(); $date = date('Ymd', $nowTime); $rand = rand(10, 99); $timestr = substr($nowTime, -2); $userstr = substr($user_uid, -2); return $date . $userstr . $timestr . $rand; } /** * 酷家乐api curl组合 * @param $url * @param array $data * @return mixed * 模拟发送get 和 post 请求 */ function pbKapiCurlAsk($url, $data = '', $type = "GET") { set_time_limit(0); //执行时间无限 ini_set('memory_limit', '-1'); //内存无限 $ch = curl_init(); curl_setopt($ch, CURLOPT_TIMEOUT, 600); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_HEADER, 0); $type = strtolower($type); switch ($type) { case 'post': $header = array( // 'Content-Type: text/plain;charset=utf-8', 'Content-Type: application/json;charset=utf-8', "X-AjaxPro-Method:ShowList", ); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //post请求配置 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); break; case 'text': $header = array( 'Content-Type: text/plain;charset=utf-8', // 'Content-Type: application/json;charset=utf-8', "X-AjaxPro-Method:ShowList", ); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //post请求配置 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); break; default: $header = array( 'Content-Type: text/plain;charset=utf-8', ); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); break; } $result = curl_exec($ch); curl_close($ch); return json_decode($result, true); } /** * 清空文件夹函数和清空文件夹后删除空文件夹函数的处理 * @param type $path */ function pbDelDirFile($path) { //如果是目录则继续 if (is_dir($path)) { //扫描一个文件夹内的所有文件夹和文件并返回数组 $p = scandir($path); foreach ($p as $val) { //排除目录中的.和.. if ($val != "." && $val != "..") { //如果是目录则递归子目录,继续操作 if (is_dir($path . $val)) { //子目录中操作删除文件夹和文件 pbDelDirFile($path . $val . '/'); //目录清空后删除空文件夹 @rmdir($path . $val . '/'); } else { //如果是文件直接删除 unlink($path . $val); } } } } } /** * 获取文章摘要 * @param type $data * @param type $cut * @param type $str * @return type */ function PcutArticle($data, $cut = 140, $str = "....") { $data = strip_tags($data); //去除html标记 $pattern = "/&[a-zA-Z]+;/"; //去除特殊符号 $data = preg_replace($pattern, '', $data); if (!is_numeric($cut)) { return $data; } if ($cut > 0) { $data = mb_strimwidth($data, 0, $cut, $str); } return $data; } /** * 作用:将xml转为array */ function PxmlToArray($xml) { //将XML转为array libxml_disable_entity_loader(true); $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $values; } /** * 作用:array转xml */ function ParrayToXml($arr) { $xml = ""; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . ""; } else { $xml .= "<" . $key . ">"; } } $xml .= ""; return $xml; } /** * 随机生成安全符号 登录校验 * @param int $length * @return string */ function pbGetRandStr($length = 4) { // 密码字符集,可任意添加你需要的字符 $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); // 在 $chars 中随机取 $length 个数组元素键名 $keys = array_rand($chars, $length); $password = ''; for ($i = 0; $i < $length; $i++) { // 将 $length 个数组元素连接成字符串 $password .= $chars[$keys[$i]]; } return $password; } /** * 过滤接收数据 * @param [array] $data [数据] * @param [boole] $unset [是否删除空值] * @return [array] [description] */ function dataFilterNull($data, $unset = false) { foreach ($data as $key => &$val) { if (is_array($val)) { $val = array_filter($val); } else { # 值为空,是否删除空值 if ($val == null && !$unset) { return false; } elseif ($val == null && $unset) { unset($data[$key]); } } } return $data; } /** * 根据字段重新组合数组(二维) * @param type $array 二维数组 * @param type $field 字段 * @return type */ function pdFieldRegroupArr($array, $field) { $list = $temp = array(); if (!empty($array)) { # 提取字段 foreach ($array as $v) { $v = $v[$field]; $temp[] = $v; } # 去除重复字段 $temp = array_unique($temp); // 组合 foreach ($temp as $k => $v) { foreach ($array as $kk => $vv) { // var_dump($vv['date'], $v); if ($vv['date'] == $v) { $list[$v][] = $vv; } } } } return $list; }

你可能感兴趣的:(php公共方法集合)