A method of generating two-dimensional code for Phpqrcode class

Source: Internet
Author: User
Tags php language
Using the PHP language to generate two-dimensional code, or very difficult, of course, the call to generate a two-dimensional code image of the interface except, if you write code generation, really do not. 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, download address: 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:


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 content that the QR code contains, can be link, text, JSON string and so on;
The 2nd parameter $outfile: The default is False, do not generate files, only the two-dimensional code image return output;
The 3rd parameter $level: The default is L, which can pass values of L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_ h,30%), this parameter control the two-dimensional code fault tolerance, the different parameters of the two-dimensional code can be covered by the percentage of the area, that is covered by the area can also 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)


1. Generate the original QR code (generate picture file) function Scerweima ($url = ") {  require_once ' phpqrcode.php ';  $value = $url;         Two-dimensional code content  $errorCorrectionLevel = ' L ';  Fault tolerance level  $matrixPointSize = 5;      Generate picture size  //Generate two-dimensional code picture  $filename = ' qrcode/'. Microtime (). PNG ';  QRCode::p ng ($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);  $QR = $filename;        The original two-dimensional code image file that has been generated  $QR = imagecreatefromstring (file_get_contents ($QR));  Output Image  Imagepng ($QR, ' qrcode.png ');  Imagedestroy ($QR);  Return ' 

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


2.  Add logo (generate picture file) function scerweima1 ($url = ") {require_once ' phpqrcode.php ') in the generated QR code;         $value = $url;  Two-dimensional code content $errorCorrectionLevel = ' H ';      Fault tolerance level $matrixPointSize = 6; Generate picture size//Generate two-dimensional code picture $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 map that has been generated if (file_exists ($logo)) {$QR = Imagecreatefromstring (file_get_contents ($QR));    Target Image connection resource.  $logo = imagecreatefromstring (file_get_contents ($logo));    Source Image Connection resource.      $QR _width = Imagesx ($QR);     Two-dimensional code picture width $QR _height = Imagesy ($QR);    Two-dimensional code image height $logo _width = imagesx ($logo);   Logo image width $logo _height = Imagesy ($logo);   Logo image Height $logo _qr_width = $QR _WIDTH/4;  The width of the logo after the combination (1/5 of the QR code) $scale = $logo _width/$logo _qr_width; The width scaling ratio of the logo (width/width after itself) $logo _qr_height = $logo _height/$scale;  The height of the logo after the combination $from _width = ($QR _width-$logo _qr_width)/2; After combination LoGo to the upper left corner coordinate point//regroup the picture and resize/* * 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 ' 

3. Generate QR code (no image file generated)


3. Generates the original QR code (does not generate a picture file) function scerweima2 ($url = ") {  require_once ' phpqrcode.php ';  $value = $url;         Two-dimensional code content  $errorCorrectionLevel = ' L ';  Fault tolerance level  $matrixPointSize = 5;      Generate picture size  //Generate two-dimensional code picture  $QR = QRCode::p ng ($value, False, $errorCorrectionLevel, $matrixPointSize, 2);} Call 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 into the browser.

Related Article

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.