Home >PHP Framework >ThinkPHP >About the generation method of thinkphp5 QR code in xampp environment

About the generation method of thinkphp5 QR code in xampp environment

藏色散人
藏色散人forward
2021-02-15 10:13:053163browse

Generation of thinkphp5 QR code in xampp environment

Preface

Due to the functional needs of the team, we need to use PHP to make a QR code based on Our action of generating a QR code from the given URL is a tortuous process. I will summarize it here after it is completed.

Start

At the beginning, I went in the wrong direction. My thinking at the time: Since you want to generate a QR code, you need to know how to generate a QR code. mechanism, so I searched for QR codes on the Internet and got the following results:
About the generation method of thinkphp5 QR code in xampp environment

我粗略读了一遍之后顿悟:
显然,这不是我想要的(如果研究这东西,估计未来几周不用干别的了)

On the right track

Although I went in the wrong direction, I still understand it simply Now that we know the QR code, how can we implement the functions we need? This time I decided to be simple and crude

About the generation method of thinkphp5 QR code in xampp environment

Google was honest with me. Someone had already written these functions before me, so I started to get on the right track.

Overall idea

  • The functions we need have been written in PHP on the Internet. Just like we quoted the library in think, we only need to generate QR codes online Download the library and then reference it to generate a QR code quickly and conveniently. With the idea settled, let’s start working.

The author found two methods, and finally used the second one

Method 1: Composer and endroid/QrCode

  • Mentioned php download section For third-party libraries, we must mention composer. This is a powerful library management tool in PHP that can help us download other libraries on the Internet. This magical thing is also introduced in the official documentation (as shown below)

About the generation method of thinkphp5 QR code in xampp environment

  • Currently, the author is using the windows environment. I downloaded it according to the link in the official document and started happily going all the way to next. Then the problem came

About the generation method of thinkphp5 QR code in xampp environment

This is a photo I found online (from the novice tutorial: composer installation). When I installed it, no address appeared, so I was confused. This is What's the meaning?

After thinking about it, the management tool we installed is related to php, and we need to find the php.exe file in the novice tutorial picture, which leads to the following question: Where is php?

  • XAMPP

    Baidu Encyclopedia: XAMPP (Apache MySQL PHP PERL) is a powerful website building integration software package. It can be seen that the xampp we use has PHP integrated in it. Next, start looking for files
    About the generation method of thinkphp5 QR code in xampp environment

找到文件,安装完成,在我们的thinphp5文件夹下,输入指令来安装好第三方库

    $ composer require endroid/qr-code

In this way, our library file After loading, if you want to develop code, you can refer to this blog

http://www.cnbreak.org/biancheng/thinkphp5/wechatwebpage/760.html

Method 2: Use phpqrcode

The author uses this method, and it is relatively simple to use (but it seems that it cannot be used in thinkphp6). We download the third-party library directly from the Internet

Download link https:// sourceforge.net/projects/phpqrcode/files/releases
Official documentation http://phpqrcode.sourceforge.net/docs/html/annotated.html

After downloading
About the generation method of thinkphp5 QR code in xampp environment

Note: It must be in the extends directory
The library we downloaded with composer is in vender, and the third-party library downloaded from outside will only work if it is placed in extends.
The following is the code for the second method

function qrcode($level = 'L', $size = 4){
    // 导入Phpqrcode插件文件require_once EXTEND_PATH.'phpqrcode/phpqrcode.php';
    
    //设置url
    $url1 = 'https://www.baidu.com/s?wd=666&rsv_spt=1&rsv_iqid=0xfea0cab90000241d&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=ib&rsv_sug3=4&rsv_sug1=3&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=528&rsv_sug4=1203';

    // 容错级别
    $errorCorrectionLevel = $level;
    // 生成图片大小
    $matrixPointSize = intval($size);
    // 生成二维码图片
    $object = new QRcode();
    // 这个一定要加上,清除缓冲区
    ob_end_clean();
    // 第二个参数false的意思是不生成图片文件,如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件
    $object->png($url1, false, $errorCorrectionLevel, $matrixPointSize, 2);
}

The meaning of the relevant parameters:

About the generation method of thinkphp5 QR code in xampp environment

  • $text is the url parameter
  • $outfile The default is no, no file is generated, only the QR code image is returned, otherwise the saving path needs to be given
  • $level QR code Fault tolerance rate, default L(7%), M(15%), Q(25%), H(30%)
  • $size QR code image size, default 4
  • $ margin The size of the blank area of ​​the QR code
  • $saveabdprint The QR code is saved and displayed. $outfile must pass the path
  • $back_color Background color
  • $fore_color Draw the QR code Color
  • tip: The color must be passed in hexadecimal color value, and replace "#" with "0x"; such as #FFFFFF => 0xFFFFFF

##Summary

When we want to implement a function, generally speaking, someone will have written this thing before us. First, we should study the implementation ideas of this function by ourselves, and then think of ways to find it. Relevant document methods can be used to implement functions.

The above is the detailed content of About the generation method of thinkphp5 QR code in xampp environment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete