thinkphp5 dimensional code generating composer

Extend into the folder

composer require endroid/qrcode

 

2. The two-dimensional code generated as services

 

 

 

QrcodeServer.phpcode show as below:

? < PHP
 / * * 
 * PhpStorm the Created by. 
 * The User: cdjyj21 
 * a Date: 2018/9/4 
 * Time: 11:57 
 * / 

namespace App \ Services; 

// add just introduced the composer installed inside the class are automatically loaded class

  use think\facade\App;
  require_once App::getRootPath().'/extend/vendor/autoload.php';


Endroid use \ QrCode \ ErrorCorrectionLevel; 
use Endroid \ QrCode \ LabelAlignment; 
use Endroid \ QrCode \ QrCode; 

class QrcodeServer 
{ 
    protected $ _qr;
     protected $ _encoding = ' UTF-. 8 ' ;               // coding type 
    protected $ _size = 300 ;                   // two-dimensional code size 
    protected $ _logo = false ;                 // need logo with a two-dimensional code 
    protected $ _logo_url = '' ;                    // logo image path 
    protected_logo_size = $ 80 ;                    // logo size 
    protected $ = _title to false ;                 // whether two-dimensional code title 
    protected $ _title_content = '' ;                    // title content 
    protected $ _generate = ' the display ' ;             // Display- displayed directly writefile- file write 
    protected $ _file_name = ' ./static/qrcode ' ;     // write the file path 
    const MARGIN = 10 ;                        // two-dimensional code with respect to the contents from the outside throughout the image 
    const WRITE_NAME = ' PNG ' ;                      // write the file name suffix 
    const FOREGROUND_COLOR color = [ ' R & lt ' => 0 , ' G ' => 0 , ' B ' => 0 , ' A ' => 0 ];           // foreground 
    const BACKGROUND_COLOR = [ ' R & lt ' => 255 , ' G ' => 255, 'b' => 255, 'a' => 0];    // 背景色

    public function __construct($config) {
        isset($config['generate'])      &&  $this->_generate        = $config['generate'];
        isset($config['encoding'])      &&  $this->_encoding        = $config['encoding'];
        isset($config['size'])          &&  $this->_size            = $config['size'];
        isset($config['logo'])          &&  $this->_logo            = $config['logo'];
        isset($config['logo_url'])      &&  $this->_logo_url        = $config['logo_url'];
        isset($config['logo_size'])     &&  $this->_logo_size       = $config['logo_size'];
        isset($config['title'])         &&  $this->_title           = $config['title'];
        isset($config['title_content']) &&  $this->_title_content   = $config['title_content'];
        isset($config['file_name'])     &&  $this->_file_name       = $config['file_name'];

    public* /
     * @return Array | Page INPUT
     * Generates two-dimensional code*/ *
    }
     * @param $content //需要写入的内容
     
     function createServer($content) {
        $this->_qr = new QrCode($content);
        $this->_qr->setSize($this->_size);
        $this->_qr->setWriterByName(self::WRITE_NAME);
        $this->_qr->setMargin(self::MARGIN);
        $this->_qr->setEncoding($this->_encoding);
        $this->_qr->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);   // 容错率
        $this->_qr->setForegroundColor(self::FOREGROUND_COLOR);
        $this->_qr->setBackgroundColor(self::BACKGROUND_COLOR);
        // 是否需要title
        if ($this->_title) {
            $this->_qr->setLabel($this->_title_content, 16, null, LabelAlignment::CENTER);
        }
        // 是否需要logo
        if ($this->_logo) {
            $this->_qr->setLogoPath($this->_logo_url);
            $this->_qr->setLogoWidth($this->_logo_size);
        }

        $this->_qr->setValidateResult(false);

