Php qr code generation _ PHP Tutorial

Source: Internet
Author: User
Tags vcard
PHP generates a QR code. Parameter description: chtqr: Required parameter, fixed value qr, indicating to generate the two-dimensional code chswidthxheight: Required parameter, generating the two-dimensional code size, unit: Pixel, currently generated two-dimensional code are parameter description:

Cht = qr: Required parameter, fixed value To generate a QR code

Chs = X : Required parameter. The size of the generated QR code, in pixels. Currently, the generated QR code is square. Therefore, both width and height values are set to the same value.

Chlorophyll =: Required parameter to generate information contained in the QR code, which can be numbers, characters, binary information, and Chinese characters. The data type cannot be mixed, and must pass through the UTF-8 URL-encoded. if the information to be passed exceeds 2 K bytes, use the POST method

Choe = : Optional parameter, encoding format. Includes three options: UTF-8/Shift_JIS/ISO-8859-1, which defaults to UTF-8.

Chld = | : Optional parameter,

Error_correction_level: fault tolerance level (divided into four levels, L-default: 7% of lost data can be identified; M-can identify 15% of lost data; q-can identify 25% of data loss; H-can identify 30% of data loss)

Margin: The distance between the generated QR code and the image border.

Example 1: Scan the QR code to open the URL (API)

// QR code, which is encoded with urlencode

$ Data = urlencode ('http: // www.baidu.com ');

// Generate the QR code size

$ Size = '300x300 ';

// Complete API address

$ Qrurl = "http://chart.googleapis.com/chart? Chs = $ size & cht = qr & chlorophyll = $ data & chld = L | 1 & choe = UTF-8 ";

// Obtain the QR code

$ Qrcode = file_get_contents ($ qrurl );

// Output image

Header ('content-type: image/png ');

Echo $ qrcode;

After opening a website, we want to achieve the initial goal of scanning the QR code and importing the enterprise information into the address book. that is, the QR code contains a business card information, in fact, the QR code is simply to convert a pile of information into images for expression, and it does not care about what the pile of information looks like. what really cares about this information is a tool for scanning the QR code, in the preceding example, after scanning the QR code, you can read the data stored in the QR code and try to identify it, when it finds that the content is Baidu's website, it is opened in a browser. Similarly, when it finds that the content is a pile of simple text data, it is displayed in the form of text, we can also identify whether the content of the QR code is a digital business card in vcard Format. most of our mobile phone address book import and export are in this format, therefore, we can directly include a standard vcard e-card in the QR code, so that the scanning tool can recognize it as the address book.

The vcard format is as follows:

BEGIN: VCARD

VERSION: 3.0

FN: User name

TEL; CELL; VOICE: 18858140621

TEL; WORK; VOICE: 0358-2157466

TEL; WORK; FAX: 0358-2157466

EMAIL; PREF; INTERNET: lzw # lzw. me

URL: http://lzw.me

OrG: Zhiwen Studio

ROLE: Product Department

TITLE: CTO

ADR; WORK; POSTAL: No. 35, Beisihuan Middle Road, Chaoyang district, Beijing; 100101

REV: 2012-12-27T08: 30: 02Z

END: VCARD

If you want to customize the required format in more detail, please use the Baidu vcard format standard.

Example 2: generate a business card QR code (API)

// Construct a vcard format data

$ Vcard =

"BEGIN: VCARD ".

"\ NVERSION: 3.0 ".

"\ NFN: Small Network ".

"\ NTEL; CELL; VOICE: 18858140621 ".

"\ NTEL; WORK; VOICE: 0358-2157466 ".

"\ NEMAIL: luoluo@qq.com ".

"\ NURL: http://www.baidu.com ".

"\ NADR: Binjiang District, Hangzhou city, Zhejiang province ".

"\ NEND: VCARD ";

// QR code, which is encoded with urlencode

$ Data = urlencode ($ vcard );

// Generate the QR code size

$ Size = '300x300 ';

// Complete API address

$ Qrurl = "http://chart.googleapis.com/chart? Chs = $ size & cht = qr & chlorophyll = $ data & chld = L | 1 & choe = UTF-8 ";

// Obtain the QR code

$ Qrcode = file_get_contents ($ qrurl );

// Output image

Header ('content-type: image/png ');

Echo $ qrcode;

So far, our initial goal has been basically achieved, but in actual use, because we directly added the assembled url to the src attribute of the img tag, there was a small problem, due to the large amount of business card information, a QR code generation error occurs. we have marked it in red at the beginning of this article. the get method can only transmit 2 K big data at most, we need to use the POST request when the data is larger than 2 K. Therefore, in actual use, we can first obtain the QR code in the program using the POST method and save it as a local image, and then directly call the local image.

Method 2: use php qr code to generate a QR CODE

Official Homepage: http://phpqrcode.sourceforge.net

: Http://sourceforge.net/projects/phpqrcode/

Usage: QRcode: png ($ data, $ filename, $ errorCorrectionLevel, $ matrixPointSize, $ margin );

* $ Data

* $ Filename: generate the path for saving the QR code. if it is false, it is directly output to the browser.

* $ ErrorCorrectionLevel error handling level L, M, Q, and H; same as the error correction level in Google API

* $ MatrixPointSize the pixels of each black spot

* $ White border pixels on the periphery of the margin image

Example 3: Scan the QR code to open the URL (phpqrcode)

// Contains qrlib. php or phpqrcode. php

// Qrlib. php must be put together with other files. phpqrcode. php is a merged version that only needs to contain this file, but the generated image is slow and inaccurate.

Include ('./phpqrcode/qrlib. php ');

// QR code data

$ Data = 'http: // www.111cn.net ';

// Error correction level: L, M, Q, H

$ ErrorCorrectionLevel = 'l ';

// Vertex size: 1 to 10

$ MatrixPointSize = 5;

QRcode: png ($ data, false, $ errorCorrectionLevel, $ matrixPointSize, 2 );

Example 2: generate a business card QR code (phpqrcode)

// Contains qrlib. php or phpqrcode. php

Include ('./phpqrcode/qrlib. php ');

// Construct vcard data

$ Vcard =

"BEGIN: VCARD ".

"\ NVERSION: 3.0 ".

"\ NFN: Wang GE ".

"\ NTEL; CELL; VOICE: 18858140621 ".

"\ NTEL; WORK; VOICE: 0358-2157466 ".

"\ NEMAIL: wang@qq.com ".

"\ NURL: http://www.mailuow.com ".

"\ NADR: No. 1750, Jianghong Road, Binjiang District, Hangzhou city, Zhejiang province ".

"\ NEND: VCARD ";

// Error correction level: L, M, Q, H

$ ErrorCorrectionLevel = 'l ';

// Vertex size: 1 to 10

$ MatrixPointSize = 4;

QRcode: png ($ vcard, false, $ errorCorrectionLevel, $ matrixPointSize, 2 );

Required cht = qr: Required parameter, fixed value qr, indicating to generate two-dimensional code chs = widthxheight: Required parameter, generating two-dimensional code size, unit: Pixel, currently generated two-dimensional code are all...

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.