Two ways to generate QR codes using PHP _php tutorial

Source: Internet
Author: User
With the progress of science and technology, two-dimensional Code application field more and more widely, today I give you to share how to use PHP to generate two-dimensional code, and how to generate a logo image in the middle of the two-dimensional code

First, the use of Google API to generate two-dimensional code Google provides a more complete QR code generation interface, call API interface is very simple, the following is the calling code: the code is as follows: $urlToEncode = "Http://www.jb51.net"; 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 , misread, vague, and data. * L-Default: can identify loss of 7% of data * M can identify loss of 15% of data * Q can identify loss of 25% data * H can identify loss of 30% data * @param int $margin The distance generated by the QR code from the picture border */function Generateqrfromgoogle ($chl, $widhtHeight = ' Max ', $EC _level= ' L ', $margin = ' 0 ') {$chl = UrlEncode ($CHL); Echo ';} Using PHP QR code to generate a class library PHP code to generate two-dimensional codes php qr code is a php two-dimensional code generation library, using it can easily generate two-dimensional code, the official website provides a download and a number of demo demo, view address:/HTTP phpqrcode.sourceforge.net/. 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%);The number $size represents the generated picture size, the default is 3, the parameter $margin represents the space around the border between the two-dimensional code, the parameter $saveandprint indicates whether to save the QR code and display. 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); Calling PHP QR code is very simple, and the following code generates a QR code that reads "Http://www.jb51.net". PHP code include ' phpqrcode.php '; QRCode::p ng (' http://www.jb51.net '); In the actual application, we will add their own logo in the middle of the two-dimensional 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. The code is as follows: include ' phpqrcode.php '; $value = ' http://www.jb51.net '; 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 figure already generated if ($logo!== FALSE) {$QR = imagecreatefromstring (file_g Et_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 pictures and resize imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $logo _qr_width, $logo _qr_height, $logo _ width, $logo _height); }//Output picture imagepng ($QR, ' helloweba.png '); Echo '; Because the two-dimensional code allows a certain degree of fault tolerance, the general QR code even in the masked part but still able to decode, often we scan the QR code when scanned to even less than half of the scanning results can be decoded, because the generator will be part of the information repeated representation to improve its fault tolerance, This is why we add a logo image in the middle of the QR code does not affect the decoding result of the reason.

http://www.bkjia.com/PHPjc/741816.html www.bkjia.com true http://www.bkjia.com/PHPjc/741816.html techarticle with the progress of science and technology, the application of two-dimensional code more and more widely, today I give you to share how to use PHP to generate two-dimensional code, and how to generate a logo image in the middle of the two-dimensional code one, Lee ...

  • 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.