|
| 1 | +# PHP Code 128 Encoder for Barcode Font |
| 2 | + |
| 3 | +Encode text for use with Code 128 fonts such as [Libre Barcode 128](https://github.com/graphicore/librebarcode). The actual encoding is done by [tc-lib-barcode](https://github.com/tecnickcom/tc-lib-barcode). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install PHP Code 128 Encoder using [Composer](https://getcomposer.org/): |
| 8 | + |
| 9 | +``` |
| 10 | +composer require hbgl/php-code-128-encoder |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +```php |
| 16 | +<?php |
| 17 | + |
| 18 | +require __DIR__ . '/../vendor/autoload.php'; |
| 19 | + |
| 20 | +use Hbgl\Barcode\Code128Encoder; |
| 21 | + |
| 22 | +// Encode as Code 128. |
| 23 | +$encoded = Code128Encoder::encode('ABC123456DEF'); |
| 24 | +assert ($encoded === 'ÌABCÇ,BXÈDEFqÎ'); |
| 25 | + |
| 26 | +// Only encode using Type A. |
| 27 | +$encodedA = Code128Encoder::encode('ABC123456DEF', 'A'); |
| 28 | +assert ($encodedA === 'ËABC123456DEFLÎ'); |
| 29 | + |
| 30 | +// Only encode using Type B. |
| 31 | +$encodedB = Code128Encoder::encode('AcC123456DeF', 'B'); |
| 32 | +assert ($encodedB === 'ÌAcC123456DeFSÎ'); |
| 33 | + |
| 34 | +// Only encode using Type C. |
| 35 | +$encodedC = Code128Encoder::encode('123456', 'C'); |
| 36 | +assert ($encodedC === 'Í,BXLÎ'); |
| 37 | +``` |
| 38 | + |
| 39 | +Use it together with the [Libre Barcode 128](https://github.com/graphicore/librebarcode) font to show a barcode on a webpage: |
| 40 | + |
| 41 | +```php |
| 42 | +<?php |
| 43 | + |
| 44 | +require __DIR__ . '/../vendor/autoload.php'; |
| 45 | + |
| 46 | +use Hbgl\Barcode\Code128Encoder; |
| 47 | + |
| 48 | +$content = 'ABC123456DEF'; |
| 49 | +$encoded = Code128Encoder::encode($content); |
| 50 | + |
| 51 | +?> |
| 52 | +<!DOCTYPE html> |
| 53 | +<html> |
| 54 | + <head> |
| 55 | + <meta charset="utf-8"> |
| 56 | + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> |
| 57 | + <title>Code 128</title> |
| 58 | + <link href="https://fonts.googleapis.com/css?family=Libre+Barcode+128&display=swap" rel="stylesheet"> |
| 59 | + <style> |
| 60 | + body { |
| 61 | + text-align: center; |
| 62 | + } |
| 63 | + .code128 { |
| 64 | + padding: 3rem 1.5rem 0 1.5rem; |
| 65 | + font-family: "Libre Barcode 128"; |
| 66 | + font-size: 3rem; |
| 67 | + transform: scaleY(1.5); |
| 68 | + } |
| 69 | + </style> |
| 70 | + </head> |
| 71 | + <body> |
| 72 | + <div class="code128"><?= htmlspecialchars($encoded) ?></div> |
| 73 | + <div><?= htmlspecialchars($content) ?></div> |
| 74 | + </body> |
| 75 | +</html> |
| 76 | +``` |
| 77 | + |
| 78 | +Result: |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +This library is licensed under the [MIT license](https://opensource.org/licenses/MIT). |
0 commit comments