hbgl/php-code-128-encoder

Encode text for use with Code 128 fonts such as Libre Barcode 128.

1.0.0 2023-12-04 20:34 UTC

This package is auto-updated.

Last update: 2024-04-04 21:34:21 UTC


README

CI Latest Stable Version

Encode text for use with Code 128 fonts such as Libre Barcode 128. The actual encoding is done by tc-lib-barcode.

Installation

Install PHP Code 128 Encoder using Composer:

composer require hbgl/php-code-128-encoder

Usage

<?php

require __DIR__ . '/../vendor/autoload.php';

use Hbgl\Barcode\Code128Encoder;

// Encode as Code 128.
$encoded = Code128Encoder::encode('ABC123456DEF');
assert ($encoded === 'ÌABCÇ,BXÈDEFqÎ');

// Only encode using Type A.
$encodedA = Code128Encoder::encode('ABC123456DEF', 'A');
assert ($encodedA === 'ËABC123456DEFLÎ');

// Only encode using Type B.
$encodedB = Code128Encoder::encode('AcC123456DeF', 'B');
assert ($encodedB === 'ÌAcC123456DeFSÎ');

// Only encode using Type C.
$encodedC = Code128Encoder::encode('123456', 'C');
assert ($encodedC === 'Í,BXLÎ');

Use it together with the Libre Barcode 128 font to show a barcode on a webpage:

<?php

require __DIR__ . '/../vendor/autoload.php';

use Hbgl\Barcode\Code128Encoder;

$content = 'ABC123456DEF';
$encoded = Code128Encoder::encode($content);

?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>Code 128</title>
        <link href="https://fonts.googleapis.com/css?family=Libre+Barcode+128&display=swap" rel="stylesheet">
        <style>
            body {
                text-align: center;
            }
            .code128 {
                padding: 3rem 1.5rem 0 1.5rem;
                font-family: "Libre Barcode 128";
                font-size: 3rem;
                transform: scaleY(1.5);
            }
        </style>
    </head>
    <body>
        <div class="code128"><?= htmlspecialchars($encoded) ?></div>
        <div><?= htmlspecialchars($content) ?></div>
    </body>
</html>

Result:

Barcode Result

PHP 7.x

Support for PHP 7.x ended with version 0.2.7.

License

This library is licensed under the MIT license.