zoukankan      html  css  js  c++  java
  • PHP生成二维码

    一、使用谷歌Chart API生成URL二维码
    完整的API地址:
    https://chart.googleapis.com/chart?cht=qr&chs=宽x高&chld=等级|边距&choe=字符编码&chl=内容
    参数说明:
    cht - 指定一个QR码
    chs - 图像大小,单位是像素,是宽x高
    choe - 字符编码,默认UTF-8
    chld - 分两部分,格式为等级 | 边距。前者是错误校正等级,默认7% L-默认纠错水平,可以识别已损失的7%的数据;M-可以识别已损失15%的数据;Q-可以识别已损失25%的数据;H-可以识别已损失30%的数据;后者是生成的二维码离图片边框的距离,可自行调节。
    chl - 生成二维码的内容。包含中文时需使用UTF-8编码汉字,否则将出现问题。
    实现:
    function createQRbyGoogle($data, $width = '200', $height ='200', $choe = 'UTF-8', $EC_level = 'L', $margin= '0') {
        $chl = urlencode($data);
        return '<img src="http://chart.apis.google.com/chart?cht=qr&chs='.$width.'x'.$height. '&choe='.$choe .'&chld='.$EC_level.'|'.$margin.'&chl='.$chl . '" />';
    }
     
    $url = 'https://secure.php.net/';
    $qrcode = createQRbyGoogle($url);
    echo $qrcode;    // 直接在浏览器中显示二维码
     
    二、使用PHP QRCode类库生成二维码
    前提:PHP需开启GD2扩展。
    从官网http://phpqrcode.sourceforge.net/下载PHP QR Code类库,目录结构如下:
    使用时引用phpqrcode.php即可。
    phpqrcode.php提供了一个方法png(),代码如下:
    public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint = false)
    {
        $enc = QRencode::factory($level, $size, $margin);
        return $enc->encodePNG($text, $outfile, $saveandprint=false);
    }
    其中:
    $text - 生成二维码的的信息文本;
    $outfile - 是否输出二维码图片文件,默认否(在浏览器中显示);
    $level - 容错率,也就是有被覆盖的区域还能识别,分别是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%);
    $size - 生成图片大小,默认是3;
    $margin - 二维码周围边框空白区域间距值;
    $saveandprint - 是否保存二维码并显示,默认为false(不是很理解这个参数的作用,改为true也没见其他二维码生成)。
    实现:
    ① 浏览器输出二维码:
    include "phpqrcode/phpqrcode.php";
    $errorCorrectionLevel = "L";
    $matrixPointSize = "4";
    QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
     
    ② 文件输出二维码(浏览器不显示):
    include "phpqrcode/phpqrcode.php";
    $filename = 'qrcode_' . time();
    $errorCorrectionLevel = "L";
    $matrixPointSize = "4";
    $margin = 2;
    QRcode::png($value, $filename, $errorCorrectionLevel, $matrixPointSize, $margin);
     
    ③ 生成带logo的二维码:
    先使用PHP QR Code生成一张二维码图片,再利用PHP的image相关函数,将事先准备好的logo图片加入到刚生成的原始二维码图片中间,重新生成一张新的二维码图片。
    include('phpqrcode/phpqrcode.php');
    $filename = 'qrcode_' . time() . '.png';
    $errorCorrectionLevel = 'L';
    $matrixPointSize = 10;
    $margin = 2;
    QRcode::png($value, $filename, $errorCorrectionLevel, $matrixPointSize, $margin);   // 这里参数filename不能为false,必须输出二维码文件
    $logo = 'logo.jpg'; //logo的图片地址
    $QR = $filename;    //前面生成二维码图片的地址
    if($logo !== FALSE){
        $QR = imagecreatefromstring(file_get_contents($QR));
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);
        $QR_height = imagesy($QR);
        $logo_width = imagesx($logo);
        $logo_height = imagesy($logo);
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width / $logo_qr_width;
        $logo_qr_height = $logo_height / $scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
    }
    $finalName = 'final.png'; // 带logo二维码的文件名
    imagepng($QR, $finalName);  // 最后生成两个二维码,一个为先前生成的不带logo的二维码,如qrcode_1505115861.png,一个为带logo的二维码,即final.png
    上述代码最后生成两个二维码,一个为不带logo的二维码,如qrcode_1505115861.png,一个为带logo的二维码,即final.png。
    如果想在浏览器中显示二维码,则将上述代码中最后两句改为:
    header("Content-type:image/png");
    imagepng($QR);
    则在浏览器中显示带logo的二维码,但依然会生成一个不带logo的如qrcode_1505115861.png的二维码。
    原文:https://www.cnblogs.com/sunshineliulu/p/7505591.html
  • 相关阅读:
    SQL性能优化思路
    EF Migraiton错误解决
    How to resolve the 403 error when send POST request from Postman
    Disable trigger to avoid the ID is auto-updated
    MBG(Mybatis Generator)配置
    Publish Web Site To IIS From VS
    quickSort算法导论版实现
    Clang与libc++abi库安装
    Clang与libc++abi库安装
    整数中1 的个数
  • 原文地址:https://www.cnblogs.com/ivy-zheng/p/10939324.html
Copyright © 2011-2022 走看看