Using the PHP language to generate two-dimensional code, or very difficult, of course, call to generate two-dimensional code image of the interface (such as: http://www.liantu.com/network interface), except if you write code generation, really do not know. However, we can use Phpqrcode this ready-made class file, PHP QR code to generate the class library, using it can easily generate two-dimensional code.
Pre-Preparation:
1.phpqrcode class file download,: https://sourceforge.net/projects/phpqrcode/
2.PHP environment must be enabled to support GD2 expansion Library (normally open state)
Method Interpretation:
The downloaded class file is a compressed package that contains a lot of files and demo programs, we only need the inside of the phpqrcode.php this file can generate two-dimensional code. It is a collection of multiple classes, and we need to use the PNG () method (line No. 3090) of the QRCode class (line No. 2963) Inside:
[PHP]View PlainCopy
- 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);
- }
The 1th parameter $text: The QR code contains the content, may be the link, the text, the JSON string and so on;
The 2nd parameter $outfile: The default is False, does not generate the file, only the two-dimensional code picture returns the output;
The 3rd parameter $level: The default is L, this parameter can be passed the values are L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_h,30%), This parameter controls the fault tolerance of the two-dimensional code, and the different parameters indicate the percentage of the area where the QR code can be covered, that is, the covered area can be identified;
The 4th parameter $size: Control the size of the generated picture, the default is 4;
The 5th parameter $margin: Control the size of the blank area of the generated QR code;
The 6th parameter $saveandprint: Save the two-dimensional code picture and display it, $outfile must pass the picture path;
Examples of Use:
1. Generate two-dimensional code (generate picture file)
[PHP]View PlainCopy
- 1. Generate the original QR code (generate picture file)
- function Scerweima ($url =") {
- require_once ' phpqrcode.php ';
- $value = $url; //Two-D code content
- $errorCorrectionLevel = ' L '; //Fault tolerance level
- $matrixPointSize = 5; //Generate picture size
- //Generate two-dimensional code image
- $filename = ' qrcode/'. Microtime (). PNG ';
- QRCode::p ng ($value,$filename, $errorCorrectionLevel, $matrixPointSize, 2);
- $QR = $filename; //The original QR code image file that has been generated
- $QR = imagecreatefromstring (file_get_contents ($QR));
- //Output picture
- Imagepng ($QR, ' qrcode.png ');
- Imagedestroy ($QR);
- return '
- }
- Invoke View Results
- echo Scerweima (' https://www.baidu.com ');
2. Add logo to generated QR code (create image file)
[PHP]View PlainCopy
- 2. Add logo to generated QR code (create picture file)
- function scerweima1 ($url =") {
- require_once ' phpqrcode.php ';
- $value = $url; //Two-D code content
- $errorCorrectionLevel = ' H '; //Fault tolerance level
- $matrixPointSize = 6; //Generate picture size
- //Generate two-dimensional code image
- $filename = ' qrcode/'. Microtime (). PNG ';
- QRCode::p ng ($value,$filename, $errorCorrectionLevel, $matrixPointSize, 2);
- $logo = ' qrcode/logo.jpg '; //Ready logo image
- $QR = $filename; //The original two-dimensional code diagram that has been generated
- if (file_exists ($logo)) {
- $QR = imagecreatefromstring (file_get_contents ($QR)); //target image connection resources.
- $logo = imagecreatefromstring (file_get_contents ($logo)); //Source Image connection resources.
- $QR _width = imagesx ($QR); //Two-D code image width
- $QR _height = Imagesy ($QR); //Two-D code image height
- $logo _width = imagesx ($logo); //logo picture width
- $logo _height = Imagesy ($logo); //logo Picture height
- $logo _qr_width = $QR _width/4; //The width of the logo after the combination (1/5 of the two-dimensional code)
- $scale = $logo _width/$logo _qr_width; width scaling ratio of//logo (width of itself/width after combination)
- $logo _qr_height = $logo _height/$scale; //The height of logo after combination
- $from _width = ($QR _width- $logo _qr_width)/2; //Combination logo in the upper left corner of the coordinate point
- //Regroup and resize pictures
- /*
- * imagecopyresampled () copies a square area of an image (source image) into another image
- */
- Imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $logo _qr_width,$logo _qr_ Height, $logo _width, $logo _height);
- }
- //Output picture
- Imagepng ($QR, ' qrcode.png ');
- Imagedestroy ($QR);
- Imagedestroy ($logo);
- return '
- }
- Invoke View Results
- echo scerweima1 (' https://www.baidu.com ');
3. Generate QR code (no image file generated)
[PHP]View PlainCopy
- 3. Generate the original QR code (no picture file is generated)
- function Scerweima2 ($url =") {
- require_once ' phpqrcode.php ';
- $value = $url; //Two-D code content
- $errorCorrectionLevel = ' L '; //Fault tolerance level
- $matrixPointSize = 5; //Generate picture size
- //Generate two-dimensional code image
- $QR = QRCode::p ng ($value, False,$errorCorrectionLevel, $matrixPointSize, 2);
- }
- Invoke View Results
- Scerweima2 (' https://www.baidu.com ');
* The first two methods, each call will be generated locally a two-dimensional code image, the third method, do not generate files, will be directly output QR code to the browser.
Using Phpqrcode to generate two-dimensional code