超灬超  

    /**
     * 验证手机号
     */
    function isPhone($str)
    {

        if (preg_match("/^1[3456789]\d{9}$/", $str)) {
            return true;
        } else {
            return false;
        }
    }

 

 /**
     * 判断是否为合法的身份证号码
     * @param $mobile
     * @return int
     */
    function isIdcard($vStr)
    {
        $vCity = array(
            '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'
        );
        if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;
        if (!in_array(substr($vStr, 0, 2), $vCity)) return false;
        $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
        $vLength = strlen($vStr);
        if ($vLength == 18) {
            $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
        } else {
            $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
        }
        if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
        if ($vLength == 18) {
            $vSum = 0;
            for ($i = 17; $i >= 0; $i--) {
                $vSubStr = substr($vStr, 17 - $i, 1);
                $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr, 11));
            }
            if ($vSum % 11 != 1) return false;
        }
        return true;
    }

  


    /**
     * 根据IP获取省份 BAIDU
     */
    public function get_province_by_ip()
    {
        //  return [];
        $ip = $_SERVER['REMOTE_ADDR'];
        // $ip = '36.5.106.8';

        $ak = "KEY";
        $url = "http://api.map.baidu.com/location/ip?ak=$ak&ip=$ip&coor=bd09ll";
        $re = file_get_contents($url);
        $re = json_decode($re);

        if ($re->status == 0 && $re->address) {
            $address_arr = explode('|', $re->address);
            return $address_arr;

        } else {
            return [];
        }


    }

  

/**
* 用户设备类型
* @return string
*/
    function clientOS()
    {

        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);

        /*echo '<pre>';
        print_r($agent);
        exit();*/

        if (strpos($agent, 'windows nt')) {
            $platform = 'windows';
        } elseif (strpos($agent, 'macintosh')) {
            $platform = 'mac';
        } elseif (strpos($agent, 'ipod')) {
            $platform = 'ipod';
        } elseif (strpos($agent, 'ipad')) {
            $platform = 'ipad';
        } elseif (strpos($agent, 'iphone')) {
            $platform = 'iphone';
        } elseif (strpos($agent, 'android')) {
            $platform = 'android';
        } elseif (strpos($agent, 'unix')) {
            $platform = 'unix';
        } elseif (strpos($agent, 'linux')) {
            $platform = 'linux';
        } elseif (strpos($agent, 'mac')) {
            $platform = 'mac';
        } else {
            $platform = 'other';
        }

        return $platform;
    }

  

/**
* 获取分页数据
* 1.共多少页
* 3是否有上一页
* 4.是否有下一页
*/
public function page($total, $page, $size)
{
    $page_number = ceil($total / $size);

    $pre = 1;
    $next = 1;
    if ($page == 0) {
        $page = 1;
    }

    if ($page == 1) {
        $pre = 0;
    }
    if ($page >= $page_number) {
        $next = 0;
    }

    /**
     * -----------计算展示页数哎---------
     * 取当前分页的前五页 以及 后五页  当然实际要区分 后五页 是否大于总页数  当前页是否大于5
     */
    $page_list = [];
    if ($page_number < 5) {
        for ($i = 1; $i < $page_number; $i++) {
            $page_list[] = $i;
        }
    } else if ($page_number > 5 && $page_number < 10) {
        for ($i = 1; $i < $page_number; $i++) {
            $page_list[] = $i;
        }
    } else if ($page_number > 10) {
        if ($page < 5) {
            for ($i = 1; $i < 8; $i++) {
                $page_list[] = $i;
            }
        } else if ($page_number == $page) {
            for ($i = $page - 9; $i < $page; $i++) {
                $page_list[] = $i;
            }
        } else {
            for ($i = $page - 3; $i < $page + 3; $i++) {
                $page_list[] = $i;
            }
        }
        $page_list[] = '...';
        $page_list[] = $page_number;
    }

    return
        [
            'total' => $total,
            'page' => $page,
            'pn' => $page_number,
            'pre' => $pre,
            'next' => $next,
            'size' => $size,
            'pl' => $page_list
        ];
}

  

  
    /*判断是否微信环境 可以区分系企业微信*/
    function is_weixin()
    {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {

            // 是否在微信环境下
            if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false
                && strpos($_SERVER['HTTP_USER_AGENT'], 'wxwork') == false) {
                return true;
            } else {
                return false;
            }

        }
        return false;
    }

  

