Home  >  Article  >  Backend Development  >  How to use PHP to generate QR codes and barcodes

How to use PHP to generate QR codes and barcodes

PHPz
PHPzOriginal
2023-09-05 11:48:222374browse

如何使用 PHP 实现生成二维码和条形码功能

How to use PHP to realize the function of generating QR codes and barcodes

Introduction:
With the advent of mobile payment and digital era, the development of QR codes and barcodes Use is becoming more common. In the commercial field, generating QR codes and barcodes is a very practical function. PHP is a popular server-side programming language that can be used to dynamically generate QR codes and barcodes. This article explains how to use PHP to achieve this functionality.

1. Generate QR code

  1. Installing dependencies
    Before we start, we need to install some necessary dependency libraries. PHP supports a variety of libraries for generating QR codes. In this article, we choose to use the phpqrcode library.

First, you need to use composer to install the library. Execute the following command in the terminal:

composer require chillerlan/php-qrcode
  1. Create a PHP file
    Create a qrcode.php file in the root directory of the project.
  2. Write the code

    <?php
    require 'vendor/autoload.php';
    
    use chillerlanQRCode{QRCode, QROptions};
    
    $qrCode = new QRCode(new QROptions([
     'outputType' => QRCode::OUTPUT_IMAGE_PNG,
     'eccLevel'   => QRCode::ECC_H,
     'imageBase64' => false
    ]));
    
    $qrCode->render('https://example.com', 'path/to/save/qrcode.png');
  3. Run the code
    Execute the following command in the terminal:

    php qrcode.php
  4. View the generated two The QR code
    will generate a QR code image named qrcode.png under the specified saving path.

2. Generate barcode

  1. Installation dependencies
    Same as generating QR codes, we need to install the barcode generation library. In this article we choose to use the bacon/bacon-qr-code library.

Execute the following command to install the library:

composer require bacon/bacon-qr-code
  1. Create a PHP file
    Create a barcode.php file in the root directory of the project .
  2. Write the code

    <?php
    require 'vendor/autoload.php';
    
    use BaconQrCode{Code128, RendererImagePng};
    
    $code = '1234567890'; // 要生成条形码的内容
    $renderer = new Png(); // 使用 PNG 格式输出
    
    $writer = new Code128();
    $writer->setRenderer($renderer);
    $writer->setCode($code);
    $writer->writeFile('path/to/save/barcode.png');
  3. Run the code
    Execute the following command in the terminal:

    php barcode.php
  4. View the generated barcode
    A barcode image named barcode.png will be generated under the specified save path.

Summary:
By using the PHP library, we can easily realize the function of generating QR codes and barcodes. In actual development, the generated QR codes and barcodes can be customized according to needs, such as adding logos, adjusting sizes, etc. I hope this article can help you quickly get started with the function of generating QR codes and barcodes.

The above is the detailed content of How to use PHP to generate QR codes and barcodes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn