PHP generated QR code "Google api+qrcode+ round the corner logo"

Source: Internet
Author: User
Tags save file urlencode vcard

cht=qr:必要参数,固定值<qr>,表明要生成二维码

chs=<width>x<height> : necessary parameters, generate two-dimensional code size, units are pixels, the current generated QR code is square, so the two width and height values are set to the same value

chl=<data> : The necessary parameters, the generation of two-dimensional code contains information, can be numbers, characters, binary information, Chinese characters. Data types cannot be mixed and data must be UTF-8 url-encoded. If you need to pass more than 2K bytes of information, use the Post method

choe=<output_encoding> : optional parameter, encoding format. Includes utf-8/shift_jis/iso-8859-1 three options, default is UTF-8.

chld=<error_correction_level>|<margin> : Optional parameters,

  error_correction_level: Fault tolerance level (four levels, l Default: can identify lost 7% of the data, M can identify the loss of 15% of the data, Q can identify the loss of 25% of the data, H can identify the loss of 30% of data)

  margin: The distance from the picture border generated by the QR code

Example one: Scan qr code open URL (API)

<?php//QR code information, with UrlEncode encoding $data = UrlEncode (' http://www.baidu.com ');//Generate two-dimensional code size $size = ' 300x300 ';//full API Address $qrurl = "Http://chart.googleapis.com/chart?chs= $size &cht=qr&chl= $data &chld=l|1&choe=utf-8";//Get two-dimensional code $ QRCode = file_get_contents ($qrurl);//Output Picture header (' content-type:image/png '); Echo $qrcode;

After opening a website, we want to achieve the purpose of the original, scan the QR code, the enterprise information into the address book, that is, in the QR Code contains a business card information, in fact, the two-dimensional code is to put a bunch of information into the form of pictures, and this heap of information in the end what it does not care about, Really care about this information is scanning the QR Code tool, in the above example, when you scan the QR code, read the data stored in the QR code, and then try to start to identify it, when it found that the content is Baidu's Web site, then use the browser to open it, The same when it found that the content is a bunch of simple text data is displayed in the form of text, the same can be identified the content of the QR code is not a vcard format of the Electronic Business card, our phone Address Book import and export is mostly this format, so we can directly in the QR code contains a standard vcard, which allows the scanning tool to recognize it as an address book.

The vcard format reference 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: Chi-Wen Studio
ROLE: Product Division
Title:cto
ADR; work; POSTAL: No. 35th North Four Ring road, Chaoyang District, Beijing 100101
rev:2012-12-27t08:30:02z
End:vcard If you want more detailed customization of the required format, please Baidu VCARD format standard.

Example two: Generate a business card two-dimensional code (API)

<?php//building a vcard format data $vcard =     "Begin:vcard".    " \nversion:3.0 ".    " \NFN: Small ".    " \ntel; CELL; voice:18858140621 ".    " \ntel; work; voice:0358-2157466 ".    " \nemail:[email protected] ".    " \nurl:http://www.baidu.com ".    " \nadr: Bingjiang District, Hangzhou, Zhejiang, China.    \nend:vcard ";//QR code information, with UrlEncode code $data = UrlEncode ($vcard);//Generate two-dimensional code size $size = ' 300x300 ';//full API Address $qrurl =" http:// chart.googleapis.com/chart?chs= $size &cht=qr&chl= $data &chld=l|1&choe=utf-8 ";//get QR code $qrcode = File _get_contents ($qrurl);//Output Image header (' content-type:image/png '); Echo $qrcode;

So far we have basically achieved the purpose of the original, but the actual use, because the above-assembled URL directly added to the img tag src attribute, the result is a little problem, due to our business card information more, resulting in two-dimensional code generation error, at the beginning of this article has been marked with a red font, get Mode can only pass at most 2K of data, more than 2K data when we need to use the POST request, so in the actual use, you can first in the program through the post to obtain the QR code and save as a local image, then directly call local image.

Method Two: Using PHP QR code to generate two-dimensional code

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

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

How to use: QRCode::p ng ($data, $filename, $errorCorrectionLevel, $matrixPointSize, $margin);

* $data Data
* $filename generate two-dimensional code to save the path, if false output directly to the browser
* $errorCorrectionLevel error handling level L, M, Q, H; correction level with Google API
* $matrixPointSize pixels for each black point
* $margin white border pixels outside the picture

Example three: Scan qr code open URL (phpqrcode)


Include qrlib.php need to be put together with other documents; Phpqrcode.php is a merged version that only needs to contain this file, but the resulting picture 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 ';//point size: 1 to 10$matrixpointsize = 5; QRCode::p ng ($data, False, $errorCorrectionLevel, $matrixPointSize, 2);

Example two: Generate a business card QR code (PHPQRCODE)

<?php
Contains qrlib.php or Phpqrcode.phpinclude ('./phpqrcode/qrlib.php ');
Build the vcard Data $vcard = "Begin:vcard". " \nversion:3.0 ". " \NFN: Elvis ". " \ntel; CELL; voice:18858140621 ". " \ntel; work; voice:0358-2157466 ". " \nemail:[email protected] ". " \nurl:http://www.mailuow.com ". " \nadr: No. 1750th, Jianghong Road, Bingjiang District, Hangzhou, Zhejiang, China. \nend:vcard ";//Error correction level: L, M, Q, h$errorcorrectionlevel = ' L ';//point size: 1 to 10$matrixpointsize = 4; QRCode::p ng ($vcard, False, $errorCorrectionLevel, $matrixPointSize, 2);

So far we can use two methods to achieve the original idea, but the two-dimensional code is hanging up, the function has been realized, but always feel not gorgeous enough, if you can put the logo of the enterprise into the two-dimensional code in the good, hey, of course, this part of the work is not in the two-dimensional code generation of the fan, the next In the GD library to complete, but one thing to understand, we mentioned in the previous two-dimensional code of fault tolerance level, that is, the two-dimensional code after the destruction of a part of the possibility of parsing the complete information, and to the two-dimensional code in the middle of the logo can still read the complete information is its high fault tolerance.

Online has found some PHP generation with Logo QR code method, but not careful enough, the generation of two-dimensional code is very difficult to see, the author tried several methods, now will be the best one to take out and share with you.

Overall ideas such as:

Example five: Create a two-dimensional code with Logo

<?php
Contains qrlib.php or Phpqrcode.phpinclude ('./phpqrcode/qrlib.php ');
Build the vcard Data $vcard = "Begin:vcard". "\nversion:3.0". "\NFN: Elvis". "\ntel; CELL; voice:18858140621 ". "\ntel; work; voice:0358-2157466 ". "\nemail:[email protected]". "\nurl:http://www.mailuow.com". "\nadr: No. 1750th, Jianghong Road, Bingjiang District, Hangzhou, Zhejiang, China". "\nend:vcard";//Error correction level: L, M, Q, h$errorcorrectionlevel = ' m ';//point size: 1 to 10$matrixpointsize = 4;//Save file name $filename = ' Test.png ';//Generate two-dimensional code qrcode::p ng ($vcard, $filename, $errorCorrectionLevel, $matrixPointSize, 2); Get the QR code $qrcode = file_get_contents ($filename); $qrcode = imagecreatefromstring ($qrcode); $qrcode _width = Imagesx ($ QRCode); $qrcode _height = Imagesy ($qrcode);//fillet picture $corner = file_get_contents ('./images/corner.png '); $corner = Imagecreatefromstring ($corner); $corner _width = Imagesx ($corner); $corner _height = Imagesy ($corner);// Calculate the width and height of the fillet picture and relative to the location of the two-dimensional code of the Kunming National Defense Road Hospital, copy the fillet picture to the center of the QR code $corner_qr_height = $corner _qr_width = $qrcode _width/5; $from _width = ($ qrcode_width-$corner _qr_width)/2;imagecopyresampled ($qrcode, $corner, $from _width, $from _width, 0, 0, $corner _Qr_width, $corner _qr_height, $corner _width, $corner _height);//logo Picture $logo = file_get_contents ('./images/logo.jpg '); $logo = imagecreatefromstring ($logo), $logo _width = Imagesx ($logo); $logo _height = Imagesy ($logo);// Calculate the width of the logo image and relative to the location of the QR code, copy the logo to the QR code central $logo_qr_height = $logo _qr_width = $qrcode _WIDTH/5-6; $from _width = ($qrcode _ width-$logo _qr_width)/2;imagecopyresampled ($qrcode, $logo, $from _width, $from _width, 0, 0, $logo _qr_width, $logo _qr_ Height, $logo _width, $logo _height); header (' content-type:image/png '); Imagepng ($qrcode); Imagedestroy ($qrcode);
Imagedestroy ($corner);
Imagedestroy ($logo);
Related Article

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.