Php QR code generation

Source: Internet
Author: User
As QR codes become increasingly popular. We need to know what a QR code is. as a phpprogramer, we need to know how to use php to generate a QR code. Let's talk a little bit about it. This article describes two methods to generate a QR code using php.
(1) open interface for generating QR codes using google. the code is as follows:

/*** Google api QR code generation [QRcode can store any text of a maximum of 4296 letters/numbers. for details, see the QR code data format] * @ param string $ data information contained in the QR code, it can be numbers, characters, binary information, and Chinese characters. Cannot mix data types, data must go through UTF-8 URL-encoded. if the information to be transmitted exceeds 2 K bytes, use the POST method * @ param int $ widhtHeight to generate the size setting of the QR code * @ param string $ EC_level (optional) error correction level, QR codes support four levels of error correction, which are used to restore lost, read error, fuzzy, and data. * L-default: 7% of lost data can be identified * M-15% of lost data can be identified * Q-25% of lost data can be identified * H-30% of lost data can be identified *@ distance from the QR code generated by param int $ margin to the image border */function generateQRfromGoogle ($ data, $ widhtHeight = '000000', $ EC_level = 'l', $ margin = '0') {$ url = urlencode ($ data); echo '';}

Usage:

$ Data = 'All rights reserved: http://www.jb51.net '; generateQRfromGoogle ($ data );

Post method:

Function qrcode ($ width, $ height, $ string) {$ post_data = array (); $ post_data ['cht'] = 'QR '; $ post_data ['chs '] = $ width. "x ". $ height; $ post_data ['check'] = $ string; $ post_data ['choe'] = "UTF-8"; $ url = "http://chart.apis.google.com/chart"; $ data_Array = array (); foreach ($ post_data as $ key => $ value) {$ data_Array [] = $ key. '= '. $ value ;}$ data = implode ("&", $ data_Array); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); $ result = curl_exec ($ ch); // echo ""; note: return $ result without writing the header ;}

Usage:

Header ("Content-type: image/png"); $ width = 300; $ height = 300; $ data = 'copyright: http://www.jb51.net '; echo qrcode ($ width, $ height, $ data );

Of course, the generated images are the same as above.

(2) use the php QR Code library to generate a QR Code
Note: To use this class library, you must first download the class library package ,:
Address: http://phpqrcode.sourceforge.net/
There are many examples in the downloaded package, which can be studied by yourself. The following is a simple use case (the specific parameter meaning is similar to the above ):

<? Php include ". /phpqrcode. php "; $ data = 'All rights reserved: http://www.jb51.net '; $ errorCorrectionLevel =" L "; $ matrixPointSize =" 4 "; QRcode: png ($ data, false, $ errorCorrectionLevel, $ matrixPointSize );

The above is all the content of this article. I hope it will be helpful for you to master the php production QR code.

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.