Three ways to generate two-dimensional code in PHP

Source: Internet
Author: User

Simplest and most practical goolge open source method

1.google Open API

The code is as follows:

 $urlToEncode = "http://www.helloweba.com"; Generateqrfromgoogle ($ Urltoencode);  /** * Google API qr code generation "QRCode can store up to 4,296 alphanumeric types of arbitrary text, you can view the QR code data format" * @param string $chl QR Code contains information, can be numbers, characters, binary information, Chinese characters. Data types cannot be mixed, data must go through UTF-8 url-encoded * @param int $widhtHeight dimension setting for QR code * @param string $EC _level Optional error correction level, QR code supports four levels of error correction to recover lost  Missing, misread, fuzzy, data.                            * L-Default: can identify loss of 7% of data * M can identify lost 15% of the data * Q can identify data that has lost 25% * H can identify data that has lost 30% * @param int $margin The distance generated by the QR code from the picture border */function generate     Qrfromgoogle ($chl, $widhtHeight = ' Max ', $EC _level= ' L ', $margin = ' 0 ') {$chl = UrlEncode ($CHL);     Echo '  '; }

2.php class library PHP QR Code

Address: http://phpqrcode.sourceforge.net/
Download: http://sourceforge.net/projects/phpqrcode/

After downloading the class library provided by the official website, we only need to use phpqrcode.php to generate the QR code, of course, your PHP environment must turn on support GD2. Phpqrcode.php provides a key PNG () method, where the parameter $text represents the generation of two-bit information text, the parameter $outfile indicates whether to output a QR code picture file, the default is no, the parameter $level indicates fault tolerance, that is, the covered area can be recognized, respectively L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_h,30%), parameter $size indicates the image size is generated, default is 3; Margin indicates the spacing value of the border space around the QR code, and the parameter $saveandprint indicates whether to save the QR code and display it.

The code is as follows:

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); }

Call PHP QR Code:

Include ' phpqrcode.php '; QRCode::p ng (' http://www.helloweba.com ');


In practical applications, we will add their own logo in the middle of the QR code, has enhanced the publicity effect. How do you generate a QR code that contains a logo? In fact, the principle is very simple, first use PHP QR code to generate a two-dimensional code image, and then the use of PHP image correlation function, the pre-prepared logo image is added to the newly generated image of the original two-dimensional code, and then regenerate a new QR code image.

Include ' phpqrcode.php '; $value = ' http://www.helloweba.com '; QR Code content $errorCorrectionLevel = ' L ';//fault tolerance level $matrixPointSize = 6;//Generate picture size//generate QR code picture qrcode::p ng ($value, ' qrcode.png ', $ Errorcorrectionlevel, $matrixPointSize, 2); $logo = ' logo.png ';//Ready logo image $QR = ' qrcode.png ';//The original two-dimensional code graph that has been generated if ($logo!== FALSE) {$QR = Imagecreatefromstring (     File_get_contents ($QR));     $logo = imagecreatefromstring (file_get_contents ($logo));     $QR _width = Imagesx ($QR);//QR code image width $QR _height = Imagesy ($QR);//QR code image height $logo _width = imagesx ($logo);//logo Picture width     $logo _height = Imagesy ($logo),//logo picture height $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; Regroup the picture and resize imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $logo _qr_width, $logo _qr_height, $lo Go_width, $logo _height); }//Output picture imagepng ($QR, ' helloweba.png '); Echo ' 

For the second method: If you do not use $filename, the second parameter is false, the two-dimensional code image will not be saved, but the direct output.

Now there are some: Libqrencode and QRCode Perl CGI & PHP scripts qr code generation plugin Big family likes also can see.

3. jquery-based QR code generation plugin QRCode, call the plugin in the page to generate the corresponding QR code.

QRCode is actually using jquery to achieve graphical rendering, drawing, support for canvas (HTML5) and table two ways,

You can get the latest code from Https://github.com/jeromeetienne/jquery-qrcode.

How to use

(1), first add the jquery library file and the QRCode plugin to the page.

<script type= "Text/javascript" src= "jquery.js" ></script> <script type= "Text/javascript" src= " Jquery.qrcode.min.js "></script>

(2), in the page need to display the QR code place to add the following code:

<p id= "Code" ></p>

(3), call QRCode plug-in.

QRCode supports canvas and table two ways of rendering pictures, the default use of canvas, the most efficient, of course, the browser supports HTML5. The direct invocation is as follows:

$ (' #code '). QRCode ("http://www.helloweba.com"); Arbitrary string

You can also call it in the following ways:

$ ("#code"). QRCode ({     render: "table",//table mode     width:200,//Width     height:200,//height     text: " Www.helloweba.com "//any Content});

In this way, a QR code can be generated directly from the page, and you can read the QR code information using the "sweep" function of your phone.

(4). Identify Chinese

When we tried to find the two-dimensional code that did not recognize the Chinese content, we found that the Jquery-qrcode was encoded by the charCodeAt () method. This method will get its Unicode encoding by default, and if it has Chinese content, it will convert the string to UTF-8 before generating the QR code, and then generate the QR code. You can use the following functions to convert Chinese strings:

function ToUtf8 (str) {        var out, I, Len, C;        out = "";        len = str.length;        for (i = 0; i < len; i++) {            c = str.charcodeat (i);            if ((c >= 0x0001) && (c <= 0x007F)) {out                + = Str.charat (i);            } else if (C > 0x07ff) {out                + = S Tring.fromcharcode (0xE0 | ((c >> b) & 0x0F));                Out + = String.fromCharCode (0x80 | ((c >>  6) & 0x3F));                Out + = String.fromCharCode (0x80 | ((c >>  0) & 0x3F));            } else {                out + = String.fromCharCode (0xC0 | ((c >>  6) & 0x1F));                Out + = String.fromCharCode (0x80 | ((c >>  0) & 0x3F);}        }        return out;    }

The following example:

var str = ToUtf8 ("There is no story in Provence"); $ (' #code '). QRCode (str);

This article introduces three ways to generate two-dimensional code in PHP, and more about it, please follow the PHP Chinese web.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.