Skip to content

Commit eb1cbcc

Browse files
committed
Merge branch 'refs/heads/main' into upc-e
2 parents 4f1bb66 + 17f981c commit eb1cbcc

32 files changed

+802
-125
lines changed

.github/workflows/phpstan.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Static analysis (phpstan)
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: 8.3
23+
extensions: mbstring, gd, bcmath, imagick
24+
25+
- name: Install dependencies
26+
run: composer install --prefer-dist --no-progress --no-interaction
27+
28+
- name: Run analysis
29+
run: vendor/bin/phpstan --error-format=github --no-progress

.github/workflows/phpunit.yml

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
name: phpunit
1+
name: Unit tests (phpunit)
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
48

59
jobs:
610
build:
7-
811
runs-on: ubuntu-latest
912

1013
strategy:
1114
matrix:
12-
php-versions: ['7.3', '7.4', '8.0', '8.1']
15+
php-versions: ['8.1', '8.2', '8.3']
1316

1417
steps:
15-
- uses: actions/checkout@v2
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
1622

1723
- name: Setup PHP
1824
uses: shivammathur/setup-php@v2
@@ -24,7 +30,7 @@ jobs:
2430
run: composer validate
2531

2632
- name: Install dependencies
27-
run: composer install --prefer-dist --no-progress
33+
run: composer install --prefer-dist --no-progress --no-interaction
2834

2935
- name: Run test suite
3036
run: composer run-script test

CHANGELOG.md

-56
This file was deleted.

Readme.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# PHP Barcode Generator
2-
[![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=main)](https://travis-ci.org/picqer/php-barcode-generator) [![Github Actions](https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator)
2+
<a href="https://github.com/picqer/php-barcode-generator/actions"><img src="https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg" alt="Build Status"></a>
3+
<a href="https://packagist.org/packages/picqer/php-barcode-generator"><img src="https://img.shields.io/packagist/dt/picqer/php-barcode-generator" alt="Total Downloads"></a>
4+
<a href="https://packagist.org/packages/picqer/php-barcode-generator"><img src="https://img.shields.io/packagist/v/picqer/php-barcode-generator" alt="Latest Stable Version"></a>
35

4-
This is an easy to use, non-bloated, framework independent, barcode generator in PHP.
6+
This is an easy to use, non-bloated, framework independent, barcode generator in PHP. It uses zero(!) composer dependencies and is only a handful of files. Probably the reason that this is the most downloaded barcode generator for PHP on Packagist. ;)
57

68
It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards.
79

