Using Phpqrcode to generate two-dimensional code

Source: Internet
Author: User
Tags php language

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, which contains a lot of files and demo program, we only need the inside of the phpqrcode.php this file can generate a QR 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 QR code contains the content, can be a link, text, JSON string, etc.
2nd parameter $outfile: Default is False, do not generate files, only the QR code picture return output Otherwise, it is necessary to give the file name and path of the image of the generated QR code;
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 recognized;
The 4th parameter $size: Controls the size of the resulting picture, the default is 4;
The 5th parameter $margin: Control the size of the white space to generate the QR code;
6th parameter $saveandprint: Save the QR code picture and display it, $outfile must pass the picture path;

Examples of Use:
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 images
$filename = ' qrcode/'. Microtime (). PNG ';
QRCode::p ng ($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $filename; Original two-dimensional 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 ');
Add logo to generated QR code (create picture file)
2. Add logo to generated QR code (create picture file)
function scerweima1 ($url = ") {
Require_once ' phpqrcode.php ';
$value = $url; Two-dimensional code content
$errorCorrectionLevel = ' H '; Fault tolerance level
$matrixPointSize = 6; Generate picture size
Generate two-dimensional code images
$filename = ' qrcode/'. Microtime (). PNG ';
QRCode::p ng ($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
$logo = ' qrcode/logo.jpg '; Ready logo image
$QR = $filename; Original two-dimensional code diagram 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 image 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 of the logo (width/width of the combination)
$logo _qr_height = $logo _height/$scale; The height of the logo after the combination
$from _width = ($QR _width-$logo _qr_width)/2; The coordinate point of the upper left corner of the logo after combining
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 ');

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.