Home  >  Article  >  Backend Development  >  How to generate different barcodes in php

How to generate different barcodes in php

藏色散人
藏色散人Original
2022-10-31 09:53:392041browse

How to generate different barcodes in php: 1. Install the required code library through the "composer require codeitnowin/barcode" statement; 2. Write the data flow file into the content of the created file; 3. Output the picture Just file.

How to generate different barcodes in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php How to generate different barcodes?

php Generate barcode:

Barcode (barcode) is to arrange multiple black bars and blanks with different widths according to certain encoding rules. Use A graphical identifier that expresses a set of information.

This is implemented using PHP and supports most coding systems.

Barcode length

Code128, Code39 Extended, Code93 Extended supports all ASCII codes.

Code128 has three character sets: A, B, and C. Each character set supports part of it, roughly like this.

A character set supports AZ 26 uppercase letters, 09 9 Numbers and some special characters;

B character set supports AZ 26 uppercase and lowercase letters, 09 9 numbers and some special characters;

C character set supports pure numeric characters and 0` 9 9 numbers, and it is an even number. If it is an odd number, 0 will be automatically added in front when generating the barcode; the maximum length of pure characters is 32 bits, and pure characters plus special symbols are 44 bits. EAN/UCC 128 is the same as Code 128.

CODE128 code is a barcode system widely used in enterprise internal management, production processes, and logistics control systems. Due to its excellent characteristics, it is widely used in the design of management information systems. Use, CODE128 code is one of the most widely used barcode coding systems.

CODE128 code is a high-density barcode introduced in 1981. CODE128 code can represent a total of 128 characters from ASCII 0 to ASCII 127, so it is called 128 code. It contains numbers, letters and symbolic characters.

 Code 128 code and Code 39 code have many similarities, and both are widely used in corporate internal management, production processes, and logistics control systems. The difference is that Code 128 can represent more characters than Code 39, and the coding density per unit length is higher. When Code 39 encoding cannot be accommodated in the unit length or the encoded characters exceed the limits of Code 39, Code 128 can be selected for encoding.

Code39, Code93 support the range of 09 numeric characters and AZ uppercase letters and '/', ' ', '%', '$', '-', '.' and spaces, the length is theoretically unlimited limit.

Code39 Extended, Code93 Extended does not support ‘-’, ‘.’, others are the same as 39 93.

EAN8,EAN13,EAN Ext,UPC A,UPC E,UPC Ext supports the range of 0`9 numeric characters, the length of EAN8 is 8, the length of EAN13 is 13, the length of EAN8 Ext is 10 or 13, EAN13 The Ext length is 15 or 18, the UPCA length is 12, the UPCE length is 8, the UPCE Ext length is 10 or 13, and the UPCA Ext length is 14 or 17.

Bookland, ISSN, ISSN may support characters other than numbers. For support of this middleware, please refer to the description in the "About Bookland (ISBN) and ISSN" section.

Code11 supports the range of 0`9 numeric characters and ‘-’, and the length is theoretically unlimited.

Codabar supports the range of 0`9 numeric characters and 'A', 'B', 'C', 'D', '-', '.', '/', ':', ' ', '$', the starting and ending characters must be characters in 'A', 'B', 'C', 'D', and the length is theoretically not limited.

MSI, Code 2of5 supports a range of 0`9 numeric characters, and the length is theoretically unlimited.

PostNet supports a range of 0`9 numeric characters, with a length of 5, 9, or 11 digits. This development kit supports formats with non-numeric characters, such as 12345-8012, which is more convenient to use.

Matrix 25 (matrix 25 code) supports a range of 0`9 numeric characters and a length of 13 digits.

Install the required code libraries

composer require codeitnowin/barcode

Baidu Cloud address: https://pan.baidu.com/s/1-hFWYfed4y3YkWKLz2o75g

Related code (specifically shown below)

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
 
use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
use CodeItNow\BarcodeBundle\Utils\QrCode;
 
class BarCodeController extends Controller
{
    public function index()
    {
 
        echo &#39;<p>Example - QrCode</p>&#39;;
        $qrCode = new QrCode();
        $qrCode
            ->setText(&#39;https://www.php.net/manual/zh/&#39;)
            ->setSize(300)
            ->setPadding(10)
            ->setErrorCorrection(&#39;high&#39;)
            ->setForegroundColor(array(&#39;r&#39; => 0, &#39;g&#39; => 0, &#39;b&#39; => 0, &#39;a&#39; => 0))
            ->setBackgroundColor(array(&#39;r&#39; => 255, &#39;g&#39; => 255, &#39;b&#39; => 255, &#39;a&#39; => 0))
            ->setLabel(&#39;https://www.php.net/&#39;)
            ->setLabelFontSize(16)
            ->setImageType(QrCode::IMAGE_TYPE_PNG);
        echo &#39;<img  src="data:&#39; . $qrCode- alt="How to generate different barcodes in php" >getContentType() . &#39;;base64,&#39; . $qrCode->generate() . &#39;" />&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Code128</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("0123456789");
        $barcode->setType(BarcodeGenerator::Code128);
        $barcode->setScale(2);
        $barcode->setThickness(25);
        $barcode->setFontSize(10);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Code11</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("0123456789");
        $barcode->setType(BarcodeGenerator::Code11);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Code39</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("0123456789");
        $barcode->setType(BarcodeGenerator::Code39);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Code39Extended</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("0123456789");
        $barcode->setType(BarcodeGenerator::Code39Extended);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Ean128</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("00123456789012345675");
        $barcode->setType(BarcodeGenerator::Ean128);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Gs1128</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("00123456789012345675");
        $barcode->setType(BarcodeGenerator::Gs1128);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Gs1128</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("4157707266014651802001012603068039000000006377069620171215");
        $barcode->setType(BarcodeGenerator::Gs1128);
        $barcode->setNoLengthLimit(true);
        $barcode->setAllowsUnknownIdentifier(true);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - I25</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("00123456789012345675");
        $barcode->setType(BarcodeGenerator::I25);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Isbn</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("0012345678901");
        $barcode->setType(BarcodeGenerator::Isbn);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Msi</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("0012345678901");
        $barcode->setType(BarcodeGenerator::Msi);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Postnet</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("01234567890");
        $barcode->setType(BarcodeGenerator::Postnet);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - S25</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("012345678901");
        $barcode->setType(BarcodeGenerator::S25);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Upca</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("012345678901");
        $barcode->setType(BarcodeGenerator::Upca);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
        echo &#39;<hr>&#39;;
        echo &#39;<p>Example - Upce</p>&#39;;
        $barcode = new BarcodeGenerator();
        $barcode->setText("012345");
        $barcode->setType(BarcodeGenerator::Upce);
        $code = $barcode->generate();
        echo &#39;<img  src="https://img-blog.csdnimg.cn/2022010611514192897.png&#39; . $code . &#39;" / alt="How to generate different barcodes in php" >&#39;;
 
    }
}

How to generate different barcodes in php

How to convert image data in base64 format into images

// $base_img是获取到前端传递的值
            $base_img = str_replace(&#39;data:image/jpg;base64,&#39;, &#39;&#39;, $code);
//  设置文件路径和命名文件名称
            $path = "D:/Uploads/Bar/2019-12-11/";
            $output_file = time().rand(100,999).&#39;.jpg&#39;;
            $path = $path.$output_file;
//  创建将数据流文件写入我们创建的文件内容中
            file_put_contents($path, base64_decode($base_img));
// 输出文件
            print_r($output_file);
Buy me a cup of coffee :)

Recommended learning: " PHP video tutorial

The above is the detailed content of How to generate different barcodes in php. 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