示例#1
0
function qrcode($lang, $arglist = false)
{
    $s = false;
    $size = 1;
    $margin = 0;
    $fg = $bg = false;
    $quality = 'M';
    $token = false;
    $qs = array('L', 'M', 'Q', 'H');
    $with_token = false;
    if (is_array($arglist)) {
        if (isset($arglist['s'])) {
            $s = $arglist['s'];
        }
        if (isset($arglist['fg'])) {
            $fg = $arglist['fg'];
        }
        if (isset($arglist['bg'])) {
            $bg = $arglist['bg'];
        }
        if (isset($arglist['quality'])) {
            $quality = $arglist['quality'];
        }
        if (isset($arglist['size'])) {
            $size = $arglist['size'];
        }
        if (isset($arglist['margin'])) {
            $margin = $arglist['margin'];
        }
        if ($with_token) {
            if (isset($arglist['token'])) {
                $token = $arglist['token'];
            }
        }
    }
    if ($with_token) {
        if (!isset($_SESSION['qrcode_token']) or $token != $_SESSION['qrcode_token']) {
            return run('error/badrequest', $lang);
        }
        unset($_SESSION['qrcode_token']);
    }
    if (!$s or !is_numeric($size) or !is_numeric($margin) or !$quality or !in_array($quality, $qs) or $fg and !validate_color($fg) or $bg and !validate_color($bg)) {
        return run('error/badrequest', $lang);
    }
    if ($size < 1) {
        $size = 1;
    }
    if ($margin < 0) {
        $margin = 0;
    }
    $png = qrencode($s, $size, $quality, $fg, $bg, $margin);
    if (!$png) {
        return run('error/internalerror', $lang);
    }
    header('Content-Type: image/png');
    header("Content-Disposition: inline; filename=qr.png");
    echo $png;
    return false;
}
示例#2
0
function qrencode($s, $size = 1, $quality = 'M', $fg = false, $bg = false, $margin = 0)
{
    $ql = array('L' => QR_ECLEVEL_L, 'M' => QR_ECLEVEL_M, 'Q' => QR_ECLEVEL_Q, 'H' => QR_ECLEVEL_H);
    if (!$s or !is_numeric($size) or !is_numeric($margin) or !$quality or !array_key_exists($quality, $ql) or $fg and !validate_color($fg) or $bg and !validate_color($bg)) {
        return false;
    }
    if ($size < 1) {
        $size = 1;
    }
    if ($margin < 0) {
        $margin = 0;
    }
    $q = $ql[$quality];
    $frame = @QRcode::text($s, false, $q, 1, 0);
    if (!$frame) {
        return false;
    }
    $h = count($frame);
    $w = strlen($frame[0]);
    $img = imagecreatetruecolor($w + 2 * $margin, $h);
    $qrimg = imagecreatetruecolor($w * $size + 2 * $margin, $h * $size + 2 * $margin);
    if ($bg) {
        $rgb = str_split($bg[0] == '#' ? substr($bg, 1, 6) : $bg, 2);
        $r = hexdec($rgb[0]);
        $g = hexdec($rgb[1]);
        $b = hexdec($rgb[2]);
    } else {
        $r = $g = $b = 255;
    }
    $bg = imagecolorallocate($img, $r, $g, $b);
    imagefill($img, 0, 0, $bg);
    $bg = imagecolorallocate($qrimg, $r, $g, $b);
    imagefill($qrimg, 0, 0, $bg);
    if ($fg) {
        $rgb = str_split($fg[0] == '#' ? substr($fg, 1, 6) : $fg, 2);
        $r = hexdec($rgb[0]);
        $g = hexdec($rgb[1]);
        $b = hexdec($rgb[2]);
    } else {
        $r = $g = $b = 0;
    }
    $fg = imagecolorallocate($img, $r, $g, $b);
    for ($y = 0; $y < $h; $y++) {
        for ($x = 0; $x < $w; $x++) {
            if ($frame[$y][$x] == '1') {
                imagesetpixel($img, $x, $y, $fg);
            }
        }
    }
    imagecopyresized($qrimg, $img, $margin, $margin, 0, 0, $w * $size, $h * $size, $w, $h);
    imagedestroy($img);
    ob_start();
    imagepng($qrimg);
    $png = ob_get_clean();
    imagedestroy($qrimg);
    return $png;
}