/**
* 富文本图片通用图片样式整改
* @param $activity
*/
    public function set_img_style(&$activity)
    {
        $arr = ['guest','detail','content','notice','corporation'];

        foreach ($arr as $a){
            if($activity[$a]??''){
                $activity[$a] = htmlspecialchars_decode($activity[$a]);
                $activity[$a] = preg_replace("/max-width: [0-9]*px;/", "max-width:100%;", $activity[$a]);
                //$activity['guest'] = preg_replace("/width=\"[0-9]*\"/", "", $activity['guest']);
                $activity[$a] = preg_replace("/height=\"[0-9]*\"/", "", $activity[$a]);
                $activity[$a] = preg_replace("/height: [0-9]*px;/", "height:auto!important;", $activity[$a]);
                $activity[$a] = str_replace('style="', 'style="max-width:100%;', $activity[$a]);
            }
        }

    }

  

    /**蜘蛛排查*/
    function isCrawler()
    {
        echo $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if (!empty($agent)) {
            $spiderSite = array(
                "TencentTraveler",
                "Baiduspider+",
                "BaiduGame",
                "Googlebot",
                "msnbot",
                "Sosospider+",
                "Sogou web spider",
                "ia_archiver",
                "Yahoo! Slurp",
                "YoudaoBot",
                "Yahoo Slurp",
                "MSNBot",
                "Java (Often spam bot)",
                "BaiDuSpider",
                "Voila",
                "Yandex bot",
                "BSpider",
                "twiceler",
                "Sogou Spider",
                "Speedy Spider",
                "Google AdSense",
                "Heritrix",
                "Python-urllib",
                "Alexa (IA Archiver)",
                "Ask",
                "Exabot",
                "Custo",
                "OutfoxBot/YodaoBot",
                "yacy",
                "SurveyBot",
                "legs",
                "lwp-trivial",
                "Nutch",
                "StackRambler",
                "The web archive (IA Archiver)",
                "Perl tool",
                "MJ12bot",
                "Netcraft",
                "MSIECrawler",
                "WGet tools",
                "larbin",
                "Fish search",
            );
            foreach ($spiderSite as $val) {
                $str = strtolower($val);
                if (strpos($agent, $str) !== false) {
                    return true;
                }
            }
        } else {
            return false;
        }
    }

  

/*检测并禁止频繁操作*/
    public function check_repeat($name,$time=5)
    {
        $key = ($this->user['id']??get_client_ip()) . $name;
        if (Cache::get($key)) {
            $this->result([], 0, '请勿操作过于频繁', 'json');
        } else {
            Cache::set($key, 1, $time);
        }
    }

  绘图相关

