Using PHP QR code to generate a two-dimensional code ____php

Source: Internet
Author: User


Http://www.cnblogs.com/txw1958/p/phpqrcode.html


HP QR code is a PHP two-dimensional code generation class library, using it can easily generate two-dimensional code, the official website provided a download and multiple demo demo, view address:

http://phpqrcode.sourceforge.net/

Download the class library provided by the official website, only need to use phpqrcode.php can generate two-dimensional code, of course, your PHP environment must open support GD2.

Phpqrcode.php provides a key PNG () method in which
The parameter $text represents a two-bit information text;
The parameter $outfile indicates whether the two-dimensional code picture file is exported, and the default is no;
The parameter $level indicates fault tolerance, that is, the area covered is also recognized, namely L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_h,30%) ;
The parameter $size indicates that the picture size is generated, the default is 3, and the parameter $margin indicates the space value of the border around the two-dimensional code.
The parameter $saveandprint indicates whether to save the two-dimensional 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)    
{ c1/> $enc = Qrencode::factory ($level, $size, $margin);   
    Return $enc->encodepng ($text, $outfile, $saveandprint =false);   
  

Invoking PHP QR code is very simple, and the following code generates a two-dimensional code with the content "http://www.cnblogs.com/txw1958/".

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

In practical application, we will add our own logo in the middle of the two-dimensional code, which has enhanced the publicity effect. How do you generate a two-dimensional code that contains a logo? In fact, the principle is very simple, first use PHP QR code to generate a two-dimensional image, and then use the image of PHP-related functions, will be prepared in advance to add the logo to the original two-dimensional code picture, and then regenerate a new two-dimensional code picture.
. The code is as follows:

<?php include ' phpqrcode.php '; $value = ' http://www.cnblogs.com/txw1958/'; Two-dimensional code content $errorCorrectionLevel = ' L ';//fault tolerance level $matrixPointSize = 6;//Generate picture size//Generate two-dimensional code picture QRCode::p ng ($value, ' QRCode   
. png ', $errorCorrectionLevel, $matrixPointSize, 2); $logo = ' logo.png '; the prepared logo picture $QR = ' qrcode.png ';//The original two-dimensional code graph if ($logo!== FALSE) {$QR = Imagecreatefro   
    Mstring (file_get_contents ($QR));   
    $logo = imagecreatefromstring (file_get_contents ($logo)); $QR _width = Imagesx ($QR);//Two-dimensional code picture width $QR _height = Imagesy ($QR);//two-dimensional code picture 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, ' helloweixin.png ');
 Echo '  ';?>

Because the two-dimensional code is allowed to have certain fault-tolerant nature, the normal two-dimensional code, even though it is still able to decode the part, often scans the two-dimensional code when it is even less than half to decode the scan result, because the generator will repeat part of the information to improve its fault tolerance. That's why we add a logo image to the middle of a two-dimensional code that doesn't affect the decoding result.



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.