php常用类


<?php
/**
* @author Abin
*/
defined('BASEPATH') or exit('No direct script access allowed');
require './application/libraries/Medoo.php';
use Firebase\JWT\JWT;
use Medoo\Medoo;

function jsonExit($status = '', $desc = '', $content = null, $more = null)
{
    $data['status'] = $status;
    $data['desc'] = $desc;
    if (isset($content) && $content !== null && $content !== 'undefined') {
        $data['content'] = $content;
    }
    if (isset($more) && $more !== null && $more !== 'undefined') {
        $data += $more;
    }
    echo json_encode($data);
    exit;
}

function secretKey()
{
    return 'v2ILfnbsxgULEWBdAACa';
}

function setJWT($content)
{
    $key = secretKey();
    $prop = [
        'iat' => time(),
        'exp' => time() + 60,
    ];

    $array = array_merge($prop, $content);
    $data = JWT::encode($array, $key);

    return $data;
}

//得到jwt里面id的方法
function getJWT($data)
{
    $key = secretKey();
    $decoded = JWT::decode($data, $key, array('HS256'));

    return $decoded;
}

function getInput()
{
    return json_decode(trim(file_get_contents('php://input')), true);
}

function filtInput($str)
{
    return trim(htmlspecialchars($str));
}

function getCaptcha()
{
    // error_reporting(E_ALL);
    putenv('GDFONTPATH='.realpath('.'));
    $font = './application/helpers/Arial-Black.ttf';
    $string = 'abcdefghijkLmnpqrstuvwxyz123456789';
    $str = '';
    for ($i = 0; $i < 4; ++$i) {
        $pos = rand(0, 33);
        $str .= $string[$pos];
    }
    if (!session_id()) {
        session_start();
    }
    $_SESSION['captcha'] = strtolower($str);
    $img_handle = imagecreate(90, 28);
    $back_color = imagecolorallocate($img_handle, 45, 140, 240);
    $txt_color = imagecolorallocate($img_handle, 255, 255, 255);
    // 加入干扰线
    for ($i = 0; $i < 3; ++$i) {
        $line = imagecolorallocate($img_handle, rand(200, 255), rand(200, 255), rand(200, 255));
        imageline($img_handle, 0, rand(0, 28), 130, rand(0, 28), $line);
    }
    // 加入干扰象素
    for ($i = 0; $i < 400; ++$i) {
        $randcolor = imagecolorallocate($img_handle, 29, 55, 110);
        imagesetpixel($img_handle, rand() % 130, rand() % 30, $randcolor);
    }
    imagefill($img_handle, 0, 0, $back_color);
    imagettftext($img_handle, 20, 0, 4, 21, $txt_color, $font, $str);
    ob_clean();
    header('Content-Type: image/png;');
    imagepng($img_handle);
    imagedestroy($img_handle);
}

function initMedoo($dbname)
{
    return new Medoo([
        // required
        'database_type' => 'mysql',
        'database_name' => $dbname,
        'server' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        // 'port' => 3306,
    ]);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。