if (!function_exists('gene_poster')) {

    function gene_poster($param)
    {
        $user = User::where(['id' => $param['user_id']])->find();
        if ($user['show_mobile'] == 2) {
            $user['mobile'] = "无";
        }
        // echo "<script>alert('首次生成海报可能比较缓慢,请耐心等待-')</script>";
        $background = $param['background'];
        $nickname = $user['nickname'];
        if (mb_strlen($nickname) >= 7) {
            $nickname = mb_substr($nickname, 0, 6) . "...";
        }
        $mobile = $user['mobile'];
        $avatar = "../public" . $user['avatar'];
        $qrcode_file_name = $param['qrcode'];
        switch ($param['type']) {
            case 1:
                $poster_path = "../public/uploads/" . date('Ymd') . "/poster/";
                break;
            case 2:
                $poster_path = "../public/uploads/" . date('Ymd') . "/lessonposter/";
                break;
            case 3:
                $poster_path = "../public/uploads/" . date('Ymd') . "/memberposter/";
                break;
        }
        // $poster_path = "../public/uploads/" . date('Ymd') . "/poster/";

        if (!is_dir($poster_path)) {
            mkdir($poster_path, 0777, true);
        }


        //$font_path = '../public/assets/fonts/captcha.ttf';
        $font_path = '../public/assets/fonts/fangzhengheitijianti.TTF';
        //$font_path = 'D:\WORK\project\kangaroocollege\public\assets\fonts\fangzhengheitijianti.TTF';

        if ($param['type'] == 2) {
            $config = array(
                'image' => array(

                    array(
                        'url' => $qrcode_file_name,
                        'left' => 532,
                        'top' => 1136,
                        'right' => 0,
                        'stream' => 0,
                        'bottom' => 0,
                        'width' => 150,
                        'height' => 150,
                        'opacity' => 100
                    ),

                ),


                'background' => $background,
            );
        } else {

            if (is_file($avatar) && filesize($avatar) > 0) {
                $config = array(
                    'image' => array(
                        array(
                            'url' => $avatar,       //图片资源路径
                            //'url' => border_radius($avatar, $user),       //图片资源路径
                            'left' => 70,
                            'top' => 1070,
                            'stream' => 0,             //图片资源是否是字符串图像流
                            'right' => 0,
                            'bottom' => 0,
                            'width' => 110,
                            'height' => 110,
                            'opacity' => 100
                        ),
                        array(
                            'url' => $qrcode_file_name,
                            'left' => 530,
                            'top' => 1070,
                            'right' => 0,
                            'stream' => 0,
                            'bottom' => 0,
                            'width' => 150,
                            'height' => 150,
                            'opacity' => 100
                        ),

                    ),
                    'text' => array(
                        array(
                            'text' => $nickname,
                            'left' => 210,
                            'top' => 1100,
                            'fontPath' => $font_path,     //字体文件
                            'fontSize' => 30,             //字号
                            'fontColor' => '255,255,255',       //字体颜色
                            'angle' => 0,
                        ),

                        array(
                            'text' => "电话:" . $mobile,
                            'left' => 210,
                            'top' => 1170,
                            'fontPath' => $font_path,     //字体文件
                            'fontSize' => 25,             //字号
                            'fontColor' => '255,255,255',       //字体颜色
                            'angle' => 0,
                        ),

                        array(
                            'text' => '我是你的专属客服',
                            'left' => 80,
                            'top' => 1220,
                            'fontPath' => $font_path,     //字体文件
                            'fontSize' => 20,             //字号
                            'fontColor' => '255,255,255',       //字体颜色
                            'angle' => 0,
                        ),
                        array(
                            'text' => '您有任何问题可向我咨询.',
                            'left' => 80,
                            'top' => 1260,
                            'fontPath' => $font_path,     //字体文件
                            'fontSize' => 20,             //字号
                            'fontColor' => '255,255,255',       //字体颜色
                            'angle' => 0,
                        )
                    ),

                    'background' => $background,
                );
            } else {
                User::where(['id' => $param['user_id']])->update(['avatar' => '']);

                $config = array(
                    'image' => array(

                        array(
                            'url' => $qrcode_file_name,
                            'left' => 530,
                            'top' => 1070,
                            'right' => 0,
                            'stream' => 0,
                            'bottom' => 0,
                            'width' => 150,
                            'height' => 150,
                            'opacity' => 100
                        ),

                    ),
                    'text' => array(
                        array(
                            'text' => $nickname,
                            'left' => 210,
                            'top' => 1100,
                            'fontPath' => $font_path,     //字体文件
                            'fontSize' => 30,             //字号
                            'fontColor' => '255,255,255',       //字体颜色
                            'angle' => 0,
                        ),

                        array(
                            'text' => "电话:" . $mobile,
                            'left' => 210,
                            'top' => 1170,
                            'fontPath' => $font_path,     //字体文件
                            'fontSize' => 25,             //字号
                            'fontColor' => '255,255,255',       //字体颜色
                            'angle' => 0,
                        ),

                        array(
                            'text' => '我是你的专属客服',
                            'left' => 80,
                            'top' => 1220,
                            'fontPath' => $font_path,     //字体文件
                            'fontSize' => 20,             //字号
                            'fontColor' => '255,255,255',       //字体颜色
                            'angle' => 0,
                        ),
                        array(
                            'text' => '您有任何问题可向我咨询.',
                            'left' => 80,
                            'top' => 1260,
                            'fontPath' => $font_path,     //字体文件
                            'fontSize' => 20,             //字号
                            'fontColor' => '255,255,255',       //字体颜色
                            'angle' => 0,
                        )
                    ),

                    'background' => $background,
                );
            }


        }

        /*2画图 保存*/

        $post_file_name = $poster_path . date('YmdHis') . $param['user_id'] . ".png";
//echo createPoster($config,$filename);

        return createPoster($config, $post_file_name);

    }
}