@@ -32,6 +34,9 @@ $generator = new Picqer\Barcode\BarcodeGeneratorHTML();
3234
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
3335
```
3436

37+
Will result in this beauty:<br>
38+
![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg)
39+
3540
The `getBarcode()` method accepts the following parameters:
3641
- `$barcode` String needed to encode in the barcode
3742
- `$type` Type of barcode, use the constants defined in the class
@@ -84,6 +89,7 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner
8489
- TYPE_EAN_5
8590
- TYPE_EAN_8
8691
- TYPE_EAN_13
92+
- TYPE_ITF14 (Also known as GTIN-14)
8793
- TYPE_UPC_A
8894
- TYPE_UPC_E
8995
- TYPE_MSI

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^7.3|^8.0",
21+
"php": "^8.1",
2222
"ext-mbstring": "*"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^9.5"
25+
"phpunit/phpunit": "^9.5",
26+
"phpstan/phpstan": "^1.10"
2627
},
2728
"suggest": {
2829
"ext-bcmath": "Barcode IMB (Intelligent Mail Barcode) needs bcmath extension",

examples.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ These are examples of supported barcodes with this library.
66

77
### C39
88

9-
![Barcode 1234567890abcABC as C39](tests/verified-files/C39-1234567890abcABC.svg)
9+
![Barcode 1234567890ABC as C39](tests/verified-files/C39-1234567890ABC.svg)
1010

1111
### C39+
1212

13-
![Barcode 1234567890abcABC as C39+](tests/verified-files/C39+-1234567890abcABC.svg)
13+
![Barcode 1234567890ABC as C39+](tests/verified-files/C39+-1234567890ABC.svg)
14+
15+
### C39E
16+
17+
![Barcode 1234567890abcABC as C39E](tests/verified-files/C39E-1234567890abcABC.svg)
1418

1519
### C39E+
1620

@@ -44,6 +48,12 @@ These are examples of supported barcodes with this library.
4448

4549
![Barcode 004900000463 as EAN13](tests/verified-files/EAN13-004900000463.svg)
4650

51+
### ITF14
52+
53+
![Barcode 00012345600012 as ITF14](tests/verified-files/ITF14-00012345600012.svg)
54+
55+
![Barcode 05400141288766 as ITF14](tests/verified-files/ITF14-05400141288766.svg)
56+
4757
### C128
4858

4959
![Barcode 081231723897 as C128](tests/verified-files/C128-081231723897.svg)

generate-verified-files.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function getSaveFilename($value) {
88

99
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG();
1010
file_put_contents('tests/verified-files/081231723897-ean13.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13));
11+
file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13, 0.25, 25.75));
1112

1213
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML();
1314
file_put_contents('tests/verified-files/081231723897-code128.html', $generatorHTML->getBarcode('081231723897', $generatorHTML::TYPE_CODE_128));

phpstan.neon.dist

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
paths:
3+
- src
4+
level: 4

phpunit.xml

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
processIsolation="false"
9-
stopOnError="false"
10-
stopOnFailure="false"
11-
verbose="true"
12-
>
13-
<testsuites>
14-
<testsuite name="Barcode Test Suite">
15-
<directory>./tests/</directory>
16-
</testsuite>
17-
</testsuites>
18-
<filter>
19-
<whitelist>
20-
<directory>./src</directory>
21-
</whitelist>
22-
</filter>
23-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnError="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Barcode Test Suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/BarcodeGenerator.php

+15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use Picqer\Barcode\Types\TypeIntelligentMailBarcode;
4848
use Picqer\Barcode\Types\TypeInterleaved25;
4949
use Picqer\Barcode\Types\TypeInterleaved25Checksum;
50+
use Picqer\Barcode\Types\TypeITF14;
5051
use Picqer\Barcode\Types\TypeKix;
5152
use Picqer\Barcode\Types\TypeMsi;
5253
use Picqer\Barcode\Types\TypeMsiChecksum;
@@ -57,6 +58,7 @@
5758
use Picqer\Barcode\Types\TypeRms4cc;
5859
use Picqer\Barcode\Types\TypeStandard2of5;
5960
use Picqer\Barcode\Types\TypeStandard2of5Checksum;
61+
use Picqer\Barcode\Types\TypeTelepen;
6062
use Picqer\Barcode\Types\TypeUpcA;
6163
use Picqer\Barcode\Types\TypeUpcE;
6264
use Picqer\Barcode\Types\TypeUpcExtension2;
@@ -74,6 +76,7 @@ abstract class BarcodeGenerator
7476
const TYPE_STANDARD_2_5_CHECKSUM = 'S25+';
7577
const TYPE_INTERLEAVED_2_5 = 'I25';
7678
const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+';
79+
const TYPE_ITF_14 = 'ITF14';
7780
const TYPE_CODE_128 = 'C128';
7881
const TYPE_CODE_128_A = 'C128A';
7982
const TYPE_CODE_128_B = 'C128B';
@@ -88,6 +91,8 @@ abstract class BarcodeGenerator
8891
const TYPE_MSI_CHECKSUM = 'MSI+'; // MSI + CHECKSUM (modulo 11)
8992
const TYPE_POSTNET = 'POSTNET';
9093
const TYPE_PLANET = 'PLANET';
94+
const TYPE_TELEPEN_ALPHA = 'TELEPENALPHA';
95+
const TYPE_TELEPEN_NUMERIC = 'TELEPENNUMERIC';
9196
const TYPE_RMS4CC = 'RMS4CC'; // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
9297
const TYPE_KIX = 'KIX'; // KIX (Klant index - Customer index)
9398
const TYPE_IMB = 'IMB'; // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
@@ -136,6 +141,9 @@ protected function createDataBuilderForType(string $type)
136141
case self::TYPE_INTERLEAVED_2_5_CHECKSUM:
137142
return new TypeInterleaved25Checksum();
138143

144+
case self::TYPE_ITF_14:
145+
return new TypeITF14();
146+
139147
case self::TYPE_CODE_128:
140148
return new TypeCode128();
141149

@@ -198,6 +206,13 @@ protected function createDataBuilderForType(string $type)
198206

199207
case self::TYPE_PHARMA_CODE_TWO_TRACKS:
200208
return new TypePharmacodeTwoCode();
209+
210+
case self::TYPE_TELEPEN_ALPHA:
211+
return new TypeTelepen();
212+
213+
case self::TYPE_TELEPEN_NUMERIC:
214+
return new TypeTelepen('numeric');
215+
201216
}
202217

203218
throw new UnknownTypeException();

src/BarcodeGeneratorDynamicHTML.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator
1111
* This 'dynamic' version uses percentage based widths and heights, resulting in a vector-y qualitative result.
1212
*
1313
* @param string $barcode code to print
14-
* @param string $type type of barcode
14+
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode
1515
* @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent).
1616
* @return string HTML code.
1717
*/
18-
public function getBarcode($barcode, $type, string $foregroundColor = 'black')
18+
public function getBarcode(string $barcode, $type, string $foregroundColor = 'black'): string
1919
{
2020
$barcodeData = $this->getBarcodeData($barcode, $type);
2121

src/BarcodeGeneratorHTML.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class BarcodeGeneratorHTML extends BarcodeGenerator
99
* This original version uses pixel based widths and heights. Use Dynamic HTML version for better quality representation.
1010
*
1111
* @param string $barcode code to print
12-
* @param string $type type of barcode
12+
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode
1313
* @param int $widthFactor Width of a single bar element in pixels.
1414
* @param int $height Height of a single bar element in pixels.
1515
* @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent).
1616
* @return string HTML code.
1717
*/
18-
public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black')
18+
public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string
1919
{
2020
$barcodeData = $this->getBarcodeData($barcode, $type);
2121

src/BarcodeGeneratorPNG.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class BarcodeGeneratorPNG extends BarcodeGenerator
1111
{
1212
protected $useImagick = true;
1313

14+
/**
15+
* @throws BarcodeException
16+
*/
1417
public function __construct()
1518
{
1619
// Auto switch between GD and Imagick based on what is installed
@@ -43,13 +46,13 @@ public function useGd()
4346
* Return a PNG image representation of barcode (requires GD or Imagick library).
4447
*
4548
* @param string $barcode code to print
46-
* @param string $type type of barcode:
49+
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode
4750
* @param int $widthFactor Width of a single bar element in pixels.
4851
* @param int $height Height of a single bar element in pixels.
4952
* @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent).
5053
* @return string image data or false in case of error.
5154
*/
52-
public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0])
55+
public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string
5356
{
5457
$barcodeData = $this->getBarcodeData($barcode, $type);
5558
$width = round($barcodeData->getWidth() * $widthFactor);
@@ -73,7 +76,7 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height =
7376
$barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3);
7477

7578
// draw a vertical bar
76-
if ($this->useImagick && isset($imagickBarsShape)) {
79+
if ($this->useImagick) {
7780
$imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight));
7881
} else {
7982
imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor);
@@ -82,7 +85,7 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height =
8285
$positionHorizontal += $barWidth;
8386
}
8487

85-
if ($this->useImagick && isset($imagickBarsShape)) {
88+
if ($this->useImagick) {
8689
$image = $this->createImagickImageObject($width, $height);
8790
$image->drawImage($imagickBarsShape);
8891
return $image->getImageBlob();

0 commit comments

Comments
 (0)