Two ways to generate two-dimensional code using PHP (with logo image) _php instance

Source: Internet
Author: User

First, the use of Google API to generate two-dimensional code
Google provides a more complete two-dimensional code generation interface, calling API interface is very simple, the following is the calling code:

$urlToEncode = "Http://www.jb51.net"; 
Generateqrfromgoogle ($urlToEncode); 
/** 
 * Google API two-dimensional code generation "QRCode can store up to 4,296 alphanumeric types of any text, you can view two-dimensional code data format" 
 * @param string $chl Two-dimensional code contains information, can be numbers, characters, Binary information, Chinese characters. 
 cannot mix data type, data must pass through UTF-8 url-encoded 
 * @param int $widhtHeight generate two-dimensional code size setting 
 * @param string $EC _level Optional error-correcting level. QR codes support four levels of error correction, which are used to recover lost, read-wrong, blurred, and data. 
 * L Default: Can identify lost 7% of data 
 * M can identify lost 15% of data * 
 Q can identify lost 25% of data * 
 H can identify lost 30% of data 
 * @param int $ Margin generated two-dimensional code away from the picture border 
/function Generateqrfromgoogle ($chl, $widhtHeight = ' The ', $EC _level= ' L ', $margin = ' 0 ') 
{ 
 $chl = UrlEncode ($CHL); 
 Echo '  '; 
} 


Second , the use of PHP two-dimensional code generation class library PHP QR code to generate two-dimensional code

PHP 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 critical PNG () method in which the parameter $text represents the generated two-bit text of the message, the parameter $outfile indicates whether the two-dimensional code picture file is output, the default is no, and the parameter $level indicates fault tolerance, that is, the area covered is also recognizable, are L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_h,30%); parameter $size indicates the size of the picture generated, the default is 3; parameter $ The margin represents the space value of the border around the two-dimensional code, and the parameter $saveandprint indicates whether to save the two-dimensional code and display it.

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); 
 

Invoking PHP QR code is very simple, and the following code generates a two-dimensional code with the content "Http://www.jb51.net".
PHP code  
include ' phpqrcode.php ';  
QRCode::p ng (' http://www.jb51.net ');  
& nbsp
then in practical application, we will add our own logo in the middle of the two-dimensional code, which has enhanced the publicity effect. So 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.

Include ' phpqrcode.php '; $value = ' http://www.jb51.net '; 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 = Imagecreatefromstring ( 
 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 _wi 
DTH, $logo _height); 
///Output Picture imagepng ($QR, ' helloweba.png '); 
 Echo '  ';


Below is a reference to the above code, does not produce picture files, easy to invoke, save the following code as img.php

 <?php include ' phpqrcode.php '; $value = $_get[' url '];//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 = ' jb51.png '; the prepared logo picture $QR = ' qrcode.png ';//The original two-dimensional code graph if ($logo!== FALSE) {$QR = Imagecreatefromstring (f 
 Ile_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 _wi 
DTH, $logo _height);
///Output Picture Header ("content-type:image/png"); Imagepng ($QR); 

Call Method:



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.

PS: This site also provides a very powerful two-dimensional code tool for everyone to use:

Http://tools.jb51.net/transcoding/jb51qrcode

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.