/**
 * 生成宣传海报
 * @param array  参数,包括图片和文字
 * @param string $filename 生成海报文件名,不传此参数则不生成文件,直接输出图片
 * @return bool|string [type] [description]
 */
function createPoster($config = array(), $filename = "")
{
//    echo '<pre>';
//    print_r($config);
//  //  print_r($config['background']);
//    exit;
    //如果要看报什么错,可以先注释调这个header
    //if (empty($filename)) header("content-type: image/png");
    $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);
    $backgroundFun = 'imagecreatefrom' . image_type_to_extension($backgroundInfo[2], false);
    $background = $backgroundFun($background);
    $backgroundWidth = imagesx($background);  //背景宽度
    $backgroundHeight = imagesy($background);  //背景高度
    register_shutdown_function('shutdown_function');
    $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
    $color = imagecolorallocate($imageRes, 0, 0, 0);
    imagefill($imageRes, 0, 0, $color);
    // imageColorTransparent($imageRes, $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);

            /* --- 用以处理缩放png图透明背景变黑色问题 开始 --- */
            if ($key == 0) {
                $color = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
                imagecolortransparent($canvas, $color);
                imagefill($canvas, 0, 0, $color);
            }
            /* --- 用以处理缩放png图透明背景变黑色问题 结束 --- */


            //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
            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']);//左,上,右,下,宽度,高度,透明度
        }
    }
    /*echo '<pre>';
    print_r(0);
    exit;*/
    //处理文字
    if (!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'];

//            echo '<pre>';
//            print_r($val['fontPath']);
//            print_r(is_file($val['fontPath']));
//            exit;

            imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
        }
    }

    //这一句一定要有
    imagealphablending($imageRes, false);

    imagesavealpha($imageRes, true);
    //生成图片
    if (!empty($filename)) {
        $res = imagejpeg($imageRes, $filename, 100); //保存到本地
        imagedestroy($imageRes);
        if (!$res) return false;
        return $filename;
    } else {
        imagejpeg($imageRes);     //在浏览器上显示
        imagedestroy($imageRes);
    }
}

/**
 *图片修圆角
 * @param $imgpath
 * @param $user
 * @return string
 */
function border_radius($imgpath, $user)
{
    $radius = 1;
    $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];
    // $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
    $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);

                }

            }

        }

    }

    $avatar_path = "../public/uploads/" . date('Ymd') . "/avatar/";


    if (!is_dir($avatar_path)) {
        mkdir($avatar_path, 0777, true);
    }
//    echo '<pre>';
//    print_r($img);
//    exit;
    //  file_put_contents($avatar_path.'avatar'.date('Ymd').".jpg",$img);
    $filename = $avatar_path . 'avatar' . date('YmdHis') . $user['id'] . ".jpg";
    header("content-type:image/jpeg");
    imagejpeg($img, $filename, 100); //保存到本地
    /*  imagepng($img);
      imagedestroy($img);
      exit;*/
    return $filename;
}

  

posted on 2021-01-07 10:09  超灬超  阅读(93)  评论(0编辑  收藏  举报