Using Phpqrcode to generate two-dimensional code

Source: Internet
Author: User
Tags php language vars

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
  1. Public static function png ($text, $outfile = False, $level = qr_eclevel_l, $size = 3, $margin = 4, $saveandprint =false)
  2. {
  3. $enc = qrencode::factory ($level, $size, $margin);
  4. return $enc->encodepng ($text, $outfile, $saveandprint =false);
  5. }

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. 1. Generate the original QR code (generate picture file)
  2. function Scerweima ($url =") {
  3. require_once ' phpqrcode.php ';
  4. $value = $url; //Two-D code content
  5. $errorCorrectionLevel = ' L '; //Fault tolerance level
  6. $matrixPointSize = 5; //Generate picture size
  7. //Generate two-dimensional code image
  8. $filename = ' qrcode/'. Microtime ().  PNG ';
  9. QRCode::p ng ($value,$filename, $errorCorrectionLevel, $matrixPointSize, 2);
  10. $QR = $filename; //The original QR code image file that has been generated
  11. $QR = imagecreatefromstring (file_get_contents ($QR));
  12. //Output picture
  13. Imagepng ($QR, ' qrcode.png ');
  14. Imagedestroy ($QR);
  15. return '
  16. }
  17. Invoke View Results
  18. echo Scerweima (' https://www.baidu.com ');

2. Add logo to generated QR code (create image file)

[PHP]View PlainCopy
  1. 2. Add logo to generated QR code (create picture file)
  2. function scerweima1 ($url =") {
  3. require_once ' phpqrcode.php ';
  4. $value = $url; //Two-D code content
  5. $errorCorrectionLevel = ' H '; //Fault tolerance level
  6. $matrixPointSize = 6; //Generate picture size
  7. //Generate two-dimensional code image
  8. $filename = ' qrcode/'. Microtime ().  PNG ';
  9. QRCode::p ng ($value,$filename, $errorCorrectionLevel, $matrixPointSize, 2);
  10. $logo = ' qrcode/logo.jpg '; //Ready logo image
  11. $QR = $filename; //The original two-dimensional code diagram that has been generated
  12. if (file_exists ($logo)) {
  13. $QR = imagecreatefromstring (file_get_contents ($QR));  //target image connection resources.
  14. $logo = imagecreatefromstring (file_get_contents ($logo));  //Source Image connection resources.
  15. $QR _width = imagesx ($QR); //Two-D code image width
  16. $QR _height = Imagesy ($QR); //Two-D code image height
  17. $logo _width = imagesx ($logo); //logo picture width
  18. $logo _height = Imagesy ($logo); //logo Picture height
  19. $logo _qr_width = $QR _width/4; //The width of the logo after the combination (1/5 of the two-dimensional code)
  20. $scale = $logo _width/$logo _qr_width; width scaling ratio of//logo (width of itself/width after combination)
  21. $logo _qr_height = $logo _height/$scale; //The height of logo after combination
  22. $from _width = ($QR _width- $logo _qr_width)/2; //Combination logo in the upper left corner of the coordinate point
  23. //Regroup and resize pictures
  24. /* 
  25. * imagecopyresampled () copies a square area of an image (source image) into another image
  26. */
  27. Imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $logo _qr_width,$logo _qr_   Height, $logo _width, $logo _height);
  28. }
  29. //Output picture
  30. Imagepng ($QR, ' qrcode.png ');
  31. Imagedestroy ($QR);
  32. Imagedestroy ($logo);
  33. return '
  34. }
  35. Invoke View Results
  36. echo scerweima1 (' https://www.baidu.com ');


3. Generate QR code (no image file generated)

[PHP]View PlainCopy
  1. 3. Generate the original QR code (no picture file is generated)
  2. function Scerweima2 ($url =") {
  3. require_once ' phpqrcode.php ';
  4. $value = $url; //Two-D code content
  5. $errorCorrectionLevel = ' L '; //Fault tolerance level
  6. $matrixPointSize = 5; //Generate picture size
  7. //Generate two-dimensional code image
  8. $QR = QRCode::p ng ($value, False,$errorCorrectionLevel, $matrixPointSize, 2);
  9. }
  10. Invoke View Results
  11. 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

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.