        if ($this->_generate == 'display') {
            // 展示二维码
            // 前端调用 例:<img src="http://localhost/qr.php?url=base64_url_string">
            header('Content-Type: ' . $this->_qr->getContentType());
            return $this->_qr->writeString();
        } else if ($this->_generate == 'writefile') {
            // 写入文件
            $file_name = $this->_file_name;
            return $this->generateImg($file_name);
        } else {
            return ['success' => false, 'message' => 'the generate type not found', 'data' => ''];
        }
    }

    /**
     * 生成文件
     * @param $file_name //目录文件 例: /tmp
     * @return array
     */
    public function generateImg($file_name) {
        $file_path = $file_name . DIRECTORY_SEPARATOR . uniqid() . '.' . self::WRITE_NAME;

        if (!file_exists($file_name)) {
            mkdir($file_name, 0777, true);
        }

        try {
            $this->_qr->writeFile($file_path);
            $data = [
                'url' => $file_path,
                'ext' => self::WRITE_NAME,
            ];
            return ['success' => true, 'message' => 'write qrimg success', 'data' => $data];
        } catch (\Exception $e) {
            return ['success' => false, 'message' => $e->getMessage(), 'data' => ''];
        }
    }

}
3. Call

Example:

? < PHP
 / * * 
 * PhpStorm the Created by. 
 * The User: cdjyj21 
 * a Date: 2018/9/4 
 * Time: 11:57 
 * / 

namespace App \ the Test \ the Controller; 

use App \ Services \ QrcodeServer; 

class qrcode 
{ 
    / * * 
     * direct output to generate a two-dimensional code + dimensional code image file 
     * / 
    public function Create () {
         // custom two-dimensional code arranged 
        $ config = [
             ' title '          => to true ,
             ' title_content ' => ' Test ' ,
            ' Logo '           => to true ,
             ' logo_url '       => ' ./logo.png ' ,
             ' logo_size '      => 80 , 
        ]; 

        // directly outputs 
        $ qr_url = ' http://www.baidu.com?id= ' . RAND ( 1000 , 9999 ); 

        $ qr_code = new new QrcodeServer ($ config); 
        $ qr_img = $ qr_code-> the createServer ($ qr_url); 
        echo $ qr_img; 

        // write the file
        qr_url = $ ' This test is a two-dimensional code ' ; 
        $ file_name = ' ./static/qrcode ' ;   // save the definition directory 

        $ config [ ' file_name ' ] = $ file_name; 
        $ config [ ' Generate ' ] = ' WriteFile ' ; 

        $ qr_code = new new QrcodeServer ($ config); 
        $ RS = $ qr_code-> the createServer ($ qr_url); 
        print_r ($ RS); 

        Exit; 
    } 
}

 

In the browser to directly access create()method, will direct the output of two-dimensional code, and it will generate a two-dimensional code images saved in a custom directory. Results are as follows:

 

 

 This two-dimensional code that direct output of how it applies to projects, usually directly written in html <img> tag, for example:

<img src = " HTTP: // localhost: 8080 / projecttest / qrtest the above mentioned id = 1234? "   alt = " This is a two-dimensional code " />

Here a list of a few parameters I understand, are also considered himself to be a note of it.

parameter name description Examples
setText Set Text https://www.baidu.com
setSize Set the size of the two-dimensional code, there should be a square two-dimensional code, so is equivalent to the length and width 400
setMargin Set two-dimensional code Margins 10
setForegroundColor Set the foreground color, RGB color array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0)
setBackgroundColor Set the background color, RGB color array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0)
setEncoding Setting coding Utfa8
setErrorCorrectionLevel Set error level (low / medium / quartile / high) high
setLogoPath Set logo path logo.png
setLogoWidth Set logo size 50
setLabel Settings tab test
setLabelFontSize Settings tab font size 16
setLabelFontPath Settings tab font path null
setLabelAlignment Settings tab alignment (left / center / right) center
setLabelMargin 设置标签边距 array('t' => 10,'r' => 20,'b' => 10,'l' => 30)
setWriterRegistry    
setWriter    
setWriterByName 写入文件的后缀名 png
setWriterByPath    
setWriterByExtension    
setValidateResult    
writeString    
writeDataUri    
writeFile 写入文件 test.png


参考原文: https://www.jianshu.com/p/9b933907acd6

Guess you like

Origin www.cnblogs.com/jasonLiu2018/p/12